Utilisation de mutations plutôt que d'appels fetch directs
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import * as SecureStore from '@/utils/SecureStore'
|
||||
import { Platform } from 'react-native'
|
||||
|
||||
interface AuthState {
|
||||
loggedIn: boolean,
|
||||
@ -9,6 +10,7 @@ interface AuthState {
|
||||
|
||||
export interface AuthPayload {
|
||||
name: string,
|
||||
password?: string | null,
|
||||
token: string | null,
|
||||
}
|
||||
|
||||
@ -26,12 +28,28 @@ export const authSlice = createSlice({
|
||||
state.loggedIn = action.payload.token !== null
|
||||
state.name = action.payload.name
|
||||
state.token = action.payload.token
|
||||
console.log(state)
|
||||
|
||||
SecureStore.setItem('apiName', action.payload.name)
|
||||
if (action.payload.password !== undefined && Platform.OS !== "web") {
|
||||
// Le stockage navigateur n'est pas sûr, on évite de stocker un mot de passe à l'intérieur
|
||||
if (action.payload.password)
|
||||
SecureStore.setItem('apiPassword', action.payload.password)
|
||||
else
|
||||
SecureStore.deleteItemAsync('apiPassword')
|
||||
}
|
||||
if (action.payload.token)
|
||||
SecureStore.setItem('apiToken', action.payload.token)
|
||||
else
|
||||
SecureStore.deleteItemAsync('apiToken')
|
||||
},
|
||||
logout: (state) => {
|
||||
state.loggedIn = false
|
||||
state.name = null
|
||||
state.token = null
|
||||
|
||||
SecureStore.deleteItemAsync('apiName')
|
||||
SecureStore.deleteItemAsync('apiPassword')
|
||||
SecureStore.deleteItemAsync('apiToken')
|
||||
}
|
||||
},
|
||||
})
|
||||
|
Reference in New Issue
Block a user