-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add example for handling authentication with useCookie in Nuxt docs #29538
base: main
Are you sure you want to change the base?
Changes from 1 commit
580cc68
1b885c9
35e8572
b7f0369
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…docs
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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', | ||||||||||||||||||||||
}) | ||||||||||||||||||||||
Comment on lines
+243
to
+245
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strengthen cookie security configuration. The cookie configuration is missing important security options that are crucial for storing authentication tokens. const accessToken = useCookie('accessToken', {
sameSite: 'strict',
+ httpOnly: true, // Prevent XSS attacks from accessing the token
+ secure: process.env.NODE_ENV === 'production', // Require HTTPS in production
+ maxAge: 7200, // Token expiration in seconds
+ path: '/', // Cookie accessibility path
}) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
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() | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
} | ||||||||||||||||||||||
}) | ||||||||||||||||||||||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance the authentication section with security considerations and best practices.
The introduction should provide more context and security considerations for handling authentication tokens with cookies. Consider adding:
Here's a suggested expansion of the introduction:
📝 Committable suggestion