first steps
This commit is contained in:
@@ -80,7 +80,7 @@ const handleDriveKey = async (accessToken: string) => {
|
|||||||
// // A. Vérification côté WordPress (Login)
|
// // A. Vérification côté WordPress (Login)
|
||||||
// // Note : On envoie l'accessToken ou on utilise l'ID Token si configuré.
|
// // 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.
|
// // 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',
|
method: 'POST',
|
||||||
body: { access_token: accessToken }
|
body: { access_token: accessToken }
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -21,14 +21,13 @@ const handleFormSubmit = async() => {
|
|||||||
errors.value.loginFailed = false;
|
errors.value.loginFailed = false;
|
||||||
errors.value.unconfirmedUser = false;
|
errors.value.unconfirmedUser = false;
|
||||||
awaiting.value = true
|
awaiting.value = true
|
||||||
|
|
||||||
// --- Vérification des données du formulaire --- //
|
// --- Vérification des données du formulaire --- //
|
||||||
|
|
||||||
// En version raccourcie : on stocke le résultat de la condition dans la variable
|
// En version raccourcie : on stocke le résultat de la condition dans la variable
|
||||||
|
|
||||||
errors.value.loginEmpty = ( login.value == "" );
|
errors.value.loginEmpty = ( login.value == "" );
|
||||||
errors.value.passwordEmpty = ( password.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 )
|
if( !errors.value.loginEmpty && !errors.value.passwordEmpty )
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,7 @@ export default defineNuxtPlugin(() => {
|
|||||||
|
|
||||||
onRequest({ options }) {
|
onRequest({ options }) {
|
||||||
const headers = new Headers(options.headers);
|
const headers = new Headers(options.headers);
|
||||||
if (authStore.token) {
|
|
||||||
headers.set('Authorization', `Bearer ${authStore.token}`);
|
|
||||||
}
|
|
||||||
if (authStore.user?.sudo_token){
|
if (authStore.user?.sudo_token){
|
||||||
headers.set('sudo_token', authStore.user?.sudo_token);
|
headers.set('sudo_token', authStore.user?.sudo_token);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { $Fetch } from 'ofetch';
|
import type { $Fetch } from 'ofetch';
|
||||||
import type { User, ConfirmResult } from '~/types/auth'
|
import type { User, ConfirmResult, LoginResponse } from '~/types/auth'
|
||||||
|
|
||||||
export default class UserRepository {
|
export default class UserRepository {
|
||||||
private fetcher: $Fetch;
|
private fetcher: $Fetch;
|
||||||
@@ -9,7 +9,7 @@ export default class UserRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async confirm(token: string){
|
async confirm(token: string){
|
||||||
return await this.fetcher<ConfirmResult>('/auth/confirm', {
|
return await this.fetcher<ConfirmResult>('/user/confirm', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: {
|
body: {
|
||||||
token
|
token
|
||||||
@@ -17,11 +17,11 @@ export default class UserRepository {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async register(email: string, password:string, locale:string){
|
async register(email: string, password:string){
|
||||||
return await this.fetcher<ConfirmResult>('/user/register', {
|
return await this.fetcher<LoginResponse>('/auth/register', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body:{
|
body:{
|
||||||
email, password, locale
|
email, password
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,6 @@ import type { User, LoginResponse, ConfirmResult } from '~/types/auth'
|
|||||||
export const useAuthStore = defineStore('auth', {
|
export const useAuthStore = defineStore('auth', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
// On lie l'état directement au cookie
|
// 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', {
|
user: useCookie<User | null>('auth_user', {
|
||||||
// secure: false,
|
// secure: false,
|
||||||
// sameSite: 'lax',
|
// sameSite: 'lax',
|
||||||
@@ -24,18 +18,13 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
|
|
||||||
getters: {
|
getters: {
|
||||||
// Le "!!" transforme la valeur en vrai BOULÉEN (true/false)
|
// 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
|
// plus tard on pourras ajouter un getter pour récupérer le prénom
|
||||||
//userName: (state) => state.user?.name || 'Invité'
|
//userName: (state) => state.user?.name || 'Invité'
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
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) {
|
setUserCookie(newUser: User | null) {
|
||||||
// 1. On récupère le cookie sans typage strict ou typé en 'any' pour l'écriture
|
// 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
|
this.user = newUser
|
||||||
},
|
},
|
||||||
|
|
||||||
async register(email: string, password:string, locale:string ){
|
async register(email: string, password:string){
|
||||||
const { $api } = useNuxtApp();
|
const { $api } = useNuxtApp();
|
||||||
const listsStore = useListStore()
|
const listsStore = useListStore()
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
@@ -59,18 +48,16 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
this.error = null
|
this.error = null
|
||||||
|
|
||||||
try {
|
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é.
|
// En cas de réussite, le nouveau user est connecté.
|
||||||
// On assigne les valeurs : useCookie met à jour le state ET le navigateur
|
// On assigne les valeurs : useCookie met à jour le state ET le navigateur
|
||||||
this.setTokenCookie(data.token)
|
|
||||||
this.setUserCookie(data.user)
|
this.setUserCookie(data.user)
|
||||||
listsStore.saveLists(data.lists)
|
listsStore.saveLists(data.lists)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// En cas d'erreur, on nettoie les cookies
|
// En cas d'erreur, on nettoie les cookies
|
||||||
this.token = null
|
|
||||||
this.user = null
|
this.user = null
|
||||||
this.error = err.data?.message || "Erreur de connexion"
|
this.error = err.data?.message || "Erreur de connexion"
|
||||||
return false
|
return false
|
||||||
@@ -104,14 +91,12 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// On assigne les valeurs : useCookie met à jour le state ET le navigateur
|
// On assigne les valeurs : useCookie met à jour le state ET le navigateur
|
||||||
this.setTokenCookie(data.token)
|
|
||||||
this.setUserCookie(data.user)
|
this.setUserCookie(data.user)
|
||||||
listsStore.saveLists(data.lists)
|
listsStore.saveLists(data.lists)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// En cas d'erreur, on nettoie les cookies
|
// En cas d'erreur, on nettoie les cookies
|
||||||
this.token = null
|
|
||||||
this.user = null
|
this.user = null
|
||||||
this.error = err.data?.message || "Erreur de connexion"
|
this.error = err.data?.message || "Erreur de connexion"
|
||||||
return false
|
return false
|
||||||
@@ -121,11 +106,11 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
this.setTokenCookie(null)
|
|
||||||
this.setUserCookie(null)
|
this.setUserCookie(null)
|
||||||
const listsStore = useListStore()
|
const listsStore = useListStore()
|
||||||
listsStore.resetLists()
|
listsStore.resetLists()
|
||||||
return navigateTo('/')
|
return navigateTo('/')
|
||||||
|
// ajouter le logout depuis le BO (invalidation de token etc.)
|
||||||
},
|
},
|
||||||
|
|
||||||
async pwdResetResquest( email: string, locale: string) {
|
async pwdResetResquest( email: string, locale: string) {
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
export interface User {
|
export interface User {
|
||||||
id: number
|
id: number
|
||||||
username: string
|
//username: string
|
||||||
email: string
|
email: string
|
||||||
confirmed: boolean
|
isConfirmed: boolean
|
||||||
role: string
|
//role: string
|
||||||
display_name: string
|
display_name: string
|
||||||
avatar: string
|
avatar: string
|
||||||
is_google: boolean
|
googleId: boolean
|
||||||
sudo_token:string | null
|
sudo_token:string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LoginResponse {
|
export interface LoginResponse {
|
||||||
user: User
|
user: User
|
||||||
token: string
|
|
||||||
lists: any[]
|
lists: any[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ export default defineNuxtConfig({
|
|||||||
devtools: { enabled: true },
|
devtools: { enabled: true },
|
||||||
runtimeConfig: {
|
runtimeConfig: {
|
||||||
public: {
|
public: {
|
||||||
apiBase: 'http://localhost:81/wp-json/app/v1'
|
//apiBase: 'http://localhost:81/wp-json/app/v1'
|
||||||
|
apiBase: '/api-fastify'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modules: ['@pinia/nuxt',
|
modules: ['@pinia/nuxt',
|
||||||
@@ -43,7 +44,9 @@ export default defineNuxtConfig({
|
|||||||
},
|
},
|
||||||
css: ['@/assets/css/main.scss'],
|
css: ['@/assets/css/main.scss'],
|
||||||
routeRules: {
|
routeRules: {
|
||||||
'/api-wp/**': { proxy: 'http://localhost:81/wp-json/app/v1/**',
|
'/api-fastify/**': {
|
||||||
|
proxy: 'http://localhost:1234/api/**',
|
||||||
|
//'/api-wp/**': { proxy: 'http://localhost:81/wp-json/app/v1/**',
|
||||||
changeOrigin: true, // Crucial pour éviter la 502
|
changeOrigin: true, // Crucial pour éviter la 502
|
||||||
prependPath: true
|
prependPath: true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user