Skip to content

Commit

Permalink
refactor set localStorage and cookie into separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
atulmy committed Nov 7, 2017
1 parent 679eef2 commit b8812f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
16 changes: 15 additions & 1 deletion code/web/src/client/components/user/api/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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({
Expand All @@ -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 => {
Expand Down
10 changes: 3 additions & 7 deletions code/web/src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ 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
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)
}
}

Expand Down
10 changes: 8 additions & 2 deletions code/web/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit b8812f4

Please sign in to comment.