所以我正在制作一個小網路應用程式,這是我第一次嘗試同時使用 firebase 和 React Js,所以我的問題是每次新用戶注冊時都試圖在 fireStore 中制作一個檔案
// this is my SignUp Function
async function signUp(email, password,) {
await createUserWithEmailAndPassword(auth, email, password);
}
/*and Im calling in it my component and as onSubmit Function and passing it the email and password
and this is where things got me a little tricky because wheni tried to pass my CreateDocument Function as a callBack it throws me an error
and this is my CreateDocument
function const creatProfileDocument = (currentuser) => {
setDoc(doc(db, "users", currentuser.uid), {
email: currentuser.email,
age: false,
name: "",
})
};*/
我真的希望有人能在這里幫助我
uj5u.com熱心網友回復:
通常有兩種方法可以解決這個問題 - 立即在您的signUp代碼中或通過 Firebase 函式中的身份驗證觸發器。
在您的注冊代碼中
您可以在異步signUp功能后立即創建檔案:
async function signUp(email, password) {
// This function returns a credential which gives you the user's uid
// which you could then use to create your document
const credential = await createUserWithEmailAndPassword(auth, email, password);
const uid = credential.user.uid
// Create a new document using the uid as the document id
// or however else you want to use this
const ref = firebase.auth().collection("users").doc(uid)
await ref.set({
email,
uid
})
}
這樣做的好處是您可以立即創建檔案,以便在signUp函式回傳時知道它在那里。不利的一面是您必須放寬一些安全規則,以允許用戶從客戶端在此集合上創建檔案。有很多方法可以確保它的安全,但請注意這一點。
使用云功能
您的signUp代碼會在 Firebase 中創建新的身份驗證記錄,并且 Firebase 具有身份驗證觸發器函式,可以在創建或洗掉新的身份驗證記錄時觸發。
exports.createUserDoc = functions.auth.user().onCreate((user) => {
// Your new auth record will have the uid and email on it because
// you used email as the way to create the auth record
return admin.firestore().collection("users").doc(user.uid).set({
email: user.email,
uid: user.uid
})
});
這是一種安全的處理方式,不需要您打開任何安全規則。但缺點是不能保證觸發功能立即觸發。它們通常會在幾秒鐘內觸發,但我已經看到它需要 30-60 秒。因此,如果您的客戶端代碼在創建帳戶時立即需要該檔案,這不是一個可行的選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/311252.html
標籤:javascript 反应 火力基地 redux-firestore
下一篇:時間戳驗證Firestore
