redux-implicit-oauth2
Version:
OAuth 2.0 Implicit Grant Flow with Redux
48 lines (44 loc) • 1.12 kB
JavaScript
import { hasToken, getToken, getExpiresAt } from './util/token'
import { LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT } from './actions'
const initialState = {
isLoggedIn: hasToken(),
token: getToken(),
expiresAt: getExpiresAt(),
isLoggingIn: false,
error: null
}
const auth = (state = initialState, action) => {
switch (action.type) {
case LOGIN_REQUEST:
return Object.assign({}, state, {
isLoggingIn: true
})
case LOGIN_SUCCESS:
return Object.assign({}, state, {
isLoggedIn: true,
token: action.token,
expiresAt: action.expiresAt,
error: null,
isLoggingIn: false
})
case LOGIN_FAILURE:
return Object.assign({}, state, {
isLoggedIn: false,
token: null,
expiresAt: null,
error: action.error,
isLoggingIn: false
})
case LOGOUT:
return Object.assign({}, state, {
isLoggedIn: false,
token: null,
expiresAt: null,
error: null,
isLoggingIn: false
})
default:
return state
}
}
export default auth