Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create user with firebase admin sdk that can signIn using email and password

I'm using firebase admin SDK on cloud functions to create users using

  admin.auth().createUser({
email: someEmail,
password: somePassword,
})

now I want user to signIn using signInWithEmailAndPassword('someEmail', 'somePassword') but I cannot. I get the following error

{code: "auth/user-not-found", message: "There is no user record corresponding to this identifier. The user may have been deleted."}
like image 763
Yasser Zubair Avatar asked Oct 13 '17 09:10

Yasser Zubair


People also ask

How do I log into Firebase with my email and password?

If you haven't yet connected your app to your Firebase project, do so from the Firebase console. Enable Email/Password sign-in: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Email/password sign-in method and click Save.

Which method of Firebase authentication is used to log in the user with email and password?

You can use Firebase Authentication to sign in a user by sending them an email containing a link, which they can click to sign in. In the process, the user's email address is also verified.

Can you create a user with the same credentials in Firebase?

Firebase account linking allows users to sign into the same account using different authentication providers. By linking the user's Facebook and Twitter credentials, for example, the user can sign into the same account using either sign-in provider.


1 Answers

There doesn't seem to be a reason to Stringify/Parse. This worked after I struggled with an unrelated typo...

FUNCTION CALL FROM REACT JS BUTTON CLICK

     <Button onClick={() => {
                    var data = {
                        "email": "[email protected]",
                        "emailVerified": true,
                        "phoneNumber": "+15551212",
                        "password": "randomPW",
                        "displayName": "User Name",
                        "disabled": false,
                        "sponsor": "Extra Payload #1 (optional)",
                        "study": "Extra Payload #2 (optional)"
                    };
                    var createUser = firebase.functions().httpsCallable('createUser');
                    createUser( data ).then(function (result) {
                        // Read result of the Cloud Function.
                        console.log(result.data)
                    });
                }}>Create User</Button>

And in the index.js in your /functions subdirectory:

const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();

// CREATE NEW USER IN FIREBASE BY FUNCTION
exports.createUser = functions.https.onCall(async (data, context) => {
  try {
    const user = await admin.auth().createUser({
      email: data.email,
      emailVerified: true,
      password: data.password,
      displayName: data.displayName,
      disabled: false,
    });
    return {
      response: user
    };
} catch (error) {
    throw new functions.https.HttpsError('failed to create a user');
  }
});

Screen shot of console output

like image 158
DeveloperOne Avatar answered Oct 26 '22 14:10

DeveloperOne