Я ввел в Google регистрацию firebase auth, но как я могу оставаться в системе после закрытия приложения, я даже поставил эту строку await _auth.currentUser()
, но все еще не работает. Ниже мой код входа в систему.
LoginPage.dart (СТАРЫЙ)
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
//Google provider
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart';
import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = new GoogleSignIn();
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
String _email;
String _password;
GoogleSignInAccount _currentUser;
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account)
{
setState(() {
_currentUser = account;
});
if (_currentUser != null) {
//_handleGetContact();
}
});
_googleSignIn.signInSilently();
}
//google sign in
GoogleSignIn googleAuth = new GoogleSignIn();
//Facebook sign in
FacebookLogin fbLogin = new FacebookLogin();
Future<FirebaseUser> _testSignInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
print('Signed in as ${user.displayName}');
Navigator.of(context).pushReplacementNamed('/homepage');
return await _auth.currentUser();
}
void _facebookLogin() async {
var facebookLogin = FacebookLogin();
var facebookLoginResult =
await facebookLogin.logInWithReadPermissions(['email',
'public_profile']);
switch (facebookLoginResult.status) {
case FacebookLoginStatus.error:
print("Error");
onLoginStatusChanged(false);
break;
case FacebookLoginStatus.cancelledByUser:
print("CancelledByUser");
onLoginStatusChanged(false);
break;
case FacebookLoginStatus.loggedIn:
print("LoggedIn");
onLoginStatusChanged(true);
FacebookAccessToken myToken = facebookLoginResult.accessToken;
AuthCredential credential=
FacebookAuthProvider.getCredential(accessToken: myToken.token);
try {
final FirebaseUser user = await
FirebaseAuth.instance.signInWithCredential(credential);
//Navigator.pushReplacement(context, MaterialPageRoute(builder:
(context) => DiscoverPage()));
print('Signed in as ${user.displayName}');
Navigator.of(context).pushReplacementNamed('/homepage');
} catch (e) {
debugPrint("Facebook signin error: " + e.toString());
}
break;
}
}
bool isLoggedIn = false;
void onLoginStatusChanged(bool isLoggedIn) {
setState(() {
this.isLoggedIn = isLoggedIn;
});
}
}
}
LoginPage.dart (NEW)
class _LoginPageState extends State<LoginPage> {
bool loading = false;
bool isLoggedIn = false;
void onLoginStatusChanged(bool isLoggedIn) {
setState(() {
this.isLoggedIn = isLoggedIn;
});
}
@override
void initState() {
super.initState();
isSignedIn();
}
void isSignedIn() async {
setState(() {
loading = true;
});
isLoggedIn = await _googleSignIn.isSignedIn();
if (isLoggedIn) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => HomePage()));
}
setState(() {
loading = false;
});
}
}
Я должен добавить этот код, чтобы оставаться в системе. Кстати, как я могу сделать то же самое для Facebook?