android - 创建新用户时如何禁用firebaseauth更改?
问题描述
我在我的一个项目中使用了 firebase。当项目第一次启动时,我匿名验证用户并将匿名用户的帐户数据保存在 firestore 中。用户所做的一切,例如竖起大拇指,也会保存在 Firestore 中。对于某些部分,用户需要使用电子邮件和密码或谷歌帐户注册,并且当用户第一次注册时,他还需要使用发送给用户电子邮件地址的链接进行验证。
我的问题是当我使用firebaseAuth.createUserWithEmailAndPassword
firebaseauth 在验证用户之前将用户保存为当前用户。在我的情况下,我的计划是用户创建一个新帐户然后验证该帐户,当用户第一次使用经过验证的帐户成功登录时,firebaseauth 状态会发生变化。当用户是新验证帐户中的匿名用户时,我还想让用户有机会传输保存在 firestore 中的所有信息或操作。哪种方式是处理我的计划的最佳方式?
解决方案
使用您所拥有的电子邮件和密码创建帐户后,您将以该用户身份登录。要保留您当前的匿名登录,您应该初始化 Firebase SDK 的第二个实例以在您验证帐户时保留该帐户。您需要的粗略(未经测试的代码)步骤包括:
- 初始化 FirebaseApp 的第二个实例。您可以使用以下方法“克隆”默认实例:
// CreateAccountActivity.java (or similar)
FirebaseApp mDefaultApp = FirebaseApp.getInstance();
FirebaseAuth mDefaultAuth = FirebaseAuth.getInstance();
FirebaseApp mVerifierApp = FirebaseApp.initializeApp(mDefaultApp.getApplicationContext(), mDefaultApp.getOptions(), "verifier");
- 根据第二个实例获取 FirebaseAuth 实例。
// underneath above code
FirebaseAuth mVerifierAuth = FirebaseAuth.getInstance(mVerifierApp);
- 使用第二个实例创建您的用户
// in the submit handler for logging in with email and password of CreateAccountActivity.java
mVerifierAuth.createUserWithEmailAndPassword(email, password)
.onSuccessTask(this, new SuccessContinuation<AuthResult, Void>() {
@Override
public Task<Void> then(AuthResult authResult) {
// user was created, send verification email
FirebaseUser user = authResult.getUser();
return user.sendEmailVerification(/* ActionCodeSettings object here */);
}
})
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// Sign in was successful and verification email was sent
Log.d(TAG, "createNewEmailUser:send-verification-succeeded");
// TODO: Update UI to say to check email
} else {
// Sign in failed/couldn't send verification email, display a message to the user.
Log.w(TAG, "createNewEmailUser:send-verification-failure", task.getException());
Toast.makeText(CreateAccountActivity.this, "Authentication & Verification failed.",
Toast.LENGTH_SHORT).show();
// TODO: Update UI to say something went wrong
}
}
});
根据需要处理验证电子邮件的过程(您可以在应用程序中处理代码)
验证后,将匿名帐户链接到电子邮件通行证帐户
// in the event handler of "email has been verified" of CreateAccountActivity.java
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
mDefaultAuth.getCurrentUser() // the account of the anonymous login
.linkWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "linkWithCredential:success");
// TODO: show accounts have been linked and then move on to use app as normal
} else {
Log.w(TAG, "linkWithCredential:failure", task.getException());
Toast.makeText(CreateAccountActivity.this, "Failed to link accounts.",
Toast.LENGTH_SHORT).show();
// TODO: update UI to show error/contact us dialog
}
}
});
推荐阅读
- javascript - 通过 window.location.href 防止 javascript 跨域重定向
- csv - 如何使用 SAS 从文本文件中提取多个 CSV 数据集?
- symfony - 如何使用 twig 和 symfony 在输入中显示日期?
- git - 如何在 Raku Perl 6 中正确解压缩 Git 对象?
- lua - Wireshark Lua 解析器插件表错误
- c++ - 为什么 arduino 的音调功能在 RTOS 中不起作用?
- python - 将整数添加到日期时间
- java - Spring Boot Security 中 OAuth2Client 中的 SSO
- java - 客户端证书丢失:如何恢复它
- apache-spark - org.apache.spark.SparkException: Job aborted due to stage failure: Task in stage failed,Lost task in stage : ExecutorLostFailure (executor 4 lost)