1
0
Fork 0

Get cookie loaded into redux state

This commit is contained in:
Amarpreet Minhas 2019-10-19 13:16:29 -04:00
parent c7e2bb6d74
commit b8a90d9615
4 changed files with 30 additions and 27 deletions

View file

@ -32,7 +32,6 @@ export const userLogin = (username, password) => async (dispatch) => {
} }
}; };
export const checkJWT = (jwt) => async (dispatch) => { export const loadCookieToState = (data) => async (dispatch) => {
dispatch({ type: 'LOAD_COOKIE', data })
dispatch({ type: 'FETCH_USER', payload: null })
}; };

View file

@ -2,7 +2,7 @@ import React, { Component } from 'react';
import Cookies from 'universal-cookie'; import Cookies from 'universal-cookie';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { userLogin } from '../actions'; import { userLogin, loadCookieToState } from '../actions';
class AuthMenu extends Component { class AuthMenu extends Component {
constructor (props) { constructor (props) {
@ -12,8 +12,8 @@ class AuthMenu extends Component {
const datacookie = cookies.get('DataCookie'); const datacookie = cookies.get('DataCookie');
if (datacookie) { if (datacookie) {
const data = atob(datacookie.split('.')[1]) const data = JSON.parse(atob(datacookie.split('.')[1]))
if (data.exp < Date.now()) { if (data.exp > Math.floor(Date.now() / 1000)) {
this.state = { this.state = {
user_authed: true, user_authed: true,
where_in_auth_menu: "loggedIn", where_in_auth_menu: "loggedIn",
@ -31,8 +31,7 @@ class AuthMenu extends Component {
exp: null, exp: null,
} }
} }
} } else {
else {
this.state = { this.state = {
user_authed: false, user_authed: false,
where_in_auth_menu: "requestUsername", where_in_auth_menu: "requestUsername",
@ -43,6 +42,7 @@ class AuthMenu extends Component {
} }
} }
this.props.loadCookieToState(this.state)
this.handleInputChange = this.handleInputChange.bind(this) this.handleInputChange = this.handleInputChange.bind(this)
@ -201,7 +201,8 @@ const mapStateToProps = (state) => {
username: state.username, username: state.username,
}; };
} }
export default connect( export default connect(
mapStateToProps, mapStateToProps,
{ userLogin } { userLogin, loadCookieToState }
)(AuthMenu); )(AuthMenu);

View file

@ -39,7 +39,8 @@ class NavBar extends Component {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
auth: state.auth user_authed: state.user_authed,
username: state.username
}; };
} }

View file

@ -1,22 +1,24 @@
const initialState = { const initialState = {
user_logged_in: false, user_authed: false,
username: '', where_in_auth_menu: "requestUsername",
email: "",
exp: null
}; };
export default (state = initialState, action) => { export default (state = initialState, action) => {
switch (action.type) { switch (action.type) {
case 'USER_LOGIN': case 'LOAD_COOKIE':
if (action.payload) { return {...state, ...action.data}
var data = JSON.parse(atob(action.payload.data.split('.')[1])) case 'USER_LOGIN':
return {...state, ...{ if (action.payload) {
user_logged_in: true, var data = JSON.parse(atob(action.payload.data.split('.')[1]))
username: data.username, return {...state, ...{
}} user_logged_in: true,
} username: data.username,
else return; }}
default: }
return state; else return;
} default:
return state
}
}; };