React/Redux - publish component #323
3 changed files with 37 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
||||||
import React from 'react';
|
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 Logo from 'components/Logo';
|
||||||
import NavBarChannelDropdown from 'components/NavBarChannelDropdown';
|
import NavBarChannelDropdown from 'components/NavBarChannelDropdown';
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ class NavBar extends React.Component {
|
||||||
constructor (props) {
|
constructor (props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.checkForLoggedInUser = this.checkForLoggedInUser.bind(this);
|
this.checkForLoggedInUser = this.checkForLoggedInUser.bind(this);
|
||||||
|
this.logoutUser = this.logoutUser.bind(this);
|
||||||
this.handleSelection = this.handleSelection.bind(this);
|
this.handleSelection = this.handleSelection.bind(this);
|
||||||
}
|
}
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
|
@ -18,23 +20,29 @@ class NavBar extends React.Component {
|
||||||
}
|
}
|
||||||
checkForLoggedInUser () {
|
checkForLoggedInUser () {
|
||||||
// check for whether a channel is already logged in
|
// check for whether a channel is already logged in
|
||||||
let channelName, channelShortId, channelLongId;
|
authenticateUser()
|
||||||
({ channelName, channelShortId, channelLongId } = getUserCookies());
|
.then(({success, message}) => {
|
||||||
console.log(`cookies found for channel: ${channelName} ${channelShortId} ${channelLongId}`);
|
if (success) {
|
||||||
if (channelName) {
|
this.props.onChannelLogin(message.channelName, message.shortChannelId, message.channelClaimId);
|
||||||
this.props.onChannelLogin(channelName, channelShortId, channelLongId);
|
} 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) {
|
handleSelection (event) {
|
||||||
console.log('handling selection', event)
|
console.log('handling selection', event);
|
||||||
const value = event.target.selectedOptions[0].value;
|
const value = event.target.selectedOptions[0].value;
|
||||||
console.log('value', value);
|
console.log('value', value);
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case LOGOUT:
|
case LOGOUT:
|
||||||
// remove session cookies
|
this.logoutUser();
|
||||||
clearUserCookies();
|
|
||||||
// send logout request to server
|
|
||||||
window.location.href = '/logout';
|
|
||||||
break;
|
break;
|
||||||
case VIEW:
|
case VIEW:
|
||||||
// redirect to channel page
|
// 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,
|
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