java - 使用firebase auth,如何在不登录当前活动帐户的情况下创建帐户?
问题描述
在我正在进行的一个项目中,我们使用的是 firebase auth。
在应用程序内部,用户可以为其他用户创建帐户。但是,我们遇到的问题是,每当创建新帐户时,新用户都会自动登录,因此会话丢失,例如:
人员 A 创建了一个名为 A 的帐户
A 自动登录(这是正确的)
A 为 B 人创建了一个帐户,名为 B
B自动登录,A被踢出(这很糟糕)
我找到了同样问题的这个答案,但是在尝试实现之后,它仍然不起作用(也许答案已经过时了?)。我正在使用firestore,而不是实时的,以防万一。
这是我的代码:
FirebaseAuth mAuth;
FirebaseAuth mAuth2;
FirebaseFirestore firestore;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_management);
mAuth = FirebaseAuth.getInstance();
firestore = FirebaseFirestore.getInstance();
buildSecondAuth()
}
/*Builds the auxiliar mAuth in order to create a new user*/
private void buildSecondAuth() {
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setDatabaseUrl(firestore.getApp().getOptions().getDatabaseUrl())
.setApiKey(firestore.getApp().getOptions().getApiKey())
.setApplicationId(firestore.getApp().getOptions().getProjectId()).build();
try {
FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, mAuth.getApp().getName());
mAuth2 = FirebaseAuth.getInstance(myApp);
} catch (IllegalStateException e)
{
mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance(mAuth.getApp().getName()));
}
}
/*Uses the auxiliar auth to create a new account*/
private void signUpNewUser()
{
mAuthJugador.createUserWithEmailAndPassword(getEmail(), getPass())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if (task.isSuccessful())
{
mAuth2.signOut();
}
else
{
Log.w("TAG", "createUserWithEmail:failure", task.getException());
}
}
});
}
解决方案
我认为您不能为同一个 firebase 应用程序创建 2 个身份验证实例。您可以在同一个 Android 应用中为不同的 Firebase 应用创建 2 个实例。
了解更多信息。请参阅此先前已回答的帖子。
推荐阅读
- c - C 中的原子操作,而不是 C++
- c# - 如何使用 MSTest 从 TestCleanup 方法访问 TestMethod 参数
- flutter - 我试图在 Flutter 中分配用户 ID 使用 Map Funcuin
- mailchimp - MailChimp 检索大型列表数据
- python - 提高 Skyfield 中日出/日落计算的性能
- autodesk-forge - 从第三方应用程序与 Forge Viewer 交互
- c# - 将数组字节编码为字符串
- swift - 针对 URL 扩展的 iOS15 UTType 弃用
- javascript - 使用 jQuery 在多个页面中的欢乐之旅插件
- gitlab - Gitlab-CI 规则如果/更改没有按预期工作