Stockage du jeton d'authentification dans le store local, permettant l'utilisation de hooks
This commit is contained in:
40
client/utils/features/location/authSlice.ts
Normal file
40
client/utils/features/location/authSlice.ts
Normal file
@ -0,0 +1,40 @@
|
||||
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 | null,
|
||||
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 = true
|
||||
state.name = action.payload.name
|
||||
state.token = action.payload.token
|
||||
},
|
||||
logout: (state) => {
|
||||
state.loggedIn = false
|
||||
state.name = null
|
||||
state.token = null
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const { login, logout } = authSlice.actions
|
||||
|
||||
export default authSlice.reducer
|
Reference in New Issue
Block a user