diff --git a/code/web/src/client/components/user/api/actions.js b/code/web/src/client/components/user/api/actions.js index d85490497..417335247 100644 --- a/code/web/src/client/components/user/api/actions.js +++ b/code/web/src/client/components/user/api/actions.js @@ -4,6 +4,7 @@ import axios from 'axios' // App Imports import { routesApi } from '../../../setup/routes' import { queryBuilder } from '../../../setup/helpers' +import cookie from 'js-cookie' // Actions Types export const LOGIN_REQUEST = 'AUTH/LOGIN_REQUEST' @@ -39,8 +40,11 @@ export function login(userCredentials) { error = response.data.errors[0].message } else if(response.data.data.userLogin.token !== '') { const token = response.data.data.userLogin.token + const user = response.data.data.userLogin.user - dispatch(setUser(token, response.data.data.userLogin.user)) + dispatch(setUser(token, user)) + + loginSetUserLocalStorageAndCookie(token, user) } dispatch({ @@ -57,6 +61,16 @@ export function login(userCredentials) { } } +// +export function loginSetUserLocalStorageAndCookie(token, user) { + // Update token + window.localStorage.setItem('token', token) + window.localStorage.setItem('user', JSON.stringify(user)) + + // Set cookie for SSR + cookie.set('token', { token, user }, { path: '/' }) +} + // Register a user export function register(userDetails) { return dispatch => { diff --git a/code/web/src/client/index.js b/code/web/src/client/index.js index 972c15ad4..6ed34e74d 100644 --- a/code/web/src/client/index.js +++ b/code/web/src/client/index.js @@ -3,11 +3,10 @@ import React from 'react' import { hydrate } from 'react-dom' import { BrowserRouter as Router } from 'react-router-dom' import { Provider } from 'react-redux' -import cookie from 'js-cookie' // App Imports import { store } from './setup/store' -import { setUser } from './components/user/api/actions' +import { setUser, loginSetUserLocalStorageAndCookie } from './components/user/api/actions' import App from './components/App' // User Authentication @@ -15,13 +14,10 @@ const token = window.localStorage.getItem('token') if(token && token !== 'undefined' && token !== '') { const user = JSON.parse(window.localStorage.getItem('user')) if(user) { - window.localStorage.setItem('token', token) - window.localStorage.setItem('user', JSON.stringify(user)) - + // Dispatch action store.dispatch(setUser(token, user)) - // Set cookie for SSR - cookie.set('token', { token, user }, { path: '/' }) + loginSetUserLocalStorageAndCookie(token, user) } } diff --git a/code/web/src/server/index.js b/code/web/src/server/index.js index 56ac157af..37472b55d 100644 --- a/code/web/src/server/index.js +++ b/code/web/src/server/index.js @@ -44,10 +44,16 @@ const store = createStore( // Match any Route app.get('*', (request, response) => { - const auth = JSON.parse(request.cookies.token) + // Check for auth + if(request.cookies.token) { + const auth = JSON.parse(request.cookies.token) - store.dispatch(setUser(auth.token, auth.user)) + if (auth && auth.token !== '' && auth.user) { + store.dispatch(setUser(auth.token, auth.user)) + } + } + // HTTP status code let status = 200 const matches = Object.values(routes).reduce((matches, route) => {