FirebaseAuthError when using Nookies to get token. (NextJS)

Viewed 363

I'm trying to verify if the user was able to complete the registration successfully. For this, I want to get the user's token via cookies, as in the following code:

[...]

    export async function getServerSideProps(ctx) {
        try {
            const cookies = nookies.get(ctx)
            console.log(JSON.stringify(cookies, null, 2));
            const token = await firebaseAdmin.auth().verifyIdToken(cookies.token)
    
            const { email } = token
    
            return {
                props: { message: `${email} was successfully registered!`}
            }
        } catch (err) {
            console.log(err)
    
            return { props: { message: 'Error'} }
        }
    }
[...]

This function returns the following error:

errorInfo: { code: 'auth/argument-error', message: 'Decoding Firebase ID token failed. Make sure you passed the entire string JWT which represents an ID token. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.' }

I believe I must be misusing nookies.get(ctx), and I say this because the retrieved cookie (token) is equal to a "", which doesn't make much sense to me. Then, how can I properly use nookies to get that token?

I would appreciate it, if anyone could help me to find out what I'm missing out. Thanks in advance.

EDIT: The user account is created, and the email and password are stored in the Firebase Auth. It is only this piece of code that does not work.

1 Answers

Since Next 10.0.4 you can get cookies without nookies

  const cookies = ctx.req.cookies

try in this way, if you still getting token as empty check the way that you set cookies.

Related