简体   繁体   English

在使用 FirebaseAuth 时,我是否应该在重新加载 Flutter web 应用程序时直接使用 IndexedDB 来确定身份验证 state?

[英]Should I consume IndexedDB directly when reloading a Flutter web app to determine auth state when using FirebaseAuth?

I am building a Flutter web app using FirebaseAuth for authentication, and I am trying to figure out how to load the correct route when the user a) is authenticated, and b) the user refreshes the web page.我正在构建一个使用 FirebaseAuth 进行身份验证的 Flutter web 应用程序,并且我试图弄清楚当用户 a) 通过身份验证和 b) 用户刷新 web 页面时如何加载正确的路由。

When the app loads, firebase gets initialised and then fetches the user's auth state, returning it about 2 seconds later in an authStateChanges handler.当应用程序加载时,firebase 被初始化,然后获取用户的身份验证 state,大约 2 秒后在authStateChanges处理程序中返回它。

The problem I have is that by the time the auth state is returned, my app has already determined which route should be presented to the user, namely, login (since, as far as the app is concerned, the user is not yet authenticated).我遇到的问题是,在返回 auth state 时,我的应用程序已经确定应该向用户显示哪条路线,即登录(因为就应用程序而言,用户尚未通过身份验证) .

This results in the user first being navigated to login, and then later, when the auth state is updated, they can be navigated somewhere else - which is a crappy UX.这导致用户首先被导航到登录,然后,当 auth state 更新时,他们可以被导航到其他地方——这是一个糟糕的用户体验。

In the Firebase Auth docs under the section "Persisting Authentication State" it says "The Firebase SDKs for all platforms provide out of the box support for ensuring that your user's authentication state is persisted across app restarts or page reloads" - and then goes on to say "On web platforms, the user's authentication state is stored in IndexedDB."在“持久身份验证状态”部分下的Firebase Auth 文档中,它说“所有平台的 Firebase SDK 提供开箱即用的支持,以确保您的用户身份验证 state 在应用程序重新启动或页面重新加载时保持不变” - 然后继续说“在 web 平台上,用户的身份验证 state 存储在 IndexedDB 中。”

Does that mean, on page reloads, I need to access the auth state of the user directly from the IndexedDB?这是否意味着,在页面重新加载时,我需要直接从 IndexedDB 访问用户的身份验证 state? Or does it mean Firebase returns the value stored there for me automatically?或者它是否意味着 Firebase 自动返回存储在那里的值?

If I am supposed to consume IndexedDB directly, how do I do that, and what key should I use?如果我应该直接使用 IndexedDB,我该怎么做,我应该使用什么密钥?

Firebase persists and restores the user's authentication state automatically. Firebase 保留并自动恢复用户身份验证 state。 You don't need to do anything for that.你不需要为此做任何事情。

To respond to authentication state changes in your app, listen to onAuthStateChanged as shown in the first code snippet in the documentation on getting the current user .要响应应用中的身份验证 state 更改,请监听onAuthStateChanged ,如获取当前用户文档中的第一个代码片段所示。 This method returns a stream, so if you want to show the current user in the UI, you can use a StreamBuilder .此方法返回 stream,因此如果要在 UI 中显示当前用户,可以使用StreamBuilder

then when firebase auth still determined if this user authenticated or not, instead of showing login page directly you can show the loading screen.然后当 firebase auth 仍然确定该用户是否通过身份验证时,您可以显示加载屏幕而不是直接显示登录页面。

the easy way you can use go router .您可以使用go 路由器的简单方法。

and set:并设置:

GoRouter(
....
initialLocation: LoadingPage(),
redirect: _goTologinPageorHomePageBasedAuthStatus()
..)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 添加 Flutter_auth 依赖项时应用未加载 - Flutter app not loading when I add Flutter_auth dependency 如何使用 firebase_auth 包修复 flutter 应用程序中的 FirebaseAuth.instance.currentUser()? 它总是返回 null - How to fix FirebaseAuth.instance.currentUser() in flutter app using firebase_auth package? It is always returning null 如何将 FirebaseAuth.AuthStateListener 置于连续监听模式,以便当 state 发生变化时,我会收到通知 - how to put FirebaseAuth.AuthStateListener on continous listening mode so that when the state changes, i get a notification 想要在 flutter 应用程序中添加新文档时使用一个信号发送推送通知。我应该使用 launchUrl() 方法吗? - Wants to send push notification using one signal when new document is added in the flutter app.Should I use launchUrl() method for it? 用户在 flutter 中使用 FirebaseAuth 注册 android 应用程序后导航到页面 - Navigate to a page after user sign up in the android app in flutter using FirebaseAuth 添加firebase auth和core时出现Flutter错误 - Flutter errors when adding firebase auth and core 在 web 应用程序中安装 azure 文件共享时,我得到一个 state 的 InvalidCredentials - When mounting an azure file share in a web app I get a state of InvalidCredentials Flutter:[FirebaseFirestore][I-FST000001] 使用 StreamBuilder 且无 inte.net 连接时应用程序关闭时出错 - Flutter: [FirebaseFirestore][I-FST000001] Error closes App when using StreamBuilder and no internet connection Flutter FirebaseAuth 单元测试 - Flutter FirebaseAuth unit testing Flutter Web 中创建默认应用时FirebaseOptions不能为null - FirebaseOptions cannot be null when creating the default app in Flutter Web
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM