Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing uid when using local emulator suite #411

Open
deen13 opened this issue Jan 7, 2022 · 12 comments
Open

Missing uid when using local emulator suite #411

deen13 opened this issue Jan 7, 2022 · 12 comments
Labels
help wanted Extra attention is needed

Comments

@deen13
Copy link

deen13 commented Jan 7, 2022

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"
@deen13
Copy link
Author

deen13 commented Jan 7, 2022

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

@kmjennison kmjennison added the help wanted Extra attention is needed label Jan 7, 2022
@kmjennison
Copy link
Contributor

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

@deen13
Copy link
Author

deen13 commented Jan 8, 2022

Yes I did @kmjennison

@kmjennison
Copy link
Contributor

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.

@Dig-Doug
Copy link

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?

@kmjennison
Copy link
Contributor

@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.

@MingCherry
Copy link

Same issue as above. Any help would be appreciated

@samos123
Copy link

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
Copy link

egisz commented Mar 3, 2022

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....

@fabienheureux
Copy link

fabienheureux commented Jun 4, 2022

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

@arseniybanayev
Copy link

arseniybanayev commented Jun 4, 2022

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,

@thanhtutzaw

This comment was marked as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

9 participants