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

风流意气都作罢 提交于 2019-12-06 02:49:15

问题


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


回答1:


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

admin.auth().createUser({
    email: "user@example.com",
    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




回答2:


Use this Http based function to make the endpoint to create users.

the link would be something like this. https://us-central1-FIREBASE-PROYECT.cloudfunctions.net/register

exports.register = functions.https.onRequest((request,response)=>{

if (request.method !== "POST") {
    response.status(400).send("what are you trying baby?");
    return 0;
}

const email = request.body.email;
const pass = request.body.pass;

admin.auth().createUser({
    email: email,
    emailVerified: true,
    password: pass,
})
    .then(function(userRecord) {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Conductor " + email + "Creado" );
        response.send({"uid":userRecord.uid});
        return 1;
    })
    .catch(function(error) {
        response.send("Error: "+error);
        console.log("Error creating new user:", error);
        return 1;
    });

return 1;
});



回答3:


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 };
        });
});



回答4:


For anyone experiencing @tonsteri's issue "insufficient permission to access the requested resource":

To resolve your issue with insufficient privileges, you'll need to update to the v2 of the cloud-functions package. I ran into the same issue using v1 - upgrading resolved the issue.

I'd respond inline, but I lack sufficient reputation.




回答5:


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
            });
        });


来源:https://stackoverflow.com/questions/43806750/can-i-create-a-user-on-firebase-authentication-in-cloud-functions-http-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!