mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
Compare commits
5 Commits
982c766cb4
...
c01dc95b94
Author | SHA1 | Date | |
---|---|---|---|
|
c01dc95b94 | ||
|
b7f0369e79 | ||
|
35e8572b63 | ||
|
1b885c9049 | ||
|
580cc68024 |
@ -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()
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user