Skip to content
Snippets Groups Projects
AuthenticationReducer.js 1.58 KiB
Newer Older
Orlando Piñero's avatar
Orlando Piñero committed
import * as authenticationActions from './AuthenticationActions'

const initialState = {
    user: null,
    accessToken: null,
    showLoginDialog: false,
    isAdmin: false
};

function AuthenticationReducer(state = initialState, action) {
Orlando Piñero's avatar
Orlando Piñero committed

    switch (action.type) {
        case authenticationActions.SHOW_LOGIN_DIALOG:
            return {
                ...state,
                showLoginDialog: true,
                error: null
            }
        case authenticationActions.HIDE_LOGIN_DIALOG:
            return {
                ...state,
                showLoginDialog: false,
                error: null
            }
        case authenticationActions.AUTHENTICATION_PENDING:
            return {
                ...state,
                pending: true,
                error: null
            }
        case authenticationActions.AUTHENTICATION_SUCCESS:
            return {
                ...state,
                showLoginDialog: false,
                pending: false,
                user: action.user,
                accessToken: action.accessToken,
                isAdmin: action.isAdmin
            }
        case authenticationActions.AUTHENTICATION_ERROR:
            return {
                ...state,
                pending: false,
                error: 'Authentication failed'
            }
        case authenticationActions.LOGOUT:
            return {
                ...state,
                user: null,
Orlando Piñero's avatar
Orlando Piñero committed
                accessToken: null,
                isAdmin: false
            }
        default:
            return state;

    }
}

export default AuthenticationReducer;