From 580cc680240e89146996ea36fdf2477fe8fa41f2 Mon Sep 17 00:00:00 2001 From: angelo Date: Tue, 15 Oct 2024 04:26:50 +0200 Subject: [PATCH 1/2] docs: Add example for handling authentication with useCookie in Nuxt docs --- docs/3.api/2.composables/use-cookie.md | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/3.api/2.composables/use-cookie.md b/docs/3.api/2.composables/use-cookie.md index af8974ae02..dc0be001c8 100644 --- a/docs/3.api/2.composables/use-cookie.md +++ b/docs/3.api/2.composables/use-cookie.md @@ -231,3 +231,36 @@ export default defineEventHandler(event => { ``` :link-example{to="/docs/examples/advanced/use-cookie"} + +## Handling Authentication with Cookies + +You can manage authentication tokens using cookies like this: + +```ts [plugins/auth.ts] +export default defineNuxtPlugin(() => { + const nuxtApp = useNuxtApp() + const authStore = useAuthStore() + const accessToken = useCookie('accessToken', { + sameSite: 'strict', + }) + + return { + provide: { + getAccessToken() { + return accessToken.value + }, + setAccessToken(accessToken: string) { + accessToken.value = accessToken + }, + loginUser(user: User) { + nuxtApp.$setAccessToken(user.accessToken) + authStore.setUser(user) + }, + logout() { + nuxtApp.$setAccessToken('') + authStore.logout() + }, + }, + } +}) +``` \ No newline at end of file From 1b885c9049f124ee651e7749a89890b5ac56e97a Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 02:38:35 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- docs/3.api/2.composables/use-cookie.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/3.api/2.composables/use-cookie.md b/docs/3.api/2.composables/use-cookie.md index dc0be001c8..ca077b28ae 100644 --- a/docs/3.api/2.composables/use-cookie.md +++ b/docs/3.api/2.composables/use-cookie.md @@ -263,4 +263,4 @@ export default defineNuxtPlugin(() => { }, } }) -``` \ No newline at end of file +```