matthewmueller / next-cookies

Tiny little function for getting cookies on both client & server with next.js.
368 stars 17 forks source link

This does not work on production mode on firebase cloud function #19

Open wilfredonoyola opened 5 years ago

wilfredonoyola commented 5 years ago

I have created the follow function to detect if an user was logged frontend and server side

export const isLogged = ctx => {
    const { token } = nextCookie(ctx)

    /*
     * This happens on server only, ctx.req is available means it's being
     * rendered on server. If we are on server and token is not available,
     * means user is not logged in.
     */
    if (ctx.req && token) {
        return true
    }

    // We already checked for server. This should only happen on client.
    if (token) {
        return true
    }

    return false

}

This works very good in my localhost both frontend/server side. But after deploy to firebase cloud function this only work with frontend side.

Let me know that I will do to fix this.

Thanks.

nfriedly commented 5 years ago

Hi @wilfredonoyola,

I don't know a lot about firebase, but here is a couple of possibilities.

Do you think you could add some debug logging or whatnot to test any of these theories?

wilfredonoyola commented 5 years ago

Hey @nfriedly , thanks for reply!.

Let me know your comments

Thanks

nfriedly commented 5 years ago

Aah, I think I see the problem. When you export to static, it assumes that JavaScript will only be executing client-side, so I don't know that it even includes any server-side code. However, I believe that firebase virtual functions are actually executed in a server-side Node.js environment. So the client-side APIs to read cookies aren't going to work.

You might try just deploying a regular server-side build to firebase. I suspect it won't quite work out-of-the-box, but I think it's going to be closer to a solution. Next.js uses express.js under-the-hood, and I believe there are folks who have gotten that to work n firebase.

Alternatively, firebase provides some stuff for managing session cookies for you - https://firebase.google.com/docs/auth/admin/manage-cookies