问题描述
我正在使用Firebase Auth插件对我的Flutter应用进行身份验证.
I'm using Firebase Auth plugin for authentication of my Flutter app.
直到升级(不确定是否相关)到最新的Firebase身份验证版本:
Until the upgrade (not sure if relevant) to the latest Firebase auth version:
firebase_core: ^0.5.0
firebase_auth: ^0.18.0+1
一切正常.
现在,我第一次出现Sentry错误:
Now I got, for the first time, Sentry error:
FirebaseAuthException: [firebase_auth/user-token-expired] The user's credential is no longer valid. The user must sign in again.
File "exception.dart", line 20, in catchPlatformException
File "zone.dart", line 1198, in _rootRunUnary
File "zone.dart", line 1100, in _CustomZone.runUnary
File "future_impl.dart", line 160, in _FutureListener.handleError
File "future_impl.dart", line 708, in Future._propagateToListeners.handleError
File "future_impl.dart", line 729, in Future._propagateToListeners
File "future_impl.dart", line 537, in Future._completeError
File "async_patch.dart", line 47, in _AsyncAwaitCompleter.completeError
File "platform_channel.dart", in MethodChannel.invokeMapMethod
File "<asynchronous suspension>"
File "unparsed"
这怎么会发生?用户说,他已经有几天没有使用这个程序了.据我了解Firebase身份验证文档,auth令牌会自动使用刷新令牌进行刷新.
How can this happen? The user said, that he didn't use this app for a few days. As I understand Firebase Authentication documentation, the auth token automatically gets refreshed with the refresh token.
如何缓解此问题?
我在哪里/如何捕获此异常以将用户重定向到登录屏幕?
Where/how can I catch this exception to redirect a user to the login screen?
推荐答案
您可以做几件事,
第一:
当用户启动您的应用程序时,您应该首先检查该用户是否已经登录,如果已经登录,则请静默登录,如果尚未登录,请将该用户发送到登录页面,以下是示例我四个月前构建的应用程序(您必须向您的应用程序添加 google_sign_in 依赖项)
when a user starts your app , you should check first whether the user is already signed in or not , if he is ,then sign in silently ,if not , then send the user to sign in page , below is an example from an app I build four months ago (you will have to add google_sign_in dependency to your app)
class _StartingPageState extends State<StartingPage> {
Future<dynamic> decideStartingPage() async {
bool isUserSignedIn = await googleSignIn.isSignedIn();
if (isUserSignedIn == true) {
FirebaseUser futurefbuser =await getCurrentFirebaseUser();
assignFireBaseUser(futurefbuser);
await googleSignIn.signInSilently();
return HomeTabView();
}
else
return LoginPage();
}
Future<dynamic> startingpage;
@override
void initState(){
super.initState();
startingpage=decideStartingPage();
FirebaseAdMob.instance.initialize(appId: "ca-app-pub-...........");
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future:startingpage,
builder: (BuildContext ctx, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center( child : const CircularProgressIndicator());
}
else
return snapshot.data;
}
);
}
}
第二:
您可以做的第二件事是保存用户登录时获得的访问令牌和(或)刷新令牌,到期时您可以使用refresh令牌获得另一个访问令牌.这意味着您需要做一些额外的工作,因为您必须将令牌保存在共享首选项或json文件中.
The second thing you can do is to save the access token and(or) refresh token you get when a user signs in, when that expires you can get an another access token using the refresh token . That means a little extra work for you as you will have to save the token in shared preferences or in a json file .
现在在哪里添加用于处理上述过程的代码?您必须确定只有经过身份验证的用户才能执行的第一件事,例如写入数据库,因为您的应用程序未经身份验证,因此您的应用很可能抛出异常.使用try,catch并最终可以再次登录用户,而无需用户执行任何操作.有关更多信息,请访问此
Now where to add the code for that handles the above procedure ? You have to identify the first thing that only an authenticated user can do like writing to the database , it is quite possible that your app is throwing exception at that because the user is unauthenticated for that operation . Using try , catch and finally you can sign in the user again without needing the user to do anything . for more info visit this
在第一种情况下,用户需要再次显式登录,第二种方法为您带来了更多工作,但对用户而言却更为方便.选择是你的.
In first case user would need to explicitly sign in again , the second method requires extra work for you but more convenient for the user . Choice is yours .
这篇关于Flutter Firebase Auth-捕获过期的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!