Get cookie loaded into redux state
This commit is contained in:
parent
c7e2bb6d74
commit
b8a90d9615
4 changed files with 30 additions and 27 deletions
|
@ -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 })
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -39,7 +39,8 @@ class NavBar extends Component {
|
|||
|
||||
const mapStateToProps = (state) => {
|
||||
return {
|
||||
auth: state.auth
|
||||
user_authed: state.user_authed,
|
||||
username: state.username
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
};
|
||||
|
|
Reference in a new issue