9 javascript firebase firebase-authentication
请耐心等待我是谷歌Firebase的新手,对不起我的愚蠢
根据Firebase网站上的JS Auth文档,它仅显示如何获取displayName以及如何更新displayName.所以我试着更新它.但这有点不合逻辑,因为如何在不创建的情况下更新内容.
所以我的问题是,如何在注册期间设置用户的displayName?
function createUser(email, password) {
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {
error.message.replace(".", "");
alert(error.message + " (" + error.code + ")");
document.getElementById("password").value = "";
});
if (firebase.auth().currentUser != null) {
firebase.auth().currentUser.updateProfile({
displayName: document.getElementById("name").value
}).then(function () {
console.log("Updated");
}, function (error) {
console.log("Error happened");
});
}
}Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个,但事实证明它不起作用......
真诚的,法鲁克
小智 25
这是我在 firebase v9 中使用异步等待所使用的
// firebase-config.js
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
apiKey: ...,
authDomain: ...,
projectId: ...,
storageBucket: ...,
messagingSenderId: ...,
appId: ...,
measurementId: ...,
}
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
Run Code Online (Sandbox Code Playgroud)
// register.js
import { auth } from "../../services/firebase-config";
import {
createUserWithEmailAndPassword,
sendEmailVerification,
updateProfile,
} from "firebase/auth";
// handleRegister
const register = async (name, email, password) => {
try {
await createUserWithEmailAndPassword(auth, email, password).catch((err) =>
console.log(err)
);
await sendEmailVerification(auth.currentUser).catch((err) =>
console.log(err)
);
await updateProfile(auth.currentUser, { displayName: name }).catch(
(err) => console.log(err)
);
} catch (err) {
console.log(err);
}
};
Run Code Online (Sandbox Code Playgroud)
然后你只需通过传递name、email和 来调用此函数 onClick/onSubmitpassword
这是我使用 formik onSubmit 的实现
onSubmit={({ name, email, password }) => {
register(name, email, password);
}}
Run Code Online (Sandbox Code Playgroud)
或者您可以简单地调用按钮 onClick 中的函数
<button onClick={() => register(name, email, password)}>submit</button>
Run Code Online (Sandbox Code Playgroud)
boj*_*eil 22
你必须链接请求:
__CODE__
我不能({displayName: name})直接使用(编辑器中的语法错误)。然后,我找到了另一种方法:
UserUpdateInfo updateInfo = UserUpdateInfo();
updateInfo.displayName = name;
result.user.updateProfile(updateInfo);
Run Code Online (Sandbox Code Playgroud)
这是在 Dart 中(我在 Flutter 中使用 Firebase)。
小智 7
firebase
.auth()
.createUserWithEmailAndPassword(newUser.email, newUser.password)
.then((res) => {
const user = firebase.auth().currentUser;
return user.updateProfile({
displayName: newUser.name
})
})
Run Code Online (Sandbox Code Playgroud)
这对我有用。
| 归档时间: |
|
| 查看次数: |
10110 次 |
| 最近记录: |