question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access admin.auth()?

See original GitHub issue

Is it even possible to access the admin object from the migration script?

I want to change some filed names inside customUserClaims, so I thought it would be nice to create a migration for that. But for doing that I would need to access admin.auth().listUsers() and then run setCustomUserClaims() for each individual user.

I’ve added const admin = require("firebase-admin"); before the script, which amazingly worked, but that feels like a workaround and not a proper solution.

I would suggest adding admin object to MigrateOptions

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
xd2commented, Jan 5, 2022

Hi. In my case, the fireway app.auth() (v1.0.2) was failing using emulator even with --forceWait as suggested by @dsvgit (even with the --dryRun switch). I was getting the following error

FirebaseAuthError: Failed to determine project ID for Auth. Initialize the SDK with service account credentials or set project ID as an app option. Alternatively set the GOOGLE_CLOUD_PROJECT environment variable. at FirebaseAuthError.FirebaseError [as constructor] hereunder my command line to start the migration FIRESTORE_EMULATOR_HOST=localhost:8080 yarn fireway migrate --require=ts-node/register --forceWait --dryRun

I end up configuring my env using these two variables:

GCLOUD_PROJECT=myproject FIREBASE_AUTH_EMULATOR_HOST=localhost:9099

Local migration using ‘app.auth’ works properly with the emulator now !

1reaction
dsvgitcommented, Nov 2, 2021

app property which is provided by fireway seems like works for me

  • --dryrun option doesn’t work for auth updates (only for firestore)
  • I had to use --forceWait option to make it works
const clearClaims = async (auth, authUser) => {
  if (authUser.customClaims) {
    return await auth.setCustomUserClaims(authUser.uid, null);
  }
};

const updateClaims = async (auth, authUser, changes) => {
  const newCustomClaims = { ...authUser.customClaims, ...changes };
  return await auth.setCustomUserClaims(authUser.uid, newCustomClaims);
};

module.exports.migrate = async ({ firestore, app }) => {
  const auth = app.auth();

  const usersSnapshot = await firestore.collection("users").get();
  usersSnapshot.forEach(async (snapshot) => {
    const userId = snapshot.id;
    try {
      const authUser = await auth.getUser(userId);

      // await clearClaims(auth, authUser);
      // console.log(authUser.customClaims);

      await updateClaims(auth, authUser, { admin: true });
    } catch (error) {
      // console.log(error, userId);
    }
  });
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to the Admin Auth API | Firebase Authentication
The Firebase Admin SDK provides an API for managing your Firebase users with elevated privileges. The admin user management API gives you the...
Read more >
Introduction to the Admin Auth API - Identity Platform
The Firebase Admin SDK lets you set custom attributes on user accounts. With custom user claims, you can give users different levels of...
Read more >
Build a Role-based API with Firebase Authentication - Toptal
Firebase allows you to get quickly up and running with an enterprise-level auth API, which you can extend later on. · Almost every...
Read more >
How to use the firebase-admin.auth function in firebase-admin
url : undefined, email: user.email, }; let loginUid; try { if (linkedAccountUid) { await admin.auth().updateUser(linkedAccountUid ...
Read more >
Firebase Admin SDK to Log user in from Server - Stack Overflow
There is a method on the firebase admin SDK to get the user info by Email admin.auth().getUserByEmail(email) . This method returns all the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found