2

I have a weird problem with firebase-admin when trying to use admin methods such as getUsers or createUser.

For the record, I can perfectly use admin.auth().verifyToken(token), where token is a JWT token and auth() is my firebase config.

But as soon as I try other methods (admin.auth().getUsers([{uid: <firebase_uid>}]); for instance) it gives me the Error: error:0909006C:PEM routines:get_name:no start line Error.

I've been playing around for weeks now and have not found a clue from where to start.

A little bit of context here:

**App (these are different part of the app) **


// ENV FILE

BO_FIREBASE_TYPE=XXXXXX
BO_FIREBASE_project_id=XXXXXX
BO_FIREBASE_private_key=-----BEGIN PRIVATE KEY-----  private key given by firebase  -----END PRIVATE KEY-----
BO_FIREBASE_client_email=XXXXX
BO_FIREBASE_client_id=XXXXXXX
BO_FIREBASE_auth_uri=XXXXXX
BO_FIREBASE_token_uri=XXXXXX
BO_FIREBASE_auth_provider_x509_cert_url=XXXXXXX


// App
const fireBaseCreds = {
  type: process.env.BO_FIREBASE_TYPE,
  project_id: process.env.BO_FIREBASE_project_id,
  private_key_id: process.env.BO_FIREBASE_private_key,
}

export const app = admin.initializeApp(
  {
    credential: admin.credential.cert(firebaseCreds)
  },
);

export const isAuthenticated = async (req, res, next) => {
  try {
    const [_, token] = req.headers.authorization.split(" ");

    const decodedValue = await app.auth().verifyIdToken(token); // This actually works fine
    if (decodedValue) {
      const user = await userQueries.getOne(
        { email: decodedValue.email },
        {},
        "company"
      );
      if (!user) {
        throw new Error("User doesen't exist");
      }
      req.user = user;
      return next();
    } else {
      return res.status(401).send("Access denied");
    }
  } catch (err) {
    return res.status(401).send(err.message);
  }
};

// In a controller where I want to fetch users from firebase.

export const getAllUsers = catchAsync(async (req, res) => {
  const users = await userQueries.getAll({
    sort: "email",
    pagination: { skip: 0, limit: 100 },
  });
  const userMails = users._items.map((item) => ({
    email: item.email,
  }));

  // /!\ THIS FAILS
  const firebaseUsers = await admin.auth().getUsers(userMails); // This actually fails
  // gives Error: error:0909006C:PEM routines:get_name:no start line
  // /!\ THIS HAS FAILED

  return requestResolver(res, users);
});

So if anyone has any idea of how to fix this or telling me where i am wrong, it'll be much appreciated!

Thanks in advance

Lulu
  • 21
  • 3

1 Answers1

0

So after looking deep into the limbs of StackOverflow I found this answer:

The answer

Basically it tells you to let a couple of \n into the private key

\n-----BEGIN PRIVATE KEY-----\nXXXXXXX\n-----END PRIVATE KEY-----\n.

It seems that firebase decoding function needs those while in the verifyToken it doesn't need it

Lulu
  • 21
  • 3