Cookie nginx+pm2
export default defineNuxtRouteMiddleware((to, _from) => {
const user = useStrapiUser()
const authPage = ['login', 'signup']
const noAuthPage = ['blog-slug', 'blog', 'docs-slug', 'docs', 'pricing', 'index']
if (user.value) {
if (authPage.includes(to.name.toString())) {
return navigateTo({ name: 'index' })
}
} else {
if (!(noAuthPage.includes(to.name.toString())) && !authPage.includes(to.name.toString())) {
useCookie('redirect', { path: '/' }).value = to.fullPath
return navigateTo({ name: 'login' })
}
}
})
In the development environment, after logging in, const user = useStrapiUser() has a value; however, in the production environment (using Nginx + PM2), after logging in, const user = useStrapiUser() is empty. I believe this issue is related to the cookie configuration. Here is my cookie configuration:
cookie: {
path: '/',
maxAge: 14 * 24 * 60 * 60,
secure: process.env.NODE_ENV === 'production',
sameSite: true,
domain: process.env.COOKIE_DOMAIN
},
2 Replies
Hi @dominick. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the information provided in the knowledge sources, I can offer some insights and suggestions regarding your cookie issue in the production environment with Nginx and PM2.
The problem you're experiencing seems to be related to how cookies are handled between the server and client sides in your Nuxt 3 application. Here are a few points to consider:
1. Cookie Access: In Nuxt 3, the
useCookie
composable is designed to work in both client and server environments. However, there might be issues with how the cookie is being set or accessed in your production setup. Nuxt useCookie docs (https://nuxt.com/docs/api/composables/use-cookie) provide more details on its usage.
2. Server-Side Cookie Handling: Since you're using Nginx and PM2, make sure that your server configuration is correctly forwarding the cookie headers. The useStrapiUser()
function might not be receiving the cookie data on the server side.
3. Cookie Domain: Ensure that the COOKIE_DOMAIN
environment variable is set correctly in your production environment. The domain should match the domain from which your application is served.
4. Secure Flag: You're setting secure: process.env.NODE_ENV === 'production'
, which is correct for production. However, make sure your site is served over HTTPS, as secure cookies are only sent over encrypted connections.
5. SameSite Attribute: You've set sameSite: true
, which is equivalent to Lax
. Consider if this is the appropriate setting for your use case. You might want to experiment with 'strict'
or 'none'
(with secure flag) depending on your cross-site requirements.
To troubleshoot this issue, you could try the following:
1. Use the refreshCookie
utility to manually refresh the cookie value when you know it has changed. This might help if the cookie is not being updated properly after login. Nuxt refreshCookie docs (https://nuxt.com/docs/api/utils/refresh-cookie)
2. Enable the experimental cookieStore
option in your nuxt.config.ts
file. This allows Nuxt to listen for cookie updates and refresh useCookie
ref values automatically:
Nuxt experimental features docs (https://nuxt.com/docs/guide/going-further/experimental-features#cookiestore)
3. Check your Nginx configuration to ensure it's properly forwarding cookie headers to your Nuxt application.
4. Verify that the cookie is actually being set in the browser in your production environment. You can check this in the browser's developer tools.
5. Consider using server-side API routes to handle authentication if you continue to have issues with client-side cookie handling. Nuxt API routes docs (https://nuxt.com/docs/api/composables/use-cookie#cookies-in-api-routes)
If these suggestions don't resolve the issue, you may need to further investigate how cookies are being handled in your specific Nginx + PM2 setup, as the knowledge sources don't provide specific information about this particular configuration.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@468746434490007552> Kapa.ai is still learning and improving, please let me know how I did by reacting below.