Firebase创建用户无需登录 [英] Firebase create user without sign in

查看:82
本文介绍了Firebase创建用户无需登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以管理员用户"身份登录时,希望从我的应用程序中创建一个新的用户帐户.问题是我只想创建它而不实际登录.

I wish to create a new user account from my application when logged in as a "admin user", The issue is i just want to create it not actually sign in.

使用电子邮件/密码创建新用户时是否可以禁用自动登录.

is it possible to disable the automatic sign in when creating a new user with the email / password.

我看到其他人已经问过有关JS和Swift的问题,但似乎找不到任何特定于Android的相关信息.我正在尝试与此人 a>但使用android

I see others have asked this question in relation to JS and swift but can not seem to find any android specific related info. i'm trying to achieve the same thing as this person but with android

任何帮助表示赞赏

推荐答案

以下是您可以应用的经过测试的解决方案(仅在几分钟前实现).

Here is a tested solution you can apply (just implemented a few minutes before).

要创建新的用户帐户,您需要引用FirebaseAuth.

For creating a new user account you need the reference of FirebaseAuth.

因此您可以创建两个不同的FirebaseAuth对象,例如:

So you can create two different FirebaseAuth objects like:

private FirebaseAuth mAuth1;
private FirebaseAuth mAuth2;

现在在onCreate中,您可以将它们初始化为:

Now in the onCreate you can initialize them as:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mAuth1 = FirebaseAuth.getInstance();

        FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
                .setDatabaseUrl("[Database_url_here]")
                .setApiKey("Web_API_KEY_HERE")
                .setApplicationId("PROJECT_ID_HERE").build();

       try { FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, "AnyAppName");
        mAuth2 = FirebaseAuth.getInstance(myApp);
    } catch (IllegalStateException e){
        mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance("AnyAppName"));
    }

//..... other code here
}

要获取ProjectID和WebAPI密钥,可以在firebase项目控制台中转到项目设置".

To get ProjectID, WebAPI key you can go to Project Settings in your firebase project console.

现在要创建用户帐户,您必须使用 mAuth2 ,而不是 mAuth1 .然后,成功注册后,您可以注销该mAuth2用户.

Now to create the user account you have to use mAuth2, not mAuth1. And then on successful registration, you can log out that mAuth2 user.

示例:

private void createAccount(String email, String password)
    {
        mAuth2.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {


                        if (!task.isSuccessful()) {
                            String ex = task.getException().toString();
                            Toast.makeText(RegisterActivity.this, "Registration Failed"+ex,
                                    Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            Toast.makeText(RegisterActivity.this, "Registration successful",
                                    Toast.LENGTH_SHORT).show();
                            mAuth2.signOut();
                        }



                        // ...
                    }
                });

    }

您需要担心的点(实际上不必担心):

管理员只能创建新的用户帐户.但是上述解决方案允许所有经过身份验证的用户创建一个新的用户帐户.

The admin should only be able to create the new user accounts. But the above solutions is allowing all authenticated user to create a new user account.

因此,要解决此问题,您可以利用Firebase实时数据库.只需添加一个诸如"is_user_admin"之类的键,然后从控制台本身将值设置为true.您只需要在某人尝试创建新用户帐户之前验证该用户即可.使用这种方法,您可以设置自己的管理员.

So to solve this problem you can take help of your firebase real-time database. Just add a key like "is_user_admin" and set the value as true from the console itself. You just need to validate the user before someone is trying to create a new user account. And using this approach you can set your own admin.

截至目前,我认为android没有firebase-admin SDK.因此,可以使用上述方法.

As of now, I don't think there is firebase-admin SDK for android. So one can use the above approach.

这篇关于Firebase创建用户无需登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆