我在云功能上使用 firebase admin SDK 来创建用户使用
admin.auth().createUser({
email: someEmail,
password: somePassword,
})
现在我希望用户使用
signInWithEmailAndPassword('someEmail', 'somePassword')
登录,但我不能。
我收到以下错误
{code: "auth/user-not-found", message: "There is no user record corresponding to this identifier. The user may have been deleted."}
似乎没有理由进行字符串化/解析。在我遇到一个不相关的错字后,这起作用了……
来自 React JS 按钮点击的函数调用
<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>
在您的 /functions 子目录中的 index.js 中:
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');
}
});
到 2022 年,Admin SDK 中仍然没有内置允许在模拟器中创建用户的方法。
你可以做的是使用模拟器的REST API直接在那里创建用户。 API 记录在此处:https://firebase.google.com/docs/reference/rest/auth#section-create-email-password
如果你已经安装了got和nanoid,你可以使用下面的代码在模拟器中创建用户。
import { nanoid } from 'nanoid'
import httpClientFor from '../lib/http-client/client.js'
const httpClient = httpClientFor('POST')
export const createTestUser = async ({ email = `test-${nanoid(5)}@example.io`, password = nanoid(10), displayName = 'Tony' } = {}) => {
const key = nanoid(31)
const { body: responseBody } = await httpClient(`http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=${key}`, {
json: {
email,
password,
displayName
}
})
const responseObject = JSON.parse(responseBody)
const { localId: userId, email: userEmail, idToken, refreshToken } = responseObject
return { userId, userEmail, idToken, refreshToken }
}
请注意:由于没有实施错误处理,此代码段不适合生产使用。
尝试使用 firebase auth REST API 来创建用户。您可以通过 REST API 查询 Firebase Auth 后端。这可用于各种操作,例如创建新用户、登录现有用户以及编辑或删除这些用户。
https://firebase.google.com/docs/reference/rest/auth#section-create-email-password
像那样尝试
请确保用户是从面板创建的
admin.auth().createUser({
email: "[email protected]",
emailVerified: false,
phoneNumber: "+11234567890",
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);
});
以防万一其他人遇到这个我能够在this的帮助下修复它。
这是
onCreate
云函数内部的一个工作示例:
exports.newProjectLead = functions.firestore
.document('newProjectForms/{docId}')
.onCreate(async (snapshot) => {
const docId = snapshot.id
// this is what fixed it the issue
// stringify the data
const data = JSON.stringify(snapshot.data())
// then parse it back to JSON
const obj = JSON.parse(data)
console.log(obj)
const email = obj.contactEmail
console.log(email)
const password = 'ChangeMe123'
const response = await admin.auth().createUser({
email,
password
})
data
const uid = response.uid
const dbRef = admin.firestore().collection(`clients`)
await dbRef.doc(docId).set({
id: docId,
...data,
uid
}, {
merge: true
})
console.log('New Client Created')
})