使用 Firebase Auth,如何在不登出当前活跃帐户的情况下创建新帐户?

huangapple go评论99阅读模式
英文:

Using firebase auth, how can I create accounts without login out of the current active one?

问题

在我正在进行的项目中,我们正在使用Firebase身份验证。

在应用程序内,用户能够为其他用户创建帐户。然而,我们遇到的问题是,每当创建一个新帐户时,新用户会自动登录,因此会话会丢失,例如:

  • 人员A创建一个名为A的帐户

  • A会自动登录(这是正确的)

  • A为人员B创建一个帐户,名为B

  • B会自动登录,而A会被踢出(这是不好的)

我找到了解决相同问题的这个链接 的答案,但尝试实施后,它仍然不起作用(也许答案已过时?)。我正在使用Firestore,而不是实时数据库,如果相关的话。

这是我的代码:

  1. FirebaseAuth mAuth;
  2. FirebaseAuth mAuth2;
  3. FirebaseFirestore firestore;
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_account_management);
  7. mAuth = FirebaseAuth.getInstance();
  8. firestore = FirebaseFirestore.getInstance();
  9. buildSecondAuth();
  10. }
  11. /* 构建辅助 mAuth 以便创建新用户 */
  12. private void buildSecondAuth() {
  13. FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
  14. .setDatabaseUrl(firestore.getApp().getOptions().getDatabaseUrl())
  15. .setApiKey(firestore.getApp().getOptions().getApiKey())
  16. .setApplicationId(firestore.getApp().getOptions().getProjectId()).build();
  17. try {
  18. FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, mAuth.getApp().getName());
  19. mAuth2 = FirebaseAuth.getInstance(myApp);
  20. } catch (IllegalStateException e)
  21. {
  22. mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance(mAuth.getApp().getName()));
  23. }
  24. }
  25. /* 使用辅助 auth 创建新帐户 */
  26. private void signUpNewUser()
  27. {
  28. mAuthJugador.createUserWithEmailAndPassword(getEmail(), getPass())
  29. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
  30. {
  31. @Override
  32. public void onComplete(@NonNull Task<AuthResult> task)
  33. {
  34. if (task.isSuccessful())
  35. {
  36. mAuth2.signOut();
  37. }
  38. else
  39. {
  40. Log.w("TAG", "createUserWithEmail:failure", task.getException());
  41. }
  42. }
  43. });
  44. }

请注意,代码中存在一些拼写错误,例如mAuthJugador应更正为mAuth

英文:

In a project I'm working on, we are using firebase auth.

Inside the app, users are able to create accounts for other users. However, we have the problem that, whenever a new account is created, the new user is automatically signed in, so the session is lost, for example:

  • Person A creates an account, named A

  • A is automatically signed in (this is correct)

  • A creates an account for person B, named B

  • B is automatically signed in and A is kicked out (this is bad)

I found this answer to the same problem, but after trying the implementation, it still doesnt work (maybe the answer is outdated?). I am using firestore, not realtime, in case it is relevant.

This is my code:

  1. FirebaseAuth mAuth;
  2. FirebaseAuth mAuth2;
  3. FirebaseFirestore firestore;
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_account_management);
  7. mAuth = FirebaseAuth.getInstance();
  8. firestore = FirebaseFirestore.getInstance();
  9. buildSecondAuth()
  10. }
  11. /*Builds the auxiliar mAuth in order to create a new user*/
  12. private void buildSecondAuth() {
  13. FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
  14. .setDatabaseUrl(firestore.getApp().getOptions().getDatabaseUrl())
  15. .setApiKey(firestore.getApp().getOptions().getApiKey())
  16. .setApplicationId(firestore.getApp().getOptions().getProjectId()).build();
  17. try {
  18. FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, mAuth.getApp().getName());
  19. mAuth2 = FirebaseAuth.getInstance(myApp);
  20. } catch (IllegalStateException e)
  21. {
  22. mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance(mAuth.getApp().getName()));
  23. }
  24. }
  25. /*Uses the auxiliar auth to create a new account*/
  26. private void signUpNewUser()
  27. {
  28. mAuthJugador.createUserWithEmailAndPassword(getEmail(), getPass())
  29. .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;()
  30. {
  31. @Override
  32. public void onComplete(@NonNull Task&lt;AuthResult&gt; task)
  33. {
  34. if (task.isSuccessful())
  35. {
  36. mAuth2.signOut();
  37. }
  38. else
  39. {
  40. Log.w(&quot;TAG&quot;, &quot;createUserWithEmail:failure&quot;, task.getException());
  41. }
  42. }
  43. });
  44. }

答案1

得分: 0

我认为您不能为同一个Firebase应用程序创建两个身份验证实例。您可以在同一个Android应用程序中为不同的Firebase应用程序创建2个实例。

有关更多信息,请参阅此之前已回答的帖子

英文:

I think you cannot create 2 instances of authencation for the same firebase app. You can create 2 instances for different firebase app in the same Android app.

For more info. See this previously answered post.

huangapple
  • 本文由 发表于 2020年9月8日 00:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63781312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定