Revize 06808454
Přidáno uživatelem Schwobik před téměř 2 roky(ů)
src/stores/reducers/userSlice.ts | ||
---|---|---|
1 | 1 |
import { createSlice } from "@reduxjs/toolkit" |
2 |
import { checkAuth, login } from "../actions/userThunks" |
|
2 |
import { checkAuth, login, logout } from "../actions/userThunks"
|
|
3 | 3 |
|
4 | 4 |
export interface UserState { |
5 | 5 |
username: string |
6 | 6 |
loggedIn: boolean |
7 | 7 |
role: string |
8 | 8 |
lastError?: string |
9 |
checkedAuth: boolean |
|
9 | 10 |
} |
10 | 11 |
|
11 | 12 |
const initialState: UserState = { |
12 | 13 |
username: "", |
13 | 14 |
loggedIn: false, |
14 |
role: "" |
|
15 |
role: "", |
|
16 |
checkedAuth: false |
|
15 | 17 |
} |
16 | 18 |
|
17 | 19 |
export const userSlice = createSlice({ |
18 | 20 |
name: "user", |
19 | 21 |
initialState: initialState, |
20 | 22 |
reducers: { |
21 |
logout: () => initialState, |
|
23 |
resetState: (state) => { |
|
24 |
state = initialState |
|
25 |
} |
|
22 | 26 |
}, |
23 | 27 |
extraReducers: (builder) => { |
24 | 28 |
builder.addCase(login.fulfilled, (state, action) => { |
... | ... | |
33 | 37 |
builder.addCase(checkAuth.fulfilled, (state, action) => { |
34 | 38 |
state.loggedIn = action.payload.isLogged |
35 | 39 |
state.lastError = "" |
40 |
state.checkedAuth = true |
|
36 | 41 |
}) |
37 | 42 |
builder.addCase(checkAuth.rejected, (state, action) => { |
38 | 43 |
state.lastError = action.error.message |
44 |
state.checkedAuth = true |
|
39 | 45 |
}) |
46 |
builder.addCase(logout.fulfilled, (state, action) => initialState) |
|
40 | 47 |
} |
41 | 48 |
}) |
42 | 49 |
|
43 |
export const { logout } = userSlice.actions
|
|
50 |
export const { resetState } = userSlice.actions
|
|
44 | 51 |
|
45 | 52 |
export default userSlice.reducer |
Také k dispozici: Unified diff
Logout and Login fixed
re #10715