坚持用户Auth Flutter Firebase [英] Persist user Auth Flutter Firebase
问题描述
我正在将Firebase Auth与Google的Flutter一起使用.我可以登录,但是当我关闭该应用程序(杀死它)时,我必须再次重新注册.那么,有没有一种方法可以坚持用户身份验证,直到用户明确注销为止? 这是我的认证课程
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
}
}
这是我的起始页,其中会调用auth检查.
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,)));
}
}
推荐答案
我相信您的问题正在路由.在我的应用程序中,我使用FirebaseAuth
,它可以按照您所说的那样工作,并且我不会保留任何登录令牌.但是,我不知道为什么您使用getUser的方法行不通.
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.
尝试调整您的代码以使用onAuthStateChanged
.
Try to adjust your code to use onAuthStateChanged
.
基本上,在您的MaterialApp
上,创建一个StreamBuilder
来监听_auth.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();
},
);
}
这篇关于坚持用户Auth Flutter Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!