[使用createUserWithEmailAndPassword的Android Firebase身份验证

问题描述 投票:0回答:1

我正在尝试通过Android向Firebase添加新用户,但收到的错误消息对我来说没有意义。我在Firebase控制台中启用了电子邮件/密码登录。我收到的错误消息说:

com.example.myprojectD / RegisterActivity:validateForm:LoginActivity validateForm已启动。com.example.myprojectI / BiChannelGoogleApi:[FirebaseAuth:] getGoogleApiForMethod()返回的Gms:com.google.firebase.auth.api.internal.zzao@xyz123

com.example.myproject W / FirebaseMethods:createUserWithEmail:失败com.google.firebase.FirebaseException:发生内部错误。 [7:]

我的代码库如下:

public class FirebaseMethods {

private static final String TAG = "FirebaseMethods";

// declare Firebase auth
private FirebaseAuth mAuth;
private String user;

private Context mContext;

public FirebaseMethods(Context context){
    mAuth = FirebaseAuth.getInstance();
    mContext = context;

    if(mAuth.getCurrentUser() != null){
        user = mAuth.getCurrentUser().getUid();
    }
}

/**
 * Register a new username and email to Firebase authentication
 * @param email
 * @param password
 * @param username
 */
public void registerNewEmail(String email, String password, String username){
    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "createUserWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(mContext, "Authentication failed.", Toast.LENGTH_SHORT).show();
                        updateUI(null);
                    }
                }
            });
}

private void updateUI(FirebaseUser user) {
    Log.d(TAG, "updateUI: LoginActivity updateUI started.");
    if (user != null) {
        /*If the user is already logged in then navigate back to the MainActivity.*/
        Intent intent = new Intent(mContext, MainActivity.class);
        mContext.startActivity(intent);


    } else {
        //add alternative action here
    }
}

enter image description here

android firebase firebase-authentication
1个回答
2
投票

仅在Firebase控制台中启用电子邮件/密码登录是不够的。

您必须执行以下操作。

  1. 在android studio中转到“工具”:

  2. 选择Firebase:它将在窗口右侧显示提示。

  3. 选择身份验证->电子邮件和密码身份验证。

  4. 连接到Firebase。

  5. 它将显示一个对话窗口。 ->选择您要将应用程序连接到的现有项目。如果您尚未使用Android Studio登录Firebase。然后使用启用了电子邮件/密码身份验证的电子邮件登录。

  6. 此后,单击“添加到您的应用程序的FIREBASE身份验证”,然后它将再次显示一个对话框窗口。然后点击“接受更改”。它会自动添加一些依赖项和google-services.json文件添加到您的项目中。

  7. 您已完成android studio中的配置。

注意:创建用户时,请以正确的方式添加您的电子邮件地址。即:“ [email protected]”。有时由于没有“格式正确”的电子邮件地址而显示错误。

现在在此项目中使用SignIn方法,然后尝试再次登录。它肯定会工作。

© www.soinside.com 2019 - 2024. All rights reserved.