我正在嘗試在.displayName
創建用戶后立即設定用戶的屬性。
.updateProfile()
問題:用戶需要一些時間來創建,所以呼叫后無法立即使用.createUserWithEmailAndPassword()
。那么我怎樣才能最終.displayName
在創建后更新權限呢?
現在我正在嘗試這種方式:
export function signup(currentUser, email, password) {
createUserWithEmailAndPassword(auth, email, password)
updateProfile(currentUser, {displayName : "some name"})
}
當我呼叫該函式時,我將此函式用于currentUser
export function useAuth() {
const [ currentUser, setCurrentUser ] = useState();
useEffect(() => {
const unsub = onAuthStateChanged(auth, user => setCurrentUser(user));
return unsub; // Cleaning Function
}, [])
return currentUser;
}
uj5u.com熱心網友回復:
createUserWithEmailAndPassword()
和updateProfile()
方法都是異步的并回傳一個 Promise。因此,您需要鏈接 Promises或使用async/await
Scott 在他的評論中提到的。
1/ 鏈接承諾
export function signup(currentUser, email, password) {
createUserWithEmailAndPassword(auth, email, password)
.then(userCredential => {
updateProfile(userCredential.user, {displayName : "some name"})
})
}
2/ 使用async/await
export async function signup(currentUser, email, password) {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
await updateProfile(userCredential.user, {displayName : "some name"})
}
uj5u.com熱心網友回復:
您需要updateProfile(currentUser, {displayName : currentUser.displayName})
在 useAuth 鉤子更新 currentUser 后呼叫
useEffect(() =>{
if ('userName' in currentUser && currentUser.userName !== '') {
updateProfile(currentUser, {displayName : currentUser.userName})
}
}, [currentUser])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/487200.html
標籤:javascript 反应 火力基地 firebase 身份验证