From a332d1bbc53d02b4b56da693db194da16fcc7325 Mon Sep 17 00:00:00 2001 From: Guamss Date: Sun, 7 Dec 2025 15:13:46 +0100 Subject: [PATCH] =?UTF-8?q?d=C3=A9but=20du=20management=20de=20l'authentif?= =?UTF-8?q?ication?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 10 +++ package.json | 1 + src/App.vue | 113 ++++++++++++++++++++++++- src/components/Authenticate.vue | 65 ++++++++------ src/components/Home.vue | 3 +- src/components/NotFound.vue | 12 +++ src/composables/useAuth.ts | 24 ++++++ src/models/user.ts | 6 ++ src/routes.ts | 36 +++++--- src/services/authentication.service.ts | 15 +++- 10 files changed, 236 insertions(+), 49 deletions(-) create mode 100644 src/components/NotFound.vue create mode 100644 src/composables/useAuth.ts diff --git a/package-lock.json b/package-lock.json index 0b885c6..e008faa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.0", "dependencies": { "axios": "^1.13.2", + "jwt-decode": "^4.0.0", "vue": "^3.5.17", "vue-router": "^4.6.3", "vue-toast-notification": "^3.1.3" @@ -3822,6 +3823,15 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jwt-decode": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", + "integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", diff --git a/package.json b/package.json index c9eafee..ad64f64 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "axios": "^1.13.2", + "jwt-decode": "^4.0.0", "vue": "^3.5.17", "vue-router": "^4.6.3", "vue-toast-notification": "^3.1.3" diff --git a/src/App.vue b/src/App.vue index c11901d..84f6b11 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,14 +1,119 @@ diff --git a/src/components/Authenticate.vue b/src/components/Authenticate.vue index 81efd82..c00221f 100644 --- a/src/components/Authenticate.vue +++ b/src/components/Authenticate.vue @@ -3,48 +3,57 @@ import { AuthenticationService } from '@/services/authentication.service'; import { ref } from 'vue'; import { useRouter } from 'vue-router'; import { useToast } from 'vue-toast-notification'; +import { useAuth } from '@/composables/useAuth'; -const username = ref('') -const password = ref('') - -const usernameErrorDetail = ref('') -const passwordErrorDetail = ref('') -const generalErrorDetail = ref('') +const username = ref(''); +const password = ref(''); +const usernameErrorDetail = ref(''); +const passwordErrorDetail = ref(''); +const generalErrorDetail = ref(''); const toastService = useToast(); -const router = useRouter() -const authenticationService = new AuthenticationService() +const router = useRouter(); +const authenticationService = new AuthenticationService(); + +const { login } = useAuth(); async function submit() { try { - await authenticationService.authenticate(username.value, password.value) + const responseData = await authenticationService.authenticate(username.value, password.value); + + login(responseData.access, responseData.refresh); + toastService.success(`Heureux de vous revoir ${username.value}!`, { - position: 'top-right', + position: 'bottom-right', pauseOnHover: true, dismissible: true, - }) + }); + router.push('/'); } catch (error: any) { usernameErrorDetail.value = '' passwordErrorDetail.value = '' generalErrorDetail.value = '' - const errorCode = error.status - const errorDetails = error.response.data + + if (error.response) { + const errorCode = error.status || error.response.status; + const errorDetails = error.response.data; - if (errorCode === 400) { - if (errorDetails.username) { - usernameErrorDetail.value = errorDetails.username[0] // le champ username est vide - } - if (errorDetails.password) { - passwordErrorDetail.value = errorDetails.password[0] // le champ password est vide - } - } else { - generalErrorDetail.value = errorDetails.detail - toastService.error(`${generalErrorDetail.value}`, { - position: 'top-right', - pauseOnHover: true, - dismissible: true, - }) + if (errorCode === 400) { + if (errorDetails.username) { + usernameErrorDetail.value = errorDetails.username[0] + } + if (errorDetails.password) { + passwordErrorDetail.value = errorDetails.password[0] + } + } else { + generalErrorDetail.value = errorDetails.detail || 'Erreur inconnue' + toastService.error(`${generalErrorDetail.value}`, { + position: 'top-right', + pauseOnHover: true, + dismissible: true, + }) + } } } } @@ -55,7 +64,7 @@ async function submit() {

Connexion

- +
  • {{ usernameErrorDetail }}
diff --git a/src/components/Home.vue b/src/components/Home.vue index 88fe4a3..736fb6f 100644 --- a/src/components/Home.vue +++ b/src/components/Home.vue @@ -15,12 +15,11 @@ onMounted(async () => {