Created
November 11, 2020 23:48
-
-
Save blogcacanid/b22999704813d24c966018eeb819dfd4 to your computer and use it in GitHub Desktop.
auth.js Authentication JWT React JS Lumen 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
REGISTER_SUCCESS, | |
REGISTER_FAIL, | |
LOGIN_SUCCESS, | |
LOGIN_FAIL, | |
LOGOUT, | |
} from "../actions/types"; | |
const user = JSON.parse(localStorage.getItem("user")); | |
const initialState = user | |
? { isLoggedIn: true, user } | |
: { isLoggedIn: false, user: null }; | |
export default function (state = initialState, action) { | |
const { type, payload } = action; | |
switch (type) { | |
case REGISTER_SUCCESS: | |
return { | |
...state, | |
isLoggedIn: false, | |
}; | |
case REGISTER_FAIL: | |
return { | |
...state, | |
isLoggedIn: false, | |
}; | |
case LOGIN_SUCCESS: | |
return { | |
...state, | |
isLoggedIn: true, | |
user: payload.user, | |
}; | |
case LOGIN_FAIL: | |
return { | |
...state, | |
isLoggedIn: false, | |
user: null, | |
}; | |
case LOGOUT: | |
return { | |
...state, | |
isLoggedIn: false, | |
user: null, | |
}; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment