我可以在Cloud Functions Http Trigger中使用Firebase身份验证创建用户吗?

时间:2017-05-05 13:50:33

标签: firebase firebase-authentication google-cloud-functions

是否可以从云功能内部创建用户(电子邮件/密码类型)? 我正在寻找这个参考,但一无所获。

5 个答案:

答案 0 :(得分:15)

createUser()函数让你做到这一点。

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

答案 1 :(得分:1)

基于@ mike-brian-olivera的答案,我制作了一个可以在前端调用的功能云

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

答案 2 :(得分:0)

使用此基于Http的功能来使端点创建用户。

链接将是这样的。 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 :(得分:0)

对于遇到@tonsteri问题“访问所需资源的权限不足”的任何人:

要解决特权不足的问题,您需要更新到cloud-functions软件包的v2。我在使用v1时遇到了同样的问题-升级解决了该问题。

我会内联回复,但我缺乏足够的声誉。

答案 4 :(得分:0)

前一段时间,我遇到了完全相同的问题,这就是我在打字稿中解决问题的方式:

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