英文:
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,而不是实时数据库,如果相关的话。
这是我的代码:
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();
}
/* 构建辅助 mAuth 以便创建新用户 */
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()));
}
}
/* 使用辅助 auth 创建新帐户 */
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());
}
}
});
}
请注意,代码中存在一些拼写错误,例如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:
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());
}
}
});
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论