且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建新用户后,Firebase身份验证状态会发生变化

更新时间:2023-12-03 09:54:40

创建时用户使用 firebase.auth()。createUserWithEmailAndPassword()它会自动注销当前用户并登录到新创建的用户。要避免这种情况,你必须创建使用admin sdk的新用户。

while creating the user using firebase.auth().createUserWithEmailAndPassword() it will automatically logged out the current user and will logged into the newly created user.To avoid this you have to create the new user using admin sdk.

这里是示例代码:

exports.createUser = functions.firestore
.document('user/{userId}')
.onCreate(async (snap, context) => {
    try {
        const userId = snap.id;
        const batch = admin.firestore().batch();
        const newUser = await admin.auth().createUser({
            disabled: false,
            displayName: snap.get('name'),
            email: snap.get('email'),
            password: snap.get('password')
        });

        const ref1 = await 
        admin.firestore().collection('user').doc(newUser.uid);
            await batch.set(ref1, {
            id: newUser.uid,
            email: newUser.email,
            name: newUser.displayName,
            createdAt: admin.firestore.FieldValue.serverTimestamp()
        });
        const ref3 = await admin.firestore().collection('user').doc(userId);
        await batch.delete(ref3);
        return await batch.commit();
    }
    catch (error) {
        console.error(error);
    }

});