| 1 |
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; |
| 2 |
|
| 3 |
interface onLoginPayload { |
| 4 |
loggedInUser: string; |
| 5 |
error: string | null; |
| 6 |
} |
| 7 |
|
| 8 |
interface AuthState { |
| 9 |
isUserLoggedIn: boolean; |
| 10 |
user: string | null; |
| 11 |
error: string | null; |
| 12 |
} |
| 13 |
|
| 14 |
const initialState: AuthState = { |
| 15 |
isUserLoggedIn: false, |
| 16 |
user: null, |
| 17 |
error: null, |
| 18 |
}; |
| 19 |
|
| 20 |
const authSlice = createSlice({ |
| 21 |
name: "auth", |
| 22 |
initialState, |
| 23 |
reducers: { |
| 24 |
setIsLogin: (state, action: PayloadAction<onLoginPayload>) => { |
| 25 |
state.isUserLoggedIn = true; |
| 26 |
state.user = action.payload.loggedInUser; |
| 27 |
state.error = action.payload.error; |
| 28 |
}, |
| 29 |
setIsLogout: (state) => { |
| 30 |
state.isUserLoggedIn = false; |
| 31 |
state.user = null; |
| 32 |
state.error = null; |
| 33 |
}, |
| 34 |
}, |
| 35 |
}); |
| 36 |
|
| 37 |
export const { setIsLogin, setIsLogout } = authSlice.actions; |
| 38 |
export default authSlice.reducer; |