NuxtN
Nuxt14mo ago
4 replies
enchbox

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
},
Was this page helpful?