initial commit
This commit is contained in:
51
app/plugins/api.ts
Normal file
51
app/plugins/api.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import ListsRepository from '~/repositories/lists.repository';
|
||||
import UserRepository from '~/repositories/user.repository';
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
const config = useRuntimeConfig();
|
||||
const authStore = useAuthStore();
|
||||
|
||||
const apiFetcher = $fetch.create({
|
||||
baseURL: config.public.apiBase,
|
||||
|
||||
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);
|
||||
}
|
||||
options.headers = headers;
|
||||
},
|
||||
|
||||
async onResponseError({ response }) {
|
||||
// Cas 401 : Session expirée ou non autorisée
|
||||
if (response.status === 401) {
|
||||
// Optionnel : nettoyer le store avant d'afficher l'erreur
|
||||
authStore.logout();
|
||||
|
||||
// On déclenche la page d'erreur Nuxt
|
||||
throw showError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Session expirée ou accès non autorisé',
|
||||
fatal: true // 'fatal: true' force le rendu de la page d'erreur même côté client
|
||||
});
|
||||
}
|
||||
|
||||
// Optionnel : Gérer d'autres codes
|
||||
if (response.status >= 500) {
|
||||
console.error("Erreur serveur, réessayez plus tard.");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
provide: {
|
||||
api: {
|
||||
lists: new ListsRepository(apiFetcher as any),
|
||||
user: new UserRepository(apiFetcher as any)
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
30
app/plugins/auth.ts
Normal file
30
app/plugins/auth.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
// Faire vérifier le token présent dans le cookie
|
||||
// à WP via Pinia au 1er chargement de l'app.
|
||||
|
||||
import type { User } from "~/types/auth.ts"
|
||||
export default defineNuxtPlugin(async () => {
|
||||
const auth = useAuthStore()
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
// On n'exécute la vérification que s'il y a un token
|
||||
// et qu'on n'a pas encore chargé l'utilisateur (ex: au refresh)
|
||||
if (auth.token && !auth.user) {
|
||||
try {
|
||||
// On appelle le endpoint WP qui renvoie l'utilisateur connecté
|
||||
// Note: l'URL dépend du plugin perso auth
|
||||
const data = await $fetch<User>(`${config.public.apiBase}/users/me`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${auth.token}`
|
||||
}
|
||||
})
|
||||
|
||||
// Si ça marche, on met à jour l'user dans Pinia
|
||||
auth.user = data
|
||||
} catch (error) {
|
||||
// Si le token est expiré ou invalide, WP renvoie une erreur
|
||||
// On vide tout pour forcer la reconnexion
|
||||
console.error("Session expirée ou token invalide")
|
||||
auth.logout()
|
||||
}
|
||||
}
|
||||
})
|
||||
34
app/plugins/fontawesome.ts
Normal file
34
app/plugins/fontawesome.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
// Font awesome
|
||||
/* import the fontawesome core */
|
||||
import { library, config } from '@fortawesome/fontawesome-svg-core'
|
||||
/* import font awesome icon component */
|
||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
|
||||
|
||||
// import icons //
|
||||
import { faEye, faEyeSlash, faLessThan, faGreaterThan, faPlus, faMinus, faLanguage, faGear, faLocationDot, faArrowsRotate, faDiamondTurnRight} from '@fortawesome/free-solid-svg-icons'
|
||||
import { faCircleCheck, faCircleXmark, faSquare, faSquareCheck, faTrashCan} from '@fortawesome/free-regular-svg-icons'
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
library.add(faCircleCheck,
|
||||
faCircleXmark,
|
||||
faEye,
|
||||
faEyeSlash,
|
||||
faSquare,
|
||||
faSquareCheck,
|
||||
faTrashCan,
|
||||
faLessThan,
|
||||
faGreaterThan,
|
||||
faPlus,
|
||||
faMinus,
|
||||
faGear,
|
||||
faLocationDot,
|
||||
faArrowsRotate,
|
||||
faDiamondTurnRight,
|
||||
faLanguage)
|
||||
nuxtApp.vueApp.component('FontAwesomeIcon', FontAwesomeIcon)
|
||||
})
|
||||
|
||||
import '@fortawesome/fontawesome-svg-core/styles.css'
|
||||
|
||||
// Empêche FontAwesome d’injecter son CSS automatiquement
|
||||
config.autoAddCss = false
|
||||
Reference in New Issue
Block a user