added server route for authentication
This commit is contained in:
parent
c5b22c287b
commit
c2bb1d600f
3 changed files with 37 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
|||
import React from 'react';
|
||||
import { getUserCookies, clearUserCookies } from 'utils/cookies';
|
||||
import { clearUserCookies } from 'utils/cookies';
|
||||
import { authenticateUser } from 'utils/auth';
|
||||
import Logo from 'components/Logo';
|
||||
import NavBarChannelDropdown from 'components/NavBarChannelDropdown';
|
||||
|
||||
|
@ -10,6 +11,7 @@ class NavBar extends React.Component {
|
|||
constructor (props) {
|
||||
super(props);
|
||||
this.checkForLoggedInUser = this.checkForLoggedInUser.bind(this);
|
||||
this.logoutUser = this.logoutUser.bind(this);
|
||||
this.handleSelection = this.handleSelection.bind(this);
|
||||
}
|
||||
componentDidMount () {
|
||||
|
@ -18,23 +20,29 @@ class NavBar extends React.Component {
|
|||
}
|
||||
checkForLoggedInUser () {
|
||||
// check for whether a channel is already logged in
|
||||
let channelName, channelShortId, channelLongId;
|
||||
({ channelName, channelShortId, channelLongId } = getUserCookies());
|
||||
console.log(`cookies found for channel: ${channelName} ${channelShortId} ${channelLongId}`);
|
||||
if (channelName) {
|
||||
this.props.onChannelLogin(channelName, channelShortId, channelLongId);
|
||||
authenticateUser()
|
||||
.then(({success, message}) => {
|
||||
if (success) {
|
||||
this.props.onChannelLogin(message.channelName, message.shortChannelId, message.channelClaimId);
|
||||
} else {
|
||||
console.log('user was not logged in');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('authenticate user errored:', error);
|
||||
});
|
||||
}
|
||||
logoutUser () {
|
||||
// send logout request to server
|
||||
window.location.href = '/logout'; // NOTE: replace with a call to the server
|
||||
}
|
||||
handleSelection (event) {
|
||||
console.log('handling selection', event)
|
||||
console.log('handling selection', event);
|
||||
const value = event.target.selectedOptions[0].value;
|
||||
console.log('value', value);
|
||||
switch (value) {
|
||||
case LOGOUT:
|
||||
// remove session cookies
|
||||
clearUserCookies();
|
||||
// send logout request to server
|
||||
window.location.href = '/logout';
|
||||
this.logoutUser();
|
||||
break;
|
||||
case VIEW:
|
||||
// redirect to channel page
|
||||
|
|
9
react/utils/auth.js
Normal file
9
react/utils/auth.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
import {makeGetRequest} from 'utils/xhr';
|
||||
|
||||
module.exports = {
|
||||
authenticateUser () {
|
||||
// send authentication request to server
|
||||
// receive the user info back
|
||||
return makeGetRequest('/user');
|
||||
},
|
||||
};
|
|
@ -23,4 +23,12 @@ module.exports = (app) => {
|
|||
shortChannelId: req.user.shortChannelId,
|
||||
});
|
||||
});
|
||||
// see if user is authenticated, and return credentials if so
|
||||
app.get('/user', (req, res) => {
|
||||
if (req.user) {
|
||||
res.status(200).json({success: true, message: req.user});
|
||||
} else {
|
||||
res.status(200).json({success: false, message: 'user is not logged in'});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue