问题描述
我使用 Firebase Auth 和 Google 登录 Flutter.我可以登录,但是当我关闭应用程序(杀死它)时,我必须重新注册.那么有没有一种方法可以将用户身份验证持久化,直到用户专门注销?这是我的身份验证课程
import 'package:firebase_auth/firebase_auth.dart';导入包:google_sign_in/google_sign_in.dart";类认证{FirebaseAuth _firebaseAuth;FirebaseUser _user;认证(){this._firebaseAuth = FirebaseAuth.instance;}未来isLoggedIn() 异步 {this._user = await _firebaseAuth.currentUser();如果(this._user == null){返回假;}返回真;}未来authenticationWithGoogle() 异步 {最终 googleSignIn = GoogleSignIn();最终 GoogleSignInAccount googleUser = 等待 googleSignIn.signIn();最终的 GoogleSignInAuthentication googleAuth =等待 googleUser.authentication;this._user = await _firebaseAuth.signInWithGoogle(accessToken: googleAuth.accessToken,idToken: googleAuth.idToken,);如果(this._user == null){返回假;}返回真;//对登录用户做一些事情}}
这是调用身份验证检查的起始页.
import 'package:flutter/material.dart';导入 'auth.dart';导入 'login_screen.dart';导入 'chat_screen.dart';类 Splash 扩展了 StatefulWidget {@覆盖_Splash createState() =>_溅();}class _Splash 扩展 State{@覆盖小部件构建(BuildContext 上下文){返回脚手架(身体:中心(孩子:CircularProgressIndicator(值:空,),),);}@覆盖无效的初始化状态(){super.initState();_handleStartScreen();}未来<void>_handleStartScreen() 异步 {Auth _auth = Auth();如果(等待_auth.isLoggedIn()){Navigator.of(context).pushReplacementNamed("/chat");}Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => LoginScreen(auth: _auth,)));}}
我相信您的问题是路由.在我的应用程序中,我使用 FirebaseAuth
并且它按照您说的那样工作,并且我不保留任何登录令牌.但是,我不知道为什么您使用 getUser 的方法不起作用.
尝试调整您的代码以使用 onAuthStateChanged
.
基本上,在您的 MaterialApp
上,创建一个 StreamBuilder
监听 _auth.onAuthStateChanged
并根据 Auth 状态选择您的页面.>
我将复制并粘贴我的应用程序的部分内容,以便您有一个想法:
[...]最终 FirebaseAuth _auth = FirebaseAuth.instance;未来<void>主()异步{FirebaseApp.configure(名称: '...',选项:平台.isIOS?const FirebaseOptions(...): const FirebaseOptions(...),);[...]运行应用程序(新材料应用程序(标题: '...',主页:等待 getLandingPage(),主题:ThemeData(...),));}未来getLandingPage() 异步 {返回 StreamBuilder(流:_auth.onAuthStateChanged,构建器:(BuildContext 上下文,快照){如果 (snapshot.hasData && (!snapshot.data.isAnonymous)) {返回主页();}返回 AccountLoginPage();},);}
I am using Firebase Auth with google sign in Flutter. I am able to sign in however when I close the app(kill it), I have to sign up all over again. So is there a way to persist user authentication till specifically logged out by the user?Here is my auth class
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
class Auth {
FirebaseAuth _firebaseAuth;
FirebaseUser _user;
Auth() {
this._firebaseAuth = FirebaseAuth.instance;
}
Future<bool> isLoggedIn() async {
this._user = await _firebaseAuth.currentUser();
if (this._user == null) {
return false;
}
return true;
}
Future<bool> authenticateWithGoogle() async {
final googleSignIn = GoogleSignIn();
final GoogleSignInAccount googleUser = await googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
this._user = await _firebaseAuth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
if (this._user == null) {
return false;
}
return true;
// do something with signed-in user
}
}
Here is my start page where the auth check is called.
import 'package:flutter/material.dart';
import 'auth.dart';
import 'login_screen.dart';
import 'chat_screen.dart';
class Splash extends StatefulWidget {
@override
_Splash createState() => _Splash();
}
class _Splash extends State<Splash> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CircularProgressIndicator(
value: null,
),
),
);
}
@override
void initState() {
super.initState();
_handleStartScreen();
}
Future<void> _handleStartScreen() async {
Auth _auth = Auth();
if (await _auth.isLoggedIn()) {
Navigator.of(context).pushReplacementNamed("/chat");
}
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => LoginScreen(auth: _auth,)));
}
}
I believe your problem is routing. In my apps I use FirebaseAuth
and it works just as you say you wanted to, and I don't persist any login token. However, I don't know why your approach of using a getUser is not working.
Try to adjust your code to use onAuthStateChanged
.
Basically, on your MaterialApp
, create a StreamBuilder
listening to _auth.onAuthStateChanged
and choose your page depending on the Auth status.
I'll copy and paste parts of my app so you can have an idea:
[...]
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<void> main() async {
FirebaseApp.configure(
name: '...',
options:
Platform.isIOS
? const FirebaseOptions(...)
: const FirebaseOptions(...),
);
[...]
runApp(new MaterialApp(
title: '...',
home: await getLandingPage(),
theme: ThemeData(...),
));
}
Future<Widget> getLandingPage() async {
return StreamBuilder<FirebaseUser>(
stream: _auth.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.hasData && (!snapshot.data.isAnonymous)) {
return HomePage();
}
return AccountLoginPage();
},
);
}
这篇关于持久用户身份验证 Flutter Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!