|
@@ -1,68 +1,120 @@
|
|
|
import { createReducer } from '../utils/reducer'
|
|
|
-import { REQUEST_OK, REQUEST_KO, LOGOUT } from '../constants/ActionTypes'
|
|
|
import { setToken } from '../utils/auth'
|
|
|
-import { has, isEqual } from 'lodash'
|
|
|
|
|
|
const initialState = {
|
|
|
- isAuthenticated: false
|
|
|
+ isTokenCheck: false,
|
|
|
+ isLogging: false,
|
|
|
+ isAuthenticated: false,
|
|
|
+ token: null,
|
|
|
+ username: null
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
- * @param {*} state
|
|
|
+ * @param {*} isTokenCheck
|
|
|
* @param {*} action
|
|
|
*/
|
|
|
-const checkAuthentication = (isAuthenticated, action) => {
|
|
|
- if (!has(action.payload, 'status')) {
|
|
|
- return isAuthenticated
|
|
|
+const isTokenCheck = (isTokenCheck, action) => {
|
|
|
+ if (action.type === 'CHECK_TOKEN_REQUEST') {
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
- // success auth
|
|
|
- if (isEqual(action.payload.status, 200)) {
|
|
|
- if (has(action.payload, 'token')) {
|
|
|
- setToken(action.payload.token)
|
|
|
+ if (action.type === 'CHECK_TOKEN_SUCCESS') {
|
|
|
+ return false
|
|
|
+ }
|
|
|
|
|
|
- return true;
|
|
|
- }
|
|
|
+ if (action.type === 'CHECK_TOKEN_FAILURE') {
|
|
|
+ setToken(null)
|
|
|
+ return false
|
|
|
+ }
|
|
|
|
|
|
+ return isTokenCheck
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ */
|
|
|
+const tokenCheckReducer = createReducer(false , {
|
|
|
+ 'CHECK_TOKEN_REQUEST': isTokenCheck,
|
|
|
+ 'CHECK_TOKEN_SUCCESS': isTokenCheck,
|
|
|
+ 'CHECK_TOKEN_FAILURE': isTokenCheck
|
|
|
+})
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} isLogging
|
|
|
+ * @param {*} action
|
|
|
+ */
|
|
|
+const isLogging = (isLogging, action) => {
|
|
|
+ if (action.type === 'LOGIN_REQUEST') {
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
- // error auth
|
|
|
- if (isEqual(action.payload.status, 401)) {
|
|
|
+ if (action.type === 'LOGIN_SUCCESS' || action.type === 'LOGIN_FAILURE') {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
- return isAuthenticated
|
|
|
+ return isLogging
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ */
|
|
|
+const logginReducer = createReducer(false, {
|
|
|
+ 'LOGIN_REQUEST': isLogging,
|
|
|
+ 'LOGIN_SUCCESS': isLogging,
|
|
|
+ 'LOGIN_FAILURE': isLogging
|
|
|
+})
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* @param {*} isAuthenticated
|
|
|
* @param {*} action
|
|
|
*/
|
|
|
-const revokeAuthentication = (isAuthenticated, action) => {
|
|
|
- setToken(null)
|
|
|
- return false
|
|
|
+const isAuthenticated = (isAuthenticated, action) => {
|
|
|
+ if (action.type === 'LOGIN_SUCCESS' || action.type === 'CHECK_TOKEN_SUCCESS') {
|
|
|
+ setToken(action.payload.token)
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ if (action.type === 'LOGIN_FAILURE' || action.type === 'CHECK_TOKEN_FAILURE' || action.type === 'LOGOUT_REQUEST') {
|
|
|
+ setToken(null)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return isAuthenticated
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
-const authenticationCheckReducer = createReducer(initialState, {
|
|
|
- [REQUEST_OK]: checkAuthentication,
|
|
|
- [REQUEST_KO]: checkAuthentication,
|
|
|
- [LOGOUT]: revokeAuthentication
|
|
|
-
|
|
|
+const authenticationReducer = createReducer(false, {
|
|
|
+ 'LOGIN_SUCCESS': isAuthenticated,
|
|
|
+ 'LOGIN_FAILURE': isAuthenticated,
|
|
|
+ 'CHECK_TOKEN_SUCCESS': isAuthenticated,
|
|
|
+ 'CHECK_TOKEN_FAILURE': isAuthenticated,
|
|
|
+ 'LOGOUT_REQUEST': isAuthenticated
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
- * @param {*} state
|
|
|
+ * @param {*} username
|
|
|
* @param {*} action
|
|
|
*/
|
|
|
+const setUsername = (username, action) => {
|
|
|
+ return action.payload.username
|
|
|
+}
|
|
|
+
|
|
|
+const userReducer = createReducer(null, {
|
|
|
+ 'LOGIN_SUCCESS': setUsername,
|
|
|
+ 'CHECK_TOKEN_SUCCESS': setUsername
|
|
|
+})
|
|
|
+
|
|
|
export const auth = (state = initialState, action) => {
|
|
|
return {
|
|
|
- isAuthenticated: authenticationCheckReducer(state.isAuthenticated, action)
|
|
|
+ isTokenCheck: tokenCheckReducer(state.isTokenCheck, action),
|
|
|
+ isLogging: logginReducer(state.isLogging, action),
|
|
|
+ isAuthenticated: authenticationReducer(state.isAuthenticated, action),
|
|
|
+ username: userReducer(state.username, action)
|
|
|
}
|
|
|
}
|