nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
54.63k stars 5k forks source link

Nuxt3 useCookie does not work on firebase for production #26055

Closed snapxtp closed 4 months ago

snapxtp commented 7 months ago

Environment

{ "name": "nuxt-app", "private": true, "type": "module", "scripts": { "build": "nuxt build --dotenv .env.prod", "dev": "nuxi dev --dotenv .env.dev", "generate": "nuxt generate", "preview": "nuxt preview", "postinstall": "nuxt prepare", "build:firebase": "npm run build -- --preset=firebase" }, "dependencies": { "@mdi/font": "^7.4.47", "@pinia/nuxt": "^0.5.1", "material-design-icons-iconfont": "^6.7.0", "pinia": "^2.1.7", "save": "^2.9.0", "vue": "^3.4.19", "vue-router": "^4.2.5", "vuetify": "^3.5.1" }, "devDependencies": { "@sidebase/nuxt-session": "^0.2.8", "firebase": "^10.8.0", "firebase-admin": "^12.0.0", "firebase-functions": "^4.7.0", "nuxt": "^3.10.3", "nuxt-font-loader": "^2.3.5", "sass": "^1.70.0", "sass-loader": "^14.0.0", "vite-plugin-vuetify": "^2.0.1" } }

Reproduction

The login process application with server side cookie control using firebase admin sdk, which works fine in local environment, does not work after compilation and publishing on firebase. What I saw in the logs is that cookies cannot be read on the server side with useCookie.

Values cannot be read when the page is refreshed.

COOKIE always returns null. Also, no cookie is added in the header information in the queries made.

`export default defineNuxtRouteMiddleware(async (to, from) => {

 if(process.server){
    const url = useRuntimeConfig().public.baseUrl;
    const cookie = useCookie('userSession');

    console.log('COOKIE:', cookie.value)

    const data = await $fetch(`/api/auth/authstatus`, {
        server: true,
        credentials: 'include',
        baseURL:url,
        method: 'POST',
        body:JSON.stringify({sessionCookie:cookie.value})
    });

    if (data.statusCode === 401 && to.path !== '/login') {
        return navigateTo('/login');
    } else if(data.statusCode === 200 && to.path == '/login'){
        return navigateTo('/');
    } else {
        return;
    }
 }

}) `

Describe the bug

The login process application with server side cookie control using firebase admin sdk, which works fine in local environment, does not work after compilation and publishing on firebase. What I saw in the logs is that cookies cannot be read on the server side with useCookie.

Values cannot be read when the page is refreshed.

COOKIE always returns null. Also, no cookie is added in the header information in the queries made.

Additional context

The login process application with server side cookie control using firebase admin sdk, which works fine in local environment, does not work after compilation and publishing on firebase. What I saw in the logs is that cookies cannot be read on the server side with useCookie.

Values cannot be read when the page is refreshed.

COOKIE always returns null. Also, no cookie is added in the header information in the queries made.

Logs

No response

snapxtp commented 7 months ago

For those who use Firebase hosting and firefunction, the solution is to write a cookie with the name "__session" and read it as mentioned below. !!!!

It might be good to include it in the Nuxt3 Firebase deployment documentation.

https://www.reddit.com/r/Nuxt/comments/1aw50al/accessing_cookies_in_prod/

danielroe commented 7 months ago

Would you see if you can reproduce this in pure Nitro (reproduction sandbox), and if so, raise there? 🙏

lp-mkdir commented 7 months ago

For those who use Firebase hosting and firefunction, the solution is to write a cookie with the name "__session" and read it as mentioned below. !!!!

It might be good to include it in the Nuxt3 Firebase deployment documentation.

https://www.reddit.com/r/Nuxt/comments/1aw50al/accessing_cookies_in_prod/

I've spent 2 days doubting the whole implementation of the cookies, and trying absurd things to solve this same issue. I've just switched the cookie name to __session and solved the issue. Thank you.

PS: They have it in the firebase docs, but agree 100% that we should have this in the Nuxt3 firebase deployment docs. https://firebase.google.com/docs/hosting/manage-cache#using_cookies

romeromembersy commented 5 months ago

Gosh, I spent over 2 hours with this problem, thankfully I found this thread, otherwise it would have been days. This should definitely be part of the Firebase deployment instructions

manniL commented 5 months ago

PR welcome for the docs 🙏🏻