Created
September 20, 2021 07:51
-
-
Save akursat/d9a4dcb71a71dbf03f94b62be7bb99c2 to your computer and use it in GitHub Desktop.
redux toolkit authslice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const slice = createSlice({ | |
name: 'auth', | |
initialState: { user: null, token: null, isAuthenticated: false } as AuthState, | |
reducers: { | |
logout: (state) => { | |
state.user = null | |
state.token = null | |
state.isAuthenticated = false | |
}, | |
}, | |
extraReducers: (builder) => { | |
builder.addMatcher( | |
api.endpoints.login.matchFulfilled, | |
(state, { payload: { user, token } }) => { | |
state.user = user | |
state.token = token | |
state.isAuthenticated = true | |
}, | |
) | |
}, | |
}) | |
export const { logout } = slice.actions | |
export default slice.reducer | |
export const selectCurrentUser = (state: RootState) => state.auth.user | |
export const selectIsAuthenticated = (state: RootState) => state.auth.isAuthenticated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment