From b8a90d96151b922abd636d71589ba7fbf96c92b6 Mon Sep 17 00:00:00 2001 From: Asara Date: Sat, 19 Oct 2019 13:16:29 -0400 Subject: [PATCH] Get cookie loaded into redux state --- src/actions/index.js | 5 ++--- src/components/AuthMenu.js | 13 +++++++------ src/components/NavBar.js | 3 ++- src/reducers/authReducer.js | 36 +++++++++++++++++++----------------- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/actions/index.js b/src/actions/index.js index 49810bd..50f3a8c 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -32,7 +32,6 @@ export const userLogin = (username, password) => async (dispatch) => { } }; -export const checkJWT = (jwt) => async (dispatch) => { - - dispatch({ type: 'FETCH_USER', payload: null }) +export const loadCookieToState = (data) => async (dispatch) => { + dispatch({ type: 'LOAD_COOKIE', data }) }; diff --git a/src/components/AuthMenu.js b/src/components/AuthMenu.js index 5027e91..a64c0d2 100644 --- a/src/components/AuthMenu.js +++ b/src/components/AuthMenu.js @@ -2,7 +2,7 @@ import React, { Component } from 'react'; import Cookies from 'universal-cookie'; import { connect } from 'react-redux'; -import { userLogin } from '../actions'; +import { userLogin, loadCookieToState } from '../actions'; class AuthMenu extends Component { constructor (props) { @@ -12,8 +12,8 @@ class AuthMenu extends Component { const datacookie = cookies.get('DataCookie'); if (datacookie) { - const data = atob(datacookie.split('.')[1]) - if (data.exp < Date.now()) { + const data = JSON.parse(atob(datacookie.split('.')[1])) + if (data.exp > Math.floor(Date.now() / 1000)) { this.state = { user_authed: true, where_in_auth_menu: "loggedIn", @@ -31,8 +31,7 @@ class AuthMenu extends Component { exp: null, } } - } - else { + } else { this.state = { user_authed: false, where_in_auth_menu: "requestUsername", @@ -43,6 +42,7 @@ class AuthMenu extends Component { } } + this.props.loadCookieToState(this.state) this.handleInputChange = this.handleInputChange.bind(this) @@ -201,7 +201,8 @@ const mapStateToProps = (state) => { username: state.username, }; } + export default connect( mapStateToProps, - { userLogin } + { userLogin, loadCookieToState } )(AuthMenu); diff --git a/src/components/NavBar.js b/src/components/NavBar.js index cc0e320..17d8093 100644 --- a/src/components/NavBar.js +++ b/src/components/NavBar.js @@ -39,7 +39,8 @@ class NavBar extends Component { const mapStateToProps = (state) => { return { - auth: state.auth + user_authed: state.user_authed, + username: state.username }; } diff --git a/src/reducers/authReducer.js b/src/reducers/authReducer.js index 0afae7d..3b4fdc5 100644 --- a/src/reducers/authReducer.js +++ b/src/reducers/authReducer.js @@ -1,22 +1,24 @@ const initialState = { - user_logged_in: false, - username: '', + user_authed: false, + where_in_auth_menu: "requestUsername", + email: "", + exp: null }; - - export default (state = initialState, action) => { - switch (action.type) { - case 'USER_LOGIN': - if (action.payload) { - var data = JSON.parse(atob(action.payload.data.split('.')[1])) - return {...state, ...{ - user_logged_in: true, - username: data.username, - }} - } - else return; - default: - return state; - } + switch (action.type) { + case 'LOAD_COOKIE': + return {...state, ...action.data} + case 'USER_LOGIN': + if (action.payload) { + var data = JSON.parse(atob(action.payload.data.split('.')[1])) + return {...state, ...{ + user_logged_in: true, + username: data.username, + }} + } + else return; + default: + return state + } };