使用onAuthStateChanged使用Flutter登录Firebase [英] Firebase Login with Flutter using onAuthStateChanged
问题描述
在Flutter之外,当我实现Firebase身份验证时,我总是使用firebase提供的onAuthStateChanged侦听器来确定用户是否登录并做出相应的响应。
Outside of Flutter, when I implement firebase authentication I always use the onAuthStateChanged listener provided by firebase to determine if the user is logged in or not and respond accordingly.
我正在尝试使用flutter做类似的事情,但是我可以找到一种访问Firebase的onAuthStateChanged的方法。我正在使用firebase_auth和google_signin Flutter插件。我正在研究firebase_auth Flutter插件随附的示例代码。下面是示例代码。我可以使用google登录成功登录,但是示例太简单了,因为我想让观察者/侦听器检测用户的登录/退出状态。
I am trying to do something similar using flutter, but I can find a way to access onAuthStateChanged of Firebase. I am using the firebase_auth, and google_signin Flutter plugins. I am working of example code that is included with the firebase_auth Flutter plugin. Below is the sample code. I can login successfully with google sign in, but the example is too simple, because I want to have an observer/listener to detect the user's signed in/out state.
有没有一种方法可以使用firebase_auth / google_signin flutter插件通过观察者/侦听器进行检测,以确定用户的状态?
Is there a way to detect via observer/listener using the firebase_auth/google_signin flutter plugins to determine the status of a user?
我最终希望应用程序确定用户是否登录(是/否)。如果没有,则显示登录屏幕,如果是,则显示我的主应用程序页面。
Ultimately I want the app to determine if the user is logged in (yes/no). If not then show a login screen, if yes then show my main app page.
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = new GoogleSignIn();
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Firebase Auth Demo',
home: new MyHomePage(title: 'Firebase Auth Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<String> _message = new Future<String>.value('');
Future<String> _testSignInAnonymously() async {
final FirebaseUser user = await _auth.signInAnonymously();
assert(user != null);
assert(user == _auth.currentUser);
assert(user.isAnonymous);
assert(!user.isEmailVerified);
assert(await user.getToken() != null);
if (Platform.isIOS) {
// Anonymous auth doesn't show up as a provider on iOS
assert(user.providerData.isEmpty);
} else if (Platform.isAndroid) {
// Anonymous auth does show up as a provider on Android
assert(user.providerData.length == 1);
assert(user.providerData[0].providerId == 'firebase');
assert(user.providerData[0].uid != null);
assert(user.providerData[0].displayName == null);
assert(user.providerData[0].photoUrl == null);
assert(user.providerData[0].email == null);
}
return 'signInAnonymously succeeded: $user';
}
Future<String> _testSignInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final FirebaseUser user = await _auth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getToken() != null);
return 'signInWithGoogle succeeded: $user';
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new MaterialButton(
child: const Text('Test signInAnonymously'),
onPressed: () {
setState(() {
_message = _testSignInAnonymously();
});
}),
new MaterialButton(
child: const Text('Test signInWithGoogle'),
onPressed: () {
setState(() {
_message = _testSignInWithGoogle();
});
}),
new FutureBuilder<String>(
future: _message,
builder: (_, AsyncSnapshot<String> snapshot) {
return new Text(snapshot.data ?? '',
style: const TextStyle(
color: const Color.fromARGB(255, 0, 155, 0)));
}),
],
),
);
}
}
以下是有关有问题的软件包的链接:
https://github.com/flutter/plugins/tree/master/ packages / firebase_auth
https://github.com/ flutter / plugins / tree / master / packages / google_sign_in
Here are links to the flutter packages in question: https://github.com/flutter/plugins/tree/master/packages/firebase_auth https://github.com/flutter/plugins/tree/master/packages/google_sign_in
推荐答案
我知道这个问题已经很老了,但是这里
I know this question is pretty old, but here is the answer if anybody is still looking for it.
Firebase返回流
的是答案FirebaseUser
及其 onAuthStateChanged
函数。有很多方法可以监听用户的身份验证状态更改。这是我的方法:
Firebase returns a Stream
of FirebaseUser
with it's onAuthStateChanged
function. There are many ways to listen to the user's authentication state change. This is how I do it:
我返回了 StreamBuilder
到我的应用程序的主页,然后 StreamBuilder
根据用户的身份验证状态返回特定页面。
I return a StreamBuilder
to my App's home page, and the StreamBuilder
returns specific pages based on the auth status of the user.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Your App Name',
home: _getLandingPage()
);
}
Widget _getLandingPage() {
return StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data.providerData.length == 1) { // logged in using email and password
return snapshot.data.isEmailVerified
? MainPage()
: VerifyEmailPage(user: snapshot.data);
} else { // logged in using other providers
return MainPage();
}
} else {
return LoginPage();
}
},
);
}
解决方案2
您也可以在应用程序的 initState()
函数中创建一个侦听器。
Solution 2
You can create a listener in your app's initState()
function as well.
@override
void initState() {
super.initState();
FirebaseAuth.instance.onAuthStateChanged.listen((firebaseUser) {
// do whatever you want based on the firebaseUser state
});
}
这篇关于使用onAuthStateChanged使用Flutter登录Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!