import 'package:firebase_auth/firebase_auth.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
return StreamBuilder(
stream: _auth.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
User? user = snapshot.data as User?;
if (user == null) {
// User is not signed in
return SignInScreen();
} else {
// Check if the user's email is verified
if (user.emailVerified) {
// User is signed in and email is verified, navigate to the homepage
return HomeScreen();
} else {
// User is signed in but email is not verified, show verification screen
return VerifyEmailScreen();
}
}
}
return CircularProgressIndicator();
},
);
字符串 下面是完整的代码:
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: AuthenticationWrapper(),
);
}
}
class AuthenticationWrapper extends StatefulWidget {
@override
_AuthenticationWrapperState createState() => _AuthenticationWrapperState();
}
class _AuthenticationWrapperState extends State<AuthenticationWrapper> {
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _auth.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
User? user = snapshot.data as User?;
if (user == null) {
// User is not signed in
return SignInScreen();
} else {
// Check if the user's email is verified
if (user.emailVerified) {
// User is signed in and email is verified, navigate to the homepage
return HomeScreen();
} else {
// User is signed in but email is not verified, show verification screen
return VerifyEmailScreen();
}
}
}
return CircularProgressIndicator();
},
);
}
}
class SignInScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sign In"),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// Perform sign-in logic here
},
child: Text("Sign In"),
),
),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Center(
child: Text("Welcome to the homepage!"),
),
);
}
}
class VerifyEmailScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Verify Email"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Please verify your email to access the homepage."),
ElevatedButton(
onPressed: () async {
// Send email verification link
User? user = FirebaseAuth.instance.currentUser;
await user?.sendEmailVerification();
},
child: Text("Resend Verification Email"),
),
],
),
),
);
}
}
3条答案
按热度按时间oogrdqng1#
在这里你可以试试这个:
1.Firebase Auth监听:在Flutter应用中设置一个身份验证状态监听器。该监听器应在“验证您的电子邮件”页面处于活动状态。
1.检查邮件验证状态:在监听器中,检查用户的邮件是否通过验证,可以通过检查Firebase用户对象的
emailVerified
属性来完成。1.重定向到主页:如果
emailVerified
属性为true,则使用Flutter的导航器将用户重定向到主页。此重定向应自动进行,无需任何用户交互。下面是执行此操作的代码片段:
字符串
它监听身份验证状态的变化。当用户的电子邮件通过验证时,它会自动导航到主页。我希望它能正常工作。😁
uqzxnwby2#
此代码将向用户的电子邮件地址发送验证电子邮件。电子邮件将包含用户必须单击以验证其电子邮件地址的链接。
字符串
此代码将自动重定向到电子邮件验证后的网页.
型
jfgube3f3#
下面是逻辑代码或更短的代码:
字符串
下面是完整的代码:
型