- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚创建了 SignIn(AppCompatActivity Activity, String email, String password)
方法来登录我的 Firebase Android 应用中的现有用户。它可以在正确的凭据下完美运行,但是当输入不正确的电子邮件和/或密码时,它无法捕获正确的异常。方法如下:
public static int SignIn(final AppCompatActivity activity, String email, String password) {
int resultTask = -2;
FirebaseUtil.mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
if (!FirebaseUtil.mAuth.getCurrentUser().isEmailVerified()) {
FirebaseUtil.mAuth.getCurrentUser().sendEmailVerification();
FirebaseUtil.mAuth.signOut();
resultTask = 1;
} else {
FirebaseUtil.isCustom = true;
resultTask = 2;
}
} else
resultTask = -1;
}
});
return resultTask;
}
当输入错误的凭据时,它返回 -2。该方法出了什么问题?
我通过将方法类型从 int
更改为 void
解决了这个问题。原因如下:
在我的 Activity/fragment 中,我调用了类似的方法,
int result = SignInUpActivity(_params_);
请注意该方法的默认返回值(它是 -2,但在执行登录/注册等 Firebase 操作后,它可能会返回任何其他值)。在这行之后,我检查结果
:
if (result == -2) {
// some stuff
}
else if (result == -1) {
// some other stuff
}
else if (result == 0) {
// ...
}
// and other value checks for result handling
此时,我没有给 Firebase 时间来获取/检查/执行其自己的身份验证方法。因此,SignInUpActivity()
返回其默认值 (-2)。为了彻底解决这个问题,我将方法类型从 int
更改为 void
。
我的最终代码如下所示:
public static void SignIn(final AppCompatActivity activity, final String email, final String password) {
FirebaseUtil.mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
/**
* Thrown when one or more of the credentials passed to a method fail to
* identify and/or authenticate the user subject of that operation.
* Inspect the error code and message to find out the specific cause.
* https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuthInvalidCredentialsException
*/
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Invalid credentials", "Check email and password again.",
true, "ok", null,
false, "", null,
false, -1,
true);
} else if (task.getException() instanceof FirebaseAuthInvalidUserException) {
/**
* Inspect the error code and message to find out the specific cause.
* https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuthInvalidUserException
*/
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Invalid credentials", "Check email and password again.",
true, "ok", null,
false, "", null,
false, -1,
true);
}
} else {
if (!FirebaseUtil.mAuth.getCurrentUser().isEmailVerified()) {
FirebaseUtil.mAuth.getCurrentUser().sendEmailVerification();
FirebaseUtil.mAuth.signOut();
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Email not verified", "To start working, please verify your email.",
true, "ok", null,
false, "", null,
true, R.drawable.ic_verified_user,
true);
} else {
FirebaseUtil.isCustom = true;
ProgressDialog.DismissProgressDialog();
}
}
}
});
}
public static void Register(final AppCompatActivity activity, final String email, final String password) {
FirebaseUtil.mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
/**
* Thrown when an operation on a FirebaseUser instance couldn't be
* completed due to a conflict with another existing user.
* https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuthUserCollisionException
*/
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Try another", "This email has already registered.",
true, "ok", null,
false, "", null,
false, -1,
true);
} else {
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Whoops", "An error occurred. Try again later.",
true, "ok", null,
false, "", null,
false, -1,
true);
}
} else {
try {
FirebaseUtil.mAuth.getCurrentUser().sendEmailVerification();
FirebaseUtil.mAuth.signOut();
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"The last step", "For verification, instructions sent to your email. Please, check your email.",
true, "ok", null,
false, "", null,
true, R.drawable.ic_verified_user,
true);
} catch (Exception e) {
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Whoops", "An error occurred. Try again later.",
true, "ok", null,
false, "", null,
false, -1,
true);
}
}
}
});
}
无论如何,问题解决了。
最佳答案
根据这个问题:How to catch a Firebase Auth specific exceptions
你应该检查
if (!task.isSuccessful())
但是你正在做
if (task.isSuccessful())
然后您可以在 try block 内抛出由 task.getException 返回的异常,并捕获您正在使用的方法可能抛出的每种类型的异常
关于android - 无法捕获 Firebase 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42889052/
我刚刚设置了新的 Firebase 托管以及我的自定义域和我的网页等... 一切都很好,除了真正困扰我的是默认域: projectname.web.app projectname.firebaseap
Firebase 云消息传递和 Firebase 应用内消息传递有什么区别?它们都会向您的应用发送发生了某些事情的通知。我查看了他们的文档,这些是他们的单行描述: Firebase 云消息传递: Fi
是否有任何工具可以帮助将数据从开发迁移到登台再到生产?如果没有,是否有计划 build 它们? 我知道您可以从 Forge 导出 JSON 和导入 JSON,但这不包括授权和安全设置。 最佳答案 我们
firebase 网络应用和 firebase 托管有什么区别?据我所知,网络应用程序用于实际上只是浏览器的应用程序,并且 firebase 托管仅用于网站。 最佳答案 Firebase for We
我有两个 firebase 项目 F1 和 F2。我正在使用基于密码的身份验证来识别 F1 上的用户。我希望在 F2 中识别相同的用户。所以我正在考虑以下基于 this question 的场景: C
正在使用 firebase 云功能 构建服务器,我需要其他服务器来调用我的服务器端点。最重要的是,我需要这些调用我的服务器的第三方域进行身份验证。 问题 1:如何创建这样的服务帐户,在调用中包含服务帐
我开发了一个应用程序来测试使用 flutter 和谷歌身份验证的谷歌登录功能。该项目是一个封闭的项目,只有我可以访问它。但最近我看到有一个来自未知电子邮件 ID 的谷歌登录。用户如何在没有构建我的应用
我有 2 个存储库,一个用于 firebase 功能,另一个用于静态 firebase 托管 react 站点。他们都使用相同的firebase项目。 myfirebaseproject --- fi
背景 -我正在设置一项新功能,允许用户选择他们所在的城市,因为我的应用程序是一个公共(public)交通应用程序。我希望城市位于单独的数据库中,为此我在我的 firebase 项目中创建了一个辅助数据
我构建了一个网络平台,允许用户使用一个用户帐户注册多个网络应用程序。每个应用程序都有自己独立的 Firebase 数据库。是否可以使用 firebase 身份验证并在各种 firebase 数据库之间
在 Firebase 控制台内的 Cloud Messaging View 下,用户可以创建测试通知。此功能还允许您安排通知将发送到一个设备或一组设备的时间。 是否可以创建和发送预定 使用 Fireb
我们正在为我们最近的项目使用 firebase 平台。我们最近推出并一直在监控使用情况。 我可以在 firebase 控制台中看到 firebase 调用的总数,但找不到查看每个函数调用次数的方法。
我正在开发一个使用 Webpack 捆绑模块的 Web 应用程序。无论出于何种原因,在应用程序中引入 Firebase 都会导致 Webpack 抛出错误。当 Webpack 尝试加载 Firebas
我无法在 firebase 控制台中提交支持请求。 登录 Firebase 控制台 导航至 https://firebase.google.com/support/contact/troublesho
我安装 Firebase CLI (firebase-tools) https://github.com/firebase/firebase-tools通过此代码: curl -sL firebase
在我的应用程序中,我尝试使用Firebase crash10.2.1,但无法获得编译的好处。我不断收到消息: 'Failed to resolve:com.google.firebase:fireba
我的 firebase 应用有一个注册用户列表。这些是通过电子邮件和密码身份验证创建的。 我想将 Firebase 数据和用户列表传输到另一个 Firebase 应用。 传输 firebase 数据很
如果我将数据插入到 firebase 中的 node/a/0 中。 结果将把a视为数组a[0]。 同样,如果我在 node/a/1 中设置数据,第一个数组将变为 null "a" : [
我是 Firebase 新手,我正在尝试 Firebase 数据库规则和 Firebase 身份验证。 我尝试使用 firebase 身份验证来执行 firebase 数据库规则。 因此,我创建了一封
所以场景如下: 当我从 Firebase 存储访问文件时: 我从存储桶获取文件(.html、.png、.zip 等)(尺寸较小,不超过 2mb)。 将该文件存储在我的本地存储中,这样应用就不需要再次下
我是一名优秀的程序员,十分优秀!