import { Action } from "redux"; import { User } from "../../types/user"; import { SET_CURRENT_USER, setCurrentUserAction, LOGOUT } from "../actions/auth"; export interface AuthState { isAuthenticated: boolean currentUser: User } const defaultState = { isAuthenticated: false, currentUser: null, }; export function authReducer(state = defaultState, action: Action): AuthState { switch (action.type) { case SET_CURRENT_USER: return handleSetCurrentUser(state, action as setCurrentUserAction); case LOGOUT: return handleLogout(state); } return state; } function handleSetCurrentUser(state: AuthState, { email }: setCurrentUserAction): AuthState { return { ...state, isAuthenticated: true, currentUser: { email } }; }; function handleLogout(state: AuthState): AuthState { return { ...state, isAuthenticated: false, currentUser: null, }; }