crap
This commit is contained in:
34
src/features/authSlice.ts
Normal file
34
src/features/authSlice.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import {createSlice, PayloadAction} from "@reduxjs/toolkit";
|
||||
|
||||
interface AuthState {
|
||||
isAuthorized: boolean;
|
||||
accessToken: string;
|
||||
}
|
||||
|
||||
const initialState = (): AuthState => {
|
||||
const localStorageState = localStorage.getItem("authState");
|
||||
if (localStorageState !== null) {
|
||||
return JSON.parse(localStorageState);
|
||||
}
|
||||
return {
|
||||
accessToken: "",
|
||||
isAuthorized: false
|
||||
}
|
||||
}
|
||||
|
||||
const authSlice = createSlice({
|
||||
name: 'auth',
|
||||
initialState,
|
||||
reducers: {
|
||||
login: (state, action: PayloadAction<{ accessToken: string }>) => {
|
||||
state.accessToken = action.payload.accessToken;
|
||||
state.isAuthorized = true;
|
||||
},
|
||||
logout: (state) => {
|
||||
state.isAuthorized = false;
|
||||
state.accessToken = '';
|
||||
}
|
||||
}
|
||||
})
|
||||
export const {login, logout} = authSlice.actions;
|
||||
export default authSlice.reducer;
|
||||
Reference in New Issue
Block a user