【问题标题】:Android Firebase Authentication using createUserWithEmailAndPassword使用 createUserWithEmailAndPassword 进行 Android Firebase 身份验证
【发布时间】:2020-01-31 09:10:40
【问题描述】:

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

com.example.myproject D/RegisterActivity: validateForm: LoginActivity validateForm 已启动。 com.example.myproject I/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
    }
}

【问题讨论】:

  • 您是否从 Fire Base 控制台开启 authentication
  • 我把它打开了。查看更新的照片。我认为问题出在我的代码中。

标签: android firebase firebase-authentication


【解决方案1】:

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

你必须做以下事情。

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

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

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

  4. 连接到 Firebase。

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

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

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

注意:在创建用户时以正确的方式添加您的电子邮件地址。即:'xyz@gmail.com'。有时它会显示错误,因为不是“格式正确”的电子邮件地址。

现在在此项目中创建您的 SignIn 方法并尝试再次登录。一定会成功的。

【讨论】:

  • 我把它打开了。查看更新的照片。我认为问题出在我的代码中。
猜你喜欢
  • 2017-03-11
  • 2015-09-12
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 2020-10-09
  • 2019-09-04
  • 2018-10-19
  • 2021-05-26
相关资源
最近更新 更多