feat : ajout de l'authentification avec lexik
This commit is contained in:
38
frontend/services/auth.ts
Normal file
38
frontend/services/auth.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useApi } from '~/composables/useApi'
|
||||
import type { UserData } from '~/services/dto/user-data'
|
||||
|
||||
export async function getUsers() {
|
||||
const api = useApi()
|
||||
const data = await api.get<UserData[] | { 'hydra:member': UserData[] }>('users', {}, {
|
||||
toastErrorKey: 'errors.auth.users'
|
||||
})
|
||||
if (Array.isArray(data)) {
|
||||
return data
|
||||
}
|
||||
|
||||
return data['hydra:member'] ?? []
|
||||
}
|
||||
|
||||
export async function getCurrentUser() {
|
||||
const api = useApi()
|
||||
return api.get<UserData>('me', {}, {
|
||||
toast: false
|
||||
})
|
||||
}
|
||||
|
||||
export async function login(username: string, password: string) {
|
||||
const api = useApi()
|
||||
return api.post<{ token: string }>('login_check', { username, password }, {
|
||||
toastErrorKey: 'errors.auth.login',
|
||||
toastSuccessKey: 'success.auth.login'
|
||||
})
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
const api = useApi()
|
||||
return api.post<void>('logout', {}, {
|
||||
toastErrorKey: 'errors.auth.logout',
|
||||
toastSuccessKey: 'success.auth.logout',
|
||||
redirect: 'manual'
|
||||
})
|
||||
}
|
||||
3
frontend/services/dto/user-data.ts
Normal file
3
frontend/services/dto/user-data.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface UserData {
|
||||
username: string
|
||||
}
|
||||
Reference in New Issue
Block a user