0

预期流程:仅在用户验证其电子邮件时显示主页。实际发生的情况:用户可以弹出导航堆栈以显示主页。

我正在使用 Firebase 来处理我的身份验证流程,目前正在使用 StreamBuilder 在应用程序的根目录中显示登录页面或主页,具体取决于用户是否经过验证且不为空。

//create user object based on Firebase User
  UserApp _userFromFirebaseUser(User user) {
    return user != null ? UserApp(uid: user.uid, isVerified: user.emailVerified):null;
  }

//userapp type class
class UserApp {
  final String uid;
  final bool isVerified;
  UserApp({this.isVerified, this.uid});
}

然后我有一个 StreamBuilder,它位于我的 main.dart 文件之上:

class AuthWidgetBuilder extends StatelessWidget {
  const AuthWidgetBuilder({Key key, @required this.builder}) : super(key: key);
  final Widget Function(BuildContext, AsyncSnapshot<UserApp>) builder;

  @override
  Widget build(BuildContext context) {
    final authService = Provider.of<AuthService>(context, listen: false);
    return StreamBuilder<UserApp>(
        stream: authService.onAuthStateChanged,
        builder: (context, snapshot) {
          final UserApp user = snapshot.data;
          //only show home page if user is non null and also if they verified their email.
          if (user != null && authService.currentUser().isVerified == true) {
            return MultiProvider(
              providers: [
                Provider<UserApp>.value(value: user),
              ],
              child: builder(context, snapshot),
            );
          }
          return builder(context, snapshot);
        });
  }
}

class AuthWidget extends StatelessWidget {
  const AuthWidget({Key key, @required this.userSnapshot}) : super(key: key);
  final AsyncSnapshot<UserApp> userSnapshot;

  @override
  Widget build(BuildContext context) {
    if (userSnapshot.connectionState == ConnectionState.active) {
      return userSnapshot.hasData ? HomePage() : LandingPage();
    }
    return Scaffold(
      body: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

然后这是我的 main.dart 文件,其中 StreamBuilder 和 AuthService 类位于小部件树的顶部:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Provider<AuthService>(
        create: (_) => AuthService(),
        child: AuthWidgetBuilder(builder: (context, userSnapshot) {
          return MaterialApp(
            home: AuthWidget(userSnapshot: userSnapshot),
          );
        }));
  }

为什么我可以弹出导航堆栈以显示主页,即使我还没有通过电子邮件验证用户?在验证他们的电子邮件后,我需要在我的代码中进行哪些更改以确保用户只能看到主页?有什么资源可以让我更好地理解这些概念吗?

4

0 回答 0