From c2bb1d600f224b9e6ab41d86a1e59e5f0ba648ba Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 23 Jan 2018 19:36:09 -0800 Subject: [PATCH] added server route for authentication --- react/containers/NavBar/view.jsx | 32 ++++++++++++++++++++------------ react/utils/auth.js | 9 +++++++++ routes/auth-routes.js | 8 ++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 react/utils/auth.js diff --git a/react/containers/NavBar/view.jsx b/react/containers/NavBar/view.jsx index fe2538e4..37457022 100644 --- a/react/containers/NavBar/view.jsx +++ b/react/containers/NavBar/view.jsx @@ -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 diff --git a/react/utils/auth.js b/react/utils/auth.js new file mode 100644 index 00000000..1b0deeae --- /dev/null +++ b/react/utils/auth.js @@ -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'); + }, +}; diff --git a/routes/auth-routes.js b/routes/auth-routes.js index 01419e1b..ff50a356 100644 --- a/routes/auth-routes.js +++ b/routes/auth-routes.js @@ -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'}); + } + }); };