import axios from 'axios'; import { Storage } from 'react-jhipster'; import { REQUEST, SUCCESS, FAILURE } from './action-type.util'; export const ACTION_TYPES = { LOGIN: 'authentication/LOGIN', GET_SESSION: 'authentication/GET_SESSION', LOGOUT: 'authentication/LOGOUT', CLEAR_AUTH: 'authentication/CLEAR_AUTH', ERROR_MESSAGE: 'authentication/ERROR_MESSAGE' }; const initialState = { loading: false, isAuthenticated: false, loginSuccess: false, account: {}, errorMessage: null, // Errors returned from server side loginError: false, // Errors returned from server side redirectMessage: null, showModalLogin: false }; // Reducer export default (state = initialState, action) => { switch (action.type) { case REQUEST(ACTION_TYPES.LOGIN): case REQUEST(ACTION_TYPES.GET_SESSION): return { ...state, loading: true }; case FAILURE(ACTION_TYPES.LOGIN): return { ...initialState, errorMessage: action.payload, showModalLogin: true, loginError: true }; case FAILURE(ACTION_TYPES.GET_SESSION): return { ...state, loading: false, isAuthenticated: false, showModalLogin: true, errorMessage: action.payload }; case SUCCESS(ACTION_TYPES.LOGIN): return { ...state, loading: false, loginError: false, showModalLogin: false, loginSuccess: true }; case ACTION_TYPES.LOGOUT: return { ...initialState, showModalLogin: true }; case SUCCESS(ACTION_TYPES.GET_SESSION): { const isAuthenticated = action.payload && action.payload.data && action.payload.data.activated; return { ...state, isAuthenticated, loading: false, account: action.payload.data }; } case ACTION_TYPES.ERROR_MESSAGE: return { ...initialState, showModalLogin: true, redirectMessage: action.message }; case ACTION_TYPES.CLEAR_AUTH: return { ...state, loading: false, showModalLogin: true, isAuthenticated: false }; default: return state; } }; export const displayAuthError = message => ({ type: ACTION_TYPES.ERROR_MESSAGE, message }); export const getSession = () => dispatch => dispatch({ type: ACTION_TYPES.GET_SESSION, payload: axios.get('/api/account') }); export const login = (username, password, rememberMe = false) => async (dispatch, getState) => { const result = await dispatch({ type: ACTION_TYPES.LOGIN, payload: axios.post('/api/authenticate', { username, password, rememberMe }) }); const bearerToken = result.value.headers.authorization; if (bearerToken && bearerToken.slice(0, 7) === 'Bearer ') { const jwt = bearerToken.slice(7, bearerToken.length); if (rememberMe) { Storage.local.set('jhi-authenticationToken', jwt); } else { Storage.session.set('jhi-authenticationToken', jwt); } } dispatch(getSession()); }; <%_ if (authenticationType === 'session') { _%> export const logout = () => async dispatch => { await dispatch({ type: ACTION_TYPES.LOGOUT, payload: axios.post('/api/logout', {}) }); dispatch(getSession()); }; <%_ } else { _%> export const clearAuthToken = () => { if (Storage.local.get('jhi-authenticationToken')) { Storage.local.remove('jhi-authenticationToken'); } if (Storage.session.get('jhi-authenticationToken')) { Storage.session.remove('jhi-authenticationToken'); } }; export const logout = () => dispatch => { clearAuthToken(); dispatch({ type: ACTION_TYPES.LOGOUT }); }; <%_ } _%> export const clearAuthentication = messageKey => (dispatch, getState) => { <%_ if (authenticationType !== 'session') { _%> clearAuthToken(); <%_ } _%> dispatch(displayAuthError(messageKey)); dispatch({ type: ACTION_TYPES.CLEAR_AUTH }); };