traintrape-moi/client/utils/features/location/authSlice.ts

42 lines
1.0 KiB
TypeScript

import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import * as SecureStore from '@/utils/SecureStore'
interface AuthState {
loggedIn: boolean,
name: string | null,
token: string | null,
}
export interface AuthPayload {
name: string,
token: string | null,
}
const initialState: AuthState = {
loggedIn: SecureStore.getItem('apiName') !== null && SecureStore.getItem('apiToken') !== null,
name: SecureStore.getItem('apiName'),
token: SecureStore.getItem('apiToken'),
}
export const authSlice = createSlice({
name: 'auth',
initialState: initialState,
reducers: {
login: (state, action: PayloadAction<AuthPayload>) => {
state.loggedIn = action.payload.token !== null
state.name = action.payload.name
state.token = action.payload.token
console.log(state)
},
logout: (state) => {
state.loggedIn = false
state.name = null
state.token = null
}
},
})
export const { login, logout } = authSlice.actions
export default authSlice.reducer