gladly-team/next-firebase-auth

Missing uid when using local emulator suite

deen13 opened this issue ยท 12 comments

I'm currently trying to setup next.js authentication with this library and the firebase emulator suite. I had success connecting the next-firebase-auth with our cloud test instance but no success with the emulator. Every login attempt fails with an auth/argument-error where the uid is null.

Versions

next-firebase-auth version: ^0.14
Firebase JS SDK: ^8.9.1
Next.js: 12.0.7
Firebase Tools in Docker: 10.0.1

To Reproduce
Steps to reproduce the behavior:

  1. Setup Local Emulator Suite and Example Project
  2. Configure Emulator Suite
  3. Create firebase user using the emulator ui
  4. Attempt a login

The exception occurs when calling BaseAuth.createCustomToken.

BaseAuth.prototype.createCustomToken = function (uid, developerClaims) {
  return this.tokenGenerator.createCustomToken(uid, developerClaims);
};

Error

errorInfo: {
  code: 'auth/argument-error',
  message: '`uid` argument must be a non-empty string uid.'
}

Emulator Export

{
  "kind": "identitytoolkit#DownloadAccountResponse",
  "users": [
    {
      "uid": "5e9d49ecc652bf25ef18c461",
      "localId": "tjPIPI6VuxrhwNi5gKi8prsaJoRv",
      "createdAt": "1641059106748",
      "lastLoginAt": "1641059106748",
      "displayName": "John Doe",
      "photoUrl": "",
      "emailVerified": false,
      "email": "test@test.com",
      "salt": "fakeSalt1pE9Vg0uDz5b1JTNSNVA",
      "passwordHash": "fakeHash:salt=fakeSalt1pE9Vg0uDz5b1JTNSNVA:password=movement",
      "passwordUpdatedAt": 1641059106757,
      "validSince": "1641059106",
      "providerUserInfo": [
        {
          "providerId": "password",
          "email": "test@movementsnacks.com",
          "federatedId": "test@movementsnacks.com",
          "rawId": "test@movementsnacks.com",
          "displayName": "John Doe",
          "photoUrl": ""
        }
      ],
      "customAttributes": "{\"role\": \"admin\"}",
      "lastRefreshAt": "2022-01-01T17:45:06.758Z"
    }
  ]
}

Environment

FIREBASE_CLIENT_EMAIL=<test-cloud-environment-mail>
NEXT_PUBLIC_FIREBASE_PUBLIC_API_KEY=<test-cloud-environment-api-key>
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=<test-cloud-environment-domain>
NEXT_PUBLIC_FIREBASE_PROJECT_ID=movement-snacks-test

FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----......-----END PRIVATE KEY-----"

COOKIE_SECRET_CURRENT=someSecretValue
COOKIE_SECRET_PREVIOUS=anotherSecretValue

NEXT_PUBLIC_COOKIE_SECURE=false

FIREBASE_AUTH_EMULATOR_HOST="localhost:9099"

I also wonder about why the first properties are required in local development since they are pointing to an online cloud instance. ๐Ÿ˜•

Did you also set firebaseAuthEmulatorHost in your next-firebase-auth config?

Yes I did @kmjennison

OK, thanks. I'll investigate this when I can make the time, though that probably won't be for a little while. Hopefully, others who have set up the emulator can weigh in here with suggestions.

I got this error, though I am not using the emulator. The problem was I didn't load the google application credentials properly.

In my case, there was an "app/invalid-credential" error thrown here. Maybe there's a missing check before calling createCustomToken?

@deen13 If the cause of your issue is the same as what @Dig-Doug describes, you'll be able to catch the error by adding onVerifyTokenError to your config.

Same issue as above. Any help would be appreciated

Same issue but I'm not using the emulator nor is it set in my .env file. Here is what I have observed:

  1. This happens for me only after I introduce a broken js file and an exception is thrown
  2. I can solve it by restarting stopping the dev server and running yarn dev again
egisz commented

Had same issue, the cause was I forgot NextJs is hiding env variables from client side (ones not starting with NEXT_PUBLIC_xxxx.
In initAuth.js, I was checking if FIREBASE_AUTH_EMULATOR_HOST declared, but in reality it clientside it was undefined.
This leaded to emulator not loading or misbehaving.
Solution was to duplicate same variable as NEXT_PUBLIC_FIREBASE_AUTH_EMULATOR_HOST for client side....

I have the same issue but the suggestion from @egisz does not solve it for me.
I am still trying to find a workaround or the cause of this issue, any help would be very appreciated.

It was a configuration issue on my end, adding onVerifyToken: err => console.error(err) in the package configuration helped me debug this

OP (@deen13), I see you're using Docker. For me, this issue was caused by confusing networking (1) between containers within a Docker network and (2) between a Docker container and the host.

I have a container than runs my Next.js app with next-firebase-auth (call it app) and another container that runs my Firebase emulators, including the Auth emulator (call it firebase). I exposed a port from app to the host, so that I could visit my Next.js app. Also, I exposed (among others) the Auth emulator's port (say 4321) from firebase to the host.

app had environment variable FIREBASE_AUTH_EMULATOR_HOST set to localhost:4321 and exposed on both client and server, which allowed the host's browser to do Auth things, but did not allow the Next.js server in app to reach the Auth emulator in the firebase container (since localhost within app does not map to the firebase container).

My solution was to set two environment variables:
FIREBASE_CLIENT_AUTH_EMULATOR_HOST=localhost:4321
FIREBASE_AUTH_EMULATOR_HOST=firebase:4321

and use them in the following way with next-firebase-auth:

firebaseAuthEmulatorHost: typeof window !== 'undefined'
      ? process.env.FIREBASE_CLIENT_AUTH_EMULATOR_HOST
      : process.env.FIREBASE_AUTH_EMULATOR_HOST,

@deen13 If the cause of your issue is the same as what @Dig-Doug describes, you'll be able to catch the error by adding onVerifyTokenError to your config.

I got same error message and onVerifyTokenError not give me any help . Now I can't use next-firebase-auth thing in my whole Local Machine.Doesn't work redirect thing, cookies things and login api status 500 . It only working in Production , and It was previously pushed thing. Even example-app is stopped working in our Local Machine.