Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a user on Firebase Authentication in Cloud Functions Http Trigger?

Is it possible to create users (email/password type) from inside the Cloud Functions? I am searching for reference to this, but found nothing.

like image 256
JoaoGalli Avatar asked May 05 '17 13:05

JoaoGalli


3 Answers

Some time ago I faced the exact same problem and this is how I solved it in typescript:

admin.initializeApp();
const db = admin.firestore();
export const createUsr = functions.https.onCall((data) => {

        return admin.auth().createUser({
            email: data.email,
            emailVerified: true,
            password: data.password,
            displayName: data.nombre,
            disabled: false,
            photoURL: data.urldeImagen
        }).then(
            (userRecord) => {
                console.log('Se creó el usuario con el uid: ' + userRecord.uid);
                const infoUsr = {
                    Activo: false,
                    Contrasenia: data.password,
                    Correo: data.email,
                    Foto: data.nombrefoto,
                    Llave: userRecord.uid,
                    Nombre: data.nombre,
                    PP: false,
                    Privilegios: data.privilegios,
                    UrldeImagen: data.urldeImagen
                };
                //update your db 
                return db.collection....
            ).catch((error) => {
                console.log(error);
                return error
            });
        });
like image 199
Daniel Avatar answered Oct 12 '22 14:10

Daniel


The createUser() function let's you do just that.

admin.auth().createUser({
    email: "[email protected]",
    emailVerified: false,
    password: "secretPassword",
    displayName: "John Doe",
    photoURL: "http://www.example.com/12345678/photo.png",
    disabled: false
})
.then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully created new user:", userRecord.uid);
})
.catch(function(error) {
    console.log("Error creating new user:", error);
});

https://firebase.google.com/docs/auth/admin/manage-users#create_a_user

like image 40
Clinton Avatar answered Oct 12 '22 12:10

Clinton


Base on answer of @mike-brian-olivera I make a fucntion cloud you can call on front end

exports.register = functions.https.onCall((data, context) => {
    const { email, pass } = data;

    return admin
        .auth()
        .createUser({
            email,
            password: pass,
        })
        .then(userRecord => {
            // See the UserRecord reference doc for the contents of userRecord.

            console.log({ uid: userRecord.uid });
            return { success: userRecord.uid };
        })
        .catch(error => {
            return { error: error.message };
        });
});
like image 22
Steve Phuc Avatar answered Oct 12 '22 13:10

Steve Phuc