329bb4cee5
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | 315 | Création d'une page d'administration : modification/création d'un utilisateur | ## Description de la PR ## Modification du .env ## Check list - [x] Pas de régression - [ ] TU/TI/TF rédigée - [x] TU/TI/TF OK - [x] CHANGELOG modifié Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/17 Reviewed-by: Autin <tristan@yuno.malio.fr> Co-authored-by: kevin <kevin@yuno.malio.fr> Co-committed-by: kevin <kevin@yuno.malio.fr>
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import {defineStore} from 'pinia'
|
|
import type {UserData} from '~/services/dto/user-data'
|
|
import {getCurrentUser, createUser, login, logout} from '~/services/auth'
|
|
import type {UserPayload} from "~/services/dto/user-data";
|
|
import {ROLE} from '~/utils/constants'
|
|
|
|
export const useAuthStore = defineStore('auth', {
|
|
state: () => ({
|
|
user: null as UserData | null,
|
|
isLoading: false,
|
|
checked: false
|
|
}),
|
|
getters: {
|
|
isAuthenticated: (state) => Boolean(state.user),
|
|
isAdmin: (state) => Boolean(state.user?.roles?.includes(ROLE[0].value))
|
|
},
|
|
actions: {
|
|
clearSession() {
|
|
this.user = null
|
|
this.checked = true
|
|
this.isLoading = false
|
|
},
|
|
async ensureSession() {
|
|
if (this.checked) {
|
|
return this.user
|
|
}
|
|
|
|
this.checked = true
|
|
|
|
try {
|
|
const me = await getCurrentUser()
|
|
this.user = me
|
|
return me
|
|
} catch {
|
|
this.user = null
|
|
return null
|
|
}
|
|
},
|
|
async login(username: string, password: string) {
|
|
this.isLoading = true
|
|
|
|
try {
|
|
await login(username, password)
|
|
const me = await getCurrentUser()
|
|
this.user = me
|
|
this.checked = true
|
|
return me
|
|
} finally {
|
|
this.isLoading = false
|
|
}
|
|
},
|
|
async createUser(payload: UserPayload = {}) {
|
|
this.isLoading = true
|
|
const result = await createUser(payload).finally(() => {
|
|
this.isLoading = false
|
|
})
|
|
return result
|
|
},
|
|
async updateUser(id: number, payload: UserPayload) {
|
|
this.isLoading = true
|
|
const result = await createUser(payload).finally(() => {
|
|
this.isLoading = false
|
|
})
|
|
return result
|
|
},
|
|
async logout() {
|
|
this.isLoading = true
|
|
|
|
try {
|
|
await logout()
|
|
} catch {
|
|
// Ignore logout errors so we can still clear local auth state.
|
|
} finally {
|
|
this.user = null
|
|
this.checked = true
|
|
this.isLoading = false
|
|
}
|
|
},
|
|
}
|
|
})
|