Jam*_*oly 2 javascript firebase firebase-authentication
这是我的 JS 代码:
import { ref } from "vue"
import { projectAuth } from '../firebase/config'
import { getAuth, createUserWithEmailAndPassword, updateProfile } from 'firebase/auth'
const error = ref(null)
const isPending = ref(false)
const signup = async(email, password, displayName) => {
error.value = null
isPending.value = true
try {
const res = createUserWithEmailAndPassword(projectAuth, email, password)
console.log(projectAuth.currentUser)
if (!res) {
console.log('Could not complete the signup')
throw new Error('Could not complete the signup')
}
console.log(projectAuth.currentUser)
await updateProfile(projectAuth.currentUser, {displayName})
error.value = null
isPending.value = false
return res
} catch(err) {
console.log('ERROR: ' + err.message)
error.value = err.message
isPending.value = false
}
}
const useSignup = () => {
return {error, signup, isPending}
}
export default useSignup
Run Code Online (Sandbox Code Playgroud)
每当用户注册时,我的 Vue3 应用程序都会调用此脚本中的注册函数。createUserWithEmailAndPassword 函数成功并且用户显示在 firebase 中。但我还想为我的用户添加显示名称,因此我尝试使用 updateProfile 函数来执行此操作,但存在问题。
问题是即使在创建用户后,projectAuth.currentUser 也为空,我不明白为什么?
该createUserWithEmailAndPassword()方法返回一个承诺。由于您的功能是async,请尝试添加await:
const res = await createUserWithEmailAndPassword(projectAuth, email, password)
console.log(projectAuth.currentUser)
Run Code Online (Sandbox Code Playgroud)
或者,您可以直接从 res传递User对象:updateProfile
const { user } = await createUserWithEmailAndPassword(projectAuth, email, password)
await updateProfile(user, { displayName })
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1029 次 |
| 最近记录: |