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