【问题标题】:Anonymous User not detecting by firebase code error匿名用户未通过 firebase 代码错误检测
【发布时间】:2021-05-22 13:40:47
【问题描述】:

我正在尝试添加匿名用户功能并将其保存到设备以供进一步使用,但每当我重新启动我的应用程序时,firebase 都不会检测 currentUser 是否为匿名用户,并且它在 3 到 5 天前的工作状态非常完美。

匿名登录代码

signInAnonymously().then((value) async {
  SharedPreferences preferences = await SharedPreferences.getInstance();
  preferences.setString(
    'name',
    FirebaseAuth.instance.currentUser.uid
  );
  preferences.setBool('anon', true);
});

主代码

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
    
  await Firebase.initializeApp();
    
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var name = prefs.getString('name');
  FirebaseAuth auth = FirebaseAuth.instance;
  Algolia algolia = Application.algolia;
  print(auth.currentUser.isAnonymous); // Returns false even if i logged anonymously before
  runApp(ThemeProvider(
    saveThemesOnChange: true,
    themes: [
      AppTheme(
        id: 'white',
        data: constant.whiteTheme,
        description: 'white theme'
      ),
      AppTheme(
        id: 'dark',
        data: constant.darkTheme,
        description: 'dark theme'
      ),
    ],
    child: ThemeConsumer(
      child: Builder(
        builder: (themeContext) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            home: name == null
             ? OnBoardScreen(
                 algolia: algolia,
               )
             : WelcomeScreen(
                 isAnon: prefs.getBool('anon'),
                 algolia: algolia,
             ),
            theme: ThemeProvider.themeOf(themeContext).data,
          );
        }),
      ),
    )
  );
}

主代码

FirebaseAuth auth = FirebaseAuth.instance;
 
  print(auth.currentUser.uid); //returns UID of anon user
  print(auth.currentUser.isAnonymous);//returns false all the time

主代码日志

I/flutter (31830): ci90IxegpvMyq0vGqhtHxqcrrT52
I/flutter (31830): false

所以我可以打印匿名用户的 UID,但 isAnonymous 状态仍然返回 false。

注意:我知道我可以使用 SharedPreferences 处理用户情况,但这是什么原因造成的?如果用户在使用“FirebaseAuth”重新启动应用程序之前匿名登录,我不能得到吗?

解决

通过擦除数据和使用新模拟器解决了问题。

【问题讨论】:

    标签: flutter firebase-authentication


    【解决方案1】:

    当您重新启动应用时,Firebase 必须检查用户是否仍有访问权限(例如:您可能已禁用他们的帐户)。这是一个异步操作,因为它需要调用服务器并且可能需要一些时间才能完成。

    当您的代码访问 FirebaseAuth.instance.currentUser 时,此异步调用可能尚未完成 - 导致 currentUser 仍然是 null

    为确保您始终响应正确的身份验证状态,请使用authentication state 上的文档中所示的身份验证状态侦听器:

    FirebaseAuth.instance
      .authStateChanges()
      .listen((User user) {
        if (user == null) {
          print('User is currently signed out!');
        } else {
          print('User is signed in!');
        }
      });
    

    【讨论】:

    • 我可以打印 currentUser.uid 给我匿名用户的 UID,但 currentUser.isAnonymous 仍然返回 false,
    • stackoverflow.com/questions/66281219/…这个问题其实比较清楚
    • 如果我回答错了问题,而您想澄清您的问题,请编辑 帖子,不要创建新问题
    • 我用我的主要代码和我的代码日志编辑了这个问题,以澄清我的问题,不,你没有回答错误的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多