Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
docs: Add example for handling authentication with useCookie in Nuxt …
…docs
  • Loading branch information
AngeloSchulerPiletti committed Oct 15, 2024
commit 580cc680240e89146996ea36fdf2477fe8fa41f2
33 changes: 33 additions & 0 deletions docs/3.api/2.composables/use-cookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Comment on lines +234 to +238
Copy link

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:

  • Explanation of why cookies are preferred over localStorage for token storage
  • CSRF protection recommendations when using cookies for authentication
  • Token expiration and refresh token patterns
  • Security implications of different cookie configurations

Here's a suggested expansion of the introduction:

 ## Handling Authentication with Cookies

-You can manage authentication tokens using cookies like this:
+Cookies are often preferred over localStorage for storing authentication tokens due to their enhanced security features and protection against XSS attacks. Here's how to implement secure cookie-based authentication:
+
+::warning
+When implementing authentication, consider these security best practices:
+- Use `httpOnly: true` to prevent XSS attacks from accessing the token
+- Implement CSRF protection for cookie-based authentication
+- Consider token expiration and refresh token patterns
+- Use secure configurations like `secure: true` in production
+::
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
## Handling Authentication with Cookies
You can manage authentication tokens using cookies like this:
## Handling Authentication with Cookies
Cookies are often preferred over localStorage for storing authentication tokens due to their enhanced security features and protection against XSS attacks. Here's how to implement secure cookie-based authentication:
::warning
When implementing authentication, consider these security best practices:
- Use `httpOnly: true` to prevent XSS attacks from accessing the token
- Implement CSRF protection for cookie-based authentication
- Consider token expiration and refresh token patterns
- Use secure configurations like `secure: true` in production
::

```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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const accessToken = useCookie('accessToken', {
sameSite: 'strict',
})
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
})


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