diff --git a/src/actions/index.js b/src/actions/index.js index 043738f..49810bd 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -25,9 +25,14 @@ export const userLogin = (username, password) => async (dispatch) => { } ) if (response.status === 401) { - console.log("Please check your credentials") + dispatch({ type: 'USER_LOGIN', payload: null }) } if (response.status === 200) { dispatch({ type: 'USER_LOGIN', payload: response }) } }; + +export const checkJWT = (jwt) => async (dispatch) => { + + dispatch({ type: 'FETCH_USER', payload: null }) +}; diff --git a/src/components/AuthMenu.js b/src/components/AuthMenu.js index c8cfe24..5027e91 100644 --- a/src/components/AuthMenu.js +++ b/src/components/AuthMenu.js @@ -8,13 +8,41 @@ class AuthMenu extends Component { constructor (props) { super(props) - this.state = { - user_authed: false, - where_in_auth_menu: "requestUsername", - username: "", - password: "", - email: "" + const cookies = new Cookies(); + const datacookie = cookies.get('DataCookie'); + + if (datacookie) { + const data = atob(datacookie.split('.')[1]) + if (data.exp < Date.now()) { + this.state = { + user_authed: true, + where_in_auth_menu: "loggedIn", + username: data.username, + exp: data.exp, + } + } + else { + this.state = { + user_authed: false, + where_in_auth_menu: "requestUsername", + username: "", + password: "", + email: "", + exp: null, + } + } } + else { + this.state = { + user_authed: false, + where_in_auth_menu: "requestUsername", + username: "", + password: "", + email: "", + exp: null + } + } + this.handleInputChange = this.handleInputChange.bind(this) @@ -27,7 +55,6 @@ class AuthMenu extends Component { this.handleCreateAccount = this.handleCreateAccount.bind(this) this.authMenu = this.authMenu.bind(this) - } @@ -63,7 +90,6 @@ class AuthMenu extends Component { } handleLogin() { - const cookies = new Cookies(); this.props.userLogin(this.state.username, this.state.password) .then(res => { this.setState(state => ({ @@ -72,7 +98,6 @@ class AuthMenu extends Component { this.props.close() } ) - .then(console.log(cookies.getAll())) .catch(err => { console.log(err); }) diff --git a/src/reducers/authReducer.js b/src/reducers/authReducer.js index ef1c28a..0afae7d 100644 --- a/src/reducers/authReducer.js +++ b/src/reducers/authReducer.js @@ -8,10 +8,14 @@ const initialState = { export default (state = initialState, action) => { switch (action.type) { case 'USER_LOGIN': - return {...state, ...{ - username: action.payload.data.username, - user_logged_in: true - }} + 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; }