我在 flutter web 中开发了这个应用程序,我使用 firebase 进行身份验证,并且我有这个身份验证门,它将检查用户是否有会话,如果有,则重定向到仪表板,如果没有,则移动到登录页面。
但问题是这个Auth Gate不起作用,这里是Auth Gate的代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:rafay_portfolio/frontend/admin/pages/dashboardPage.dart';
import 'package:rafay_portfolio/frontend/admin/pages/auth/loginPage.dart';
class AuthGate extends StatelessWidget {
const AuthGate({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return const DashboardPage();
} else {
return const LoginAdmin();
}
},
),
);
}
}
我也使用带有 opt 的登录电子邮件,每次刷新此页面时,身份验证门不会检查会话并始终重定向到登录页面。
这是使用 Firebase 登录的代码:
// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:rafay_portfolio/frontend/admin/pages/auth/opt.dart';
import 'package:rafay_portfolio/frontend/admin/pages/dashboardPage.dart';
import 'package:rafay_portfolio/constants/widgets/dialogBox/dialogbox.dart';
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<UserCredential?> signInWithEmailAndPassword(
BuildContext context, String email, String password) async {
try {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
return userCredential;
} on FirebaseAuthException catch (e) {
showDialogBox(
context,
Icons.error,
Colors.red,
Colors.red,
'Error',
e.message ?? 'An error occurred.',
() => Navigator.of(context).pop(),
);
return null;
}
}
Future<void> handleLogin(BuildContext context, String email, String password,
String phoneNumber) async {
UserCredential? userCredential =
await signInWithEmailAndPassword(context, email, password);
// If the user is authenticated, start the phone number verification process
if (userCredential != null && userCredential.user != null) {
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: (PhoneAuthCredential credential) async {
await FirebaseAuth.instance.signInWithCredential(credential);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const DashboardPage(),
),
);
},
verificationFailed: (FirebaseAuthException e) {
showDialogBox(
context,
Icons.error,
Colors.red,
Colors.red,
'Error',
e.message ?? 'An error occurred.',
() => Navigator.of(context).pop(),
);
},
codeSent: (String verificationId, int? resendToken) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OtpPage(
verificationId: verificationId,
),
),
);
},
codeAutoRetrievalTimeout: (String verificationId) {
showDialogBox(
context,
Icons.error,
Colors.red,
Colors.red,
'Error',
'Sorry OTP Expired',
() => Navigator.of(context).pop(),
);
},
);
}
}
Future<void> signOut() async {
await _auth.signOut();
}
}
该项目托管在github上,链接为:https://github.com/rafay99-epic/Portfolio
所有更改都在 V1 中,我也有一个拉取请求。
查看代码库或帮助我识别问题。
我也有同样的问题,但是在flutter中,你能解决吗?