简体   繁体   English

如何在 Flutter Modular 中设置 RouteGuard 以进行 Firebase 身份验证

[英]how to set up RouteGuard in Flutter Modular for Firebase authentication

currently, the way to check if a user is logged in Flutter Fire as per the documentation ( https://firebase.flutter.dev/docs/auth/usage#authentication-state ):目前,根据文档( https://firebase.flutter.dev/docs/auth/usage#authentication-state )检查用户是否登录 Flutter Fire 的方法:

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

the way to set up a route guard in Flutter Modular as per the documentation ( https://modular.flutterando.com.br/docs/flutter_modular/navegation#route-guard )根据文档( https://modular.flutterando.com.br/docs/flutter_modular/navegation#route-guard )在 Flutter Modular 中设置路由保护的方法

class AuthGuard extends RouteGuard {
  AuthGuard() : super(redirectTo: '/login');

  @override
  Future<bool> canActivate(String path, ModularRoute router) {
    return Modular.get<AuthStore>().isLogged;
  }
}

how do I use this FlutterFire code to create the route guard in Flutter modular?如何使用这个 FlutterFire 代码在 Flutter 模块化中创建路由保护? I have trouble coming up with code that will return a Future from the FlutterFire auth code我无法想出将从 FlutterFire 身份验证代码返回 Future 的代码

try use only this:尝试只使用这个:

Future<bool> checkCurrentUser() async {
    return FirebaseAuth.instance.currentUser != null;
  }

Modular guard required one future of boolean.模块化守卫需要一个布尔值的未来。

 class AuthGuard extends RouteGuard {
  AuthGuard() : super(redirectTo: '/login/');

  @override
  Future<bool> canActivate(String path, ModularRoute route) async {
    
    return await Modular.get<AuthStore>().checkCurrentUser;
  }
}

resolve to me this.向我解决这个问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM