mirror of
https://github.com/pawelmalak/flame.git
synced 2026-03-10 06:23:11 +08:00
51 lines
923 B
TypeScript
51 lines
923 B
TypeScript
import { Action } from '../actions';
|
|
import { ActionType } from '../action-types';
|
|
|
|
interface AuthState {
|
|
isAuthenticated: boolean;
|
|
token: string | null;
|
|
}
|
|
|
|
const initialState: AuthState = {
|
|
isAuthenticated: false,
|
|
token: null,
|
|
};
|
|
|
|
export const authReducer = (
|
|
state: AuthState = initialState,
|
|
action: Action
|
|
): AuthState => {
|
|
switch (action.type) {
|
|
case ActionType.login:
|
|
return {
|
|
...state,
|
|
token: action.payload,
|
|
isAuthenticated: true,
|
|
};
|
|
|
|
case ActionType.logout:
|
|
return {
|
|
...state,
|
|
token: null,
|
|
isAuthenticated: false,
|
|
};
|
|
|
|
case ActionType.autoLogin:
|
|
return {
|
|
...state,
|
|
token: action.payload,
|
|
isAuthenticated: true,
|
|
};
|
|
|
|
case ActionType.authError:
|
|
return {
|
|
...state,
|
|
token: null,
|
|
isAuthenticated: false,
|
|
};
|
|
|
|
default:
|
|
return state;
|
|
}
|
|
};
|