first steps
This commit is contained in:
@@ -80,7 +80,7 @@ const handleDriveKey = async (accessToken: string) => {
|
||||
// // A. Vérification côté WordPress (Login)
|
||||
// // Note : On envoie l'accessToken ou on utilise l'ID Token si configuré.
|
||||
// // Si ton API WP attendait un 'code', il faudra l'adapter pour vérifier l'access_token ou l'email.
|
||||
const data = await $fetch<any>('/api-wp/auth/google', {
|
||||
const data = await $fetch<any>('/api-fastify/auth/google', {
|
||||
method: 'POST',
|
||||
body: { access_token: accessToken }
|
||||
});
|
||||
|
||||
@@ -21,14 +21,13 @@ const handleFormSubmit = async() => {
|
||||
errors.value.loginFailed = false;
|
||||
errors.value.unconfirmedUser = false;
|
||||
awaiting.value = true
|
||||
|
||||
// --- Vérification des données du formulaire --- //
|
||||
|
||||
// En version raccourcie : on stocke le résultat de la condition dans la variable
|
||||
|
||||
errors.value.loginEmpty = ( login.value == "" );
|
||||
errors.value.passwordEmpty = ( password.value == "" );
|
||||
|
||||
// Si une erreur est rencontrée, on n'envoi pas la requete au serveur !
|
||||
// Si une erreur est rencontrée, on n'envoi pas la requete au serveur
|
||||
if( !errors.value.loginEmpty && !errors.value.passwordEmpty )
|
||||
{
|
||||
|
||||
|
||||
@@ -10,9 +10,7 @@ export default defineNuxtPlugin(() => {
|
||||
|
||||
onRequest({ options }) {
|
||||
const headers = new Headers(options.headers);
|
||||
if (authStore.token) {
|
||||
headers.set('Authorization', `Bearer ${authStore.token}`);
|
||||
}
|
||||
|
||||
if (authStore.user?.sudo_token){
|
||||
headers.set('sudo_token', authStore.user?.sudo_token);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { $Fetch } from 'ofetch';
|
||||
import type { User, ConfirmResult } from '~/types/auth'
|
||||
import type { User, ConfirmResult, LoginResponse } from '~/types/auth'
|
||||
|
||||
export default class UserRepository {
|
||||
private fetcher: $Fetch;
|
||||
@@ -9,7 +9,7 @@ export default class UserRepository {
|
||||
}
|
||||
|
||||
async confirm(token: string){
|
||||
return await this.fetcher<ConfirmResult>('/auth/confirm', {
|
||||
return await this.fetcher<ConfirmResult>('/user/confirm', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
token
|
||||
@@ -17,11 +17,11 @@ export default class UserRepository {
|
||||
})
|
||||
}
|
||||
|
||||
async register(email: string, password:string, locale:string){
|
||||
return await this.fetcher<ConfirmResult>('/user/register', {
|
||||
async register(email: string, password:string){
|
||||
return await this.fetcher<LoginResponse>('/auth/register', {
|
||||
method: 'POST',
|
||||
body:{
|
||||
email, password, locale
|
||||
email, password
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,12 +6,6 @@ import type { User, LoginResponse, ConfirmResult } from '~/types/auth'
|
||||
export const useAuthStore = defineStore('auth', {
|
||||
state: () => ({
|
||||
// On lie l'état directement au cookie
|
||||
token: useCookie<string | null>('auth_token', {
|
||||
// secure: false,
|
||||
// sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 60 * 60 * 24 * 7
|
||||
}), // Expire après 7 jours
|
||||
user: useCookie<User | null>('auth_user', {
|
||||
// secure: false,
|
||||
// sameSite: 'lax',
|
||||
@@ -24,18 +18,13 @@ export const useAuthStore = defineStore('auth', {
|
||||
|
||||
getters: {
|
||||
// Le "!!" transforme la valeur en vrai BOULÉEN (true/false)
|
||||
isLoggedIn: (state) => !!state.token,
|
||||
isLoggedIn: (state) => !!state.user,
|
||||
|
||||
// plus tard on pourras ajouter un getter pour récupérer le prénom
|
||||
//userName: (state) => state.user?.name || 'Invité'
|
||||
},
|
||||
|
||||
actions: {
|
||||
setTokenCookie(newToken: string | null) {
|
||||
const cookie = useCookie('auth_token')
|
||||
cookie.value = newToken // On force l'écriture
|
||||
this.token = newToken // On met à jour le state Pinia
|
||||
},
|
||||
|
||||
setUserCookie(newUser: User | null) {
|
||||
// 1. On récupère le cookie sans typage strict ou typé en 'any' pour l'écriture
|
||||
@@ -51,7 +40,7 @@ export const useAuthStore = defineStore('auth', {
|
||||
this.user = newUser
|
||||
},
|
||||
|
||||
async register(email: string, password:string, locale:string ){
|
||||
async register(email: string, password:string){
|
||||
const { $api } = useNuxtApp();
|
||||
const listsStore = useListStore()
|
||||
const config = useRuntimeConfig()
|
||||
@@ -59,18 +48,16 @@ export const useAuthStore = defineStore('auth', {
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
const data = await $api.user.register(email, password, locale)
|
||||
const data = await $api.user.register(email, password)
|
||||
|
||||
// En cas de réussite, le nouveau user est connecté.
|
||||
// On assigne les valeurs : useCookie met à jour le state ET le navigateur
|
||||
this.setTokenCookie(data.token)
|
||||
this.setUserCookie(data.user)
|
||||
listsStore.saveLists(data.lists)
|
||||
|
||||
return true
|
||||
} catch (err: any) {
|
||||
// En cas d'erreur, on nettoie les cookies
|
||||
this.token = null
|
||||
this.user = null
|
||||
this.error = err.data?.message || "Erreur de connexion"
|
||||
return false
|
||||
@@ -104,14 +91,12 @@ export const useAuthStore = defineStore('auth', {
|
||||
})
|
||||
|
||||
// On assigne les valeurs : useCookie met à jour le state ET le navigateur
|
||||
this.setTokenCookie(data.token)
|
||||
this.setUserCookie(data.user)
|
||||
listsStore.saveLists(data.lists)
|
||||
|
||||
return true
|
||||
} catch (err: any) {
|
||||
// En cas d'erreur, on nettoie les cookies
|
||||
this.token = null
|
||||
this.user = null
|
||||
this.error = err.data?.message || "Erreur de connexion"
|
||||
return false
|
||||
@@ -121,11 +106,11 @@ export const useAuthStore = defineStore('auth', {
|
||||
},
|
||||
|
||||
logout() {
|
||||
this.setTokenCookie(null)
|
||||
this.setUserCookie(null)
|
||||
const listsStore = useListStore()
|
||||
listsStore.resetLists()
|
||||
return navigateTo('/')
|
||||
// ajouter le logout depuis le BO (invalidation de token etc.)
|
||||
},
|
||||
|
||||
async pwdResetResquest( email: string, locale: string) {
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
export interface User {
|
||||
id: number
|
||||
username: string
|
||||
//username: string
|
||||
email: string
|
||||
confirmed: boolean
|
||||
role: string
|
||||
isConfirmed: boolean
|
||||
//role: string
|
||||
display_name: string
|
||||
avatar: string
|
||||
is_google: boolean
|
||||
googleId: boolean
|
||||
sudo_token:string | null
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
user: User
|
||||
token: string
|
||||
lists: any[]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user