Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase admin - Create Custom token: There is no user record corresponding to the provided identifier

I'm using firebase admin becasue I want to validate my users in a different server. I have the following code but I'm getting the error

{ code: 'auth/user-not-found',
  message: 'There is no user record corresponding to the provided identifier.' } }

Docs https://firebase.google.com/docs/auth/admin/create-custom-tokens

    var uid = user_dict['uid'];
    admin.auth().createCustomToken(uid)
        .then(function (customToken) {
            admin.auth().updateUser(uid, {
                //email: user_dict['email'],
                emailVerified: true,
                displayName: user_dict['displayName'],
                photoURL: user_dict['photoURL']
            })
            .then(function(userRecord) {

                response = {
                    token:customToken
                };
                res.send(response);

            })
            .catch(function(error) {
                console.log("Error updating user:", error);
                res.statusCode = 401;
                res.send("Error al actualizar usuario")
            });


        })
        .catch(function (error) {
            console.log("Error creating custom token:", error);
            res.statusCode = 401;
            res.send("El usuario no existe")
        });

The wierd part this code is working for 1 user, but not for the rest. Why?

UPDATE

I change my code to this:

var uid = user_dict['uid'];

admin.auth().createCustomToken(uid) .then(function (customToken) {

    admin.auth().getUser(uid)
        .then(function(userRecord) {
            // See the UserRecord reference doc for the contents of userRecord.
            console.log("Successfully fetched user data:", userRecord.toJSON());
            admin.auth().updateUser(uid, {
                email: user_dict['email'],
                emailVerified: true,
                displayName: user_dict['displayName'],
                photoURL: user_dict['photoURL']
            })
            .then(function(userRecord) {

                response = {
                    token:customToken
                };
                res.send(response);

            })
            .catch(function(error) {
                console.log("Error updating user:", error);
                res.statusCode = 401;
                res.send("Error al actualizar usuario")
            });
        })
        .catch(function(error) {
            console.log("Error fetching user data:", error);

            admin.auth().createUser({
                uid:uid,
                email: user_dict['email'],
                emailVerified: true,
                displayName: user_dict['displayName'],
                photoURL: user_dict['photoURL'],
                password:req.body['password']
            })
            .then(function(userRecord) {
                // See the UserRecord reference doc for the contents of userRecord.
                console.log("Successfully created new user:", userRecord.uid);
                response = {
                    token:customToken
                };
                res.send(response);
            })
            .catch(function(error) {
                console.log("Error creating new user:", error);
                res.statusCode = 401;
                res.send("Error al crear el usuario")
            });

        });

})
.catch(function (error) {
    console.log("Error creating custom token:", error);
    res.statusCode = 401;
    res.send("El usuario no existe")
});

You can see in the following screenshot carlos@mail is created using the function createUser() and the other email ricardo@mail is not. They don't have the same provider why such a difference?

enter image description here

like image 735
Ricardo Avatar asked Mar 08 '23 20:03

Ricardo


1 Answers

createCustomToken() accepts arbitrary user ID strings. But updateUser() does not. The UID passed to updateUser() must already exist in the Firebase project. You need to call createUser() to first create those user accounts in Firebase.

like image 99
Hiranya Jayathilaka Avatar answered May 21 '23 20:05

Hiranya Jayathilaka