How to signout of Google auth in firebase Flutter? - firebase

These are my login and logout functions using firebase auth in flutter,The sign is working perfectly but the signout is not working, I tried to print the 'signout text' in debug console and its printing but then the navigation to SignIn page part is not working.
Future<FirebaseUser> signInWithGoogle() async {
try{GoogleSignInAccount googleUser = await _googleSignIn.signIn();
// Step 2
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential cred= GoogleAuthProvider.getCredential(idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);
FirebaseUser user = (await _auth.signInWithCredential(cred)).user;
return user;} catch(e){
print(e.toString());
return null;
}
}
signOutGoogle() async{
await _auth.signOut();
await _googleSignIn.signOut();
print('signed out');
}
This is my signout button implementation:
FlatButton.icon(
onPressed: () async => {
Navigator.pushNamed(context, '/signin'),
_auth.signOutGoogle(),
},
icon: Icon(Icons.exit_to_app),
label: Text(
'Sign out',
style: GoogleFonts.abel(color: Colors.black),
),
)

switch the 2 methods and await the signOutGoogle():
onPressed: () async {
await _auth.signOutGoogle(),
Navigator.pushNamed(context, '/signin'),
},

Related

The method 'getCredential' isn't defined for the type 'PhoneAuthProvider'

As far as I can see, there are numerous deprecated methods in flutter firebase authentication.
here is my code;
FlatButton(
child: Text("Confirm"),
textColor: Colors.white,
color: Colors.blue,
onPressed: () async {
FirebaseAuth _auth = FirebaseAuth.instance; // I didn't use it here. I did for you to see
final code = _codeController.text.trim();
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId, smsCode: code);
UserCredential result =
await _auth.signInWithCredential(credential);
User user = result.user;
if (user != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
HomeScreen(user: user)));
} else {
print("Error");
}
},
)
I try to do phone auth but getCredential has error. I looked at firebase documentation. it says that PhoneAuthProvider deprecated and I should use PhoneAuthCredential with getCredential. I used it but it didn't work. I saw that there are various question about this error on stackoverflow but I can't find workaround yet
This is how I implemented it in my app on firebase_auth: ^0.18.0+1:
#override
Future<void> signInWithSmsCode(String smsCode) async {
final AuthCredential authCredential = PhoneAuthProvider.credential(
smsCode: smsCode,
verificationId: _verificationCode,
);
try {
await auth.signInWithCredential(authCredential);
} on PlatformException catch (e) {
throw _firebaseErrorFactory.getException(e.code);
} on FirebaseAuthException {
throw _firebaseErrorFactory
.getException('ERROR_INVALID_VERIFICATION_CODE');
}
}

Cannot sign-in after signing out - Firebase Google Signin with Flutter

I am able to sign-in and sign-out using Google. However, when I sign back in, I am not redirected to the page that I have specified. It get's stuck on the Login Page.
Here's the onPressed method for the login button:
onPressed: () async {
await Provider.of<Auth>(context, listen: false).signInWithGoogle();
},
The signInWithGoogle() method is in the Auth class below.
Here's the Auth class:
class Auth with ChangeNotifier {
String _token;
String _userId;
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
bool get isAuth {
return token != null;
}
String get token {
if(_token != null)
return _token;
return null;
}
String get userId {
return _userId;
}
Future<void> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final UserCredential authResult =
await _auth.signInWithCredential(credential);
final User user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final User currentUser = _auth.currentUser;
assert(user.uid == currentUser.uid);
print('User is ' + user.displayName);
_token = await currentUser.getIdToken();
print('Token is ' + _token);
_userId = currentUser.uid;
notifyListeners();
}
Future<void> signOutGoogle() async {
await googleSignIn.signOut();
_token = null;
_userId = null;
notifyListeners();
print("User Sign Out");
}
Future<bool> tryAutoLogin() async {
//will implement later
return false;
}
}
Here's the main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Auth(),
),
ChangeNotifierProxyProvider<Auth,Categories>(
create:null,
update:(ctx,auth, previousCategories) => Categories(auth.token, auth.userId)),
],
//The consumer ensures that the material app gets built whenever Auth object changes
child: Consumer<Auth>(builder: (ctx, auth, _) =>
MaterialApp(
title: 'MyShop',
theme: ThemeData(
textTheme: Theme.of(context).textTheme.apply(
bodyColor: Colors.black,
displayColor: Colors.black,
),
primaryColor: Colors.orange,
accentColor: Colors.deepOrange,
fontFamily: 'Lato',
),
home: auth.isAuth ? CategoriesScreen()
: FutureBuilder(
future: auth.tryAutoLogin(),
builder: (ctx, authResultSnapshot) =>
authResultSnapshot.connectionState ==
ConnectionState.waiting
? SplashScreen()
: LoginPage(),
),
routes: {
CategoriesScreen.routeName: (ctx) => CategoriesScreen(),
LoginPage.routeName: (ctx) => LoginPage()
}
),
)
);
}
}
Here's the logout button:
ListTile(
leading: Icon(Icons.exit_to_app),
title: Text('Logout'),
onTap: () {
Provider.of<Auth>(context, listen:false).signOutGoogle();
Navigator.of(context).pushReplacementNamed(LoginPage.routeName);
},
),
When I sign-in for the first time, I get redirected to the CategoriesScreen() page. However, when log out and log back in, I do not get redirected.

Can some help me with this code? Google Sign in Firebase (Flutter) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
when I select the account I want to login with, it fails. I'm confuse to whats the issue, I followed the tutorial step by step. except for this code:
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
FirebaseUser firebaseUser = await firebaseAuth.signInWithCredential(credential);
because signInWithGoogle wasn't working.
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'home_everything.dart';
class login extends StatefulWidget {
#override
_loginState createState() => _loginState();
}
class _loginState extends State<login> {
final GoogleSignIn googleSignIn = new GoogleSignIn();
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
SharedPreferences preferences;
bool loading = false;
bool isLogedin = false;
#override
void initState(){
super.initState();
isSignedIn();
}
void isSignedIn() async{
setState(() {
loading = true;
});
preferences = await SharedPreferences.getInstance();
isLogedin = await googleSignIn.isSignedIn();
if(isLogedin){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => SongHome()));
}
setState(() {
loading = false;
});
}
Future handleSignIn() async{
preferences = await SharedPreferences.getInstance();
setState(() {
loading = true;
});
GoogleSignInAccount googleUser = await googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
FirebaseUser firebaseUser = await firebaseAuth.signInWithCredential(credential);
if(firebaseUser != null) {
final QuerySnapshot result = await Firestore.instance.collection("users")
.where("id", isEqualTo: firebaseUser.uid)
.getDocuments();
final List<DocumentSnapshot> documents = result.documents;
if (documents.length == 0) {
// insert the user into out collection
Firestore.instance.collection("users")
.document(firebaseUser.uid)
.setData({
"id": firebaseUser.uid,
"username": firebaseUser.displayName,
"profilePicture": firebaseUser.photoUrl
});
await preferences.setString("id", firebaseUser.uid);
await preferences.setString("username", firebaseUser.displayName);
await preferences.setString("photoUrl", firebaseUser.displayName);
} else {
await preferences.setString("id", documents[0]['id']);
await preferences.setString("username", documents[0]['username']);
await preferences.setString("photoUrl", documents[0]['photoUrl']);
}
Fluttertoast.showToast(msg: "Logged In");
setState(() {
loading = false;
});
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => SongHome()));
}else{
Fluttertoast.showToast(msg: "Login failed : (");
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
centerTitle: true,
title: new Text("Login", style: TextStyle(color: Colors.redAccent),),
elevation: 0.5,
),
body: Stack(
children: <Widget>[
Center(
child: FlatButton(onPressed: (){
handleSignIn();
},
child: Text("Sign In"),),
),
Visibility(
visible: loading ?? true,
child: Container(
color: Colors.white.withOpacity(0.7),
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.red),
),
),
)
],
),
);
}
}
Here is the error log:
E/flutter (15402): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)
try this,
FirebaseUser firebaseUser = await firebaseAuth.signInWithCredential(credential).user;

I am not able to navigate from homepage to loginpage after clicking on sign out button

I have created sign in application with google authentication using flutter. I am not able to enter into homepage after clicking on Login button, and I had once logged in to homepage, when i create the authentication code in the loginpage itself. but now I am using another file for authentication named auth.dart and I have called the function from there to loginpage and homepage(where signout button is there). please help me and tell me how to go to homepage and from homepage vice versa
//Here is my code for loginpage.dart
//button for login with google
RaisedButton(
child: Text('Login with Google'),
color: Colors.orange,
textColor: Colors.white,
elevation: 7.0,
onPressed: () { authService.googleSignIn();
}
),
Here is my Code for authentication in auth.dart
class AuthService {
final GoogleSignIn _googleSignIn= GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
Observable<FirebaseUser> user;
Observable<Map<String, dynamic>> profile;
PublishSubject loading = PublishSubject();
AuthService() {
if(_auth.currentUser()!= null) {
print('Please Sign in');
} else {
googleSignIn();
}
}
Future<FirebaseUser> googleSignIn() async{
loading.add(true);
//GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
idToken: googleAuth.idToken,
accessToken: googleAuth.accessToken,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
print("User Name: ${user.displayName}");
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
}
void SignOut() {
_auth.signOut();
}
}
final AuthService authService = AuthService();
and Finally here is my code for Homepage.dart where SignOut button is
MaterialButton(
onPressed: () {
authService.SignOut();
},
child: Text('Log Out'),
)
I have tried Navigator.of(context).pushReplacementNamed('landingpage');
but it says undefined name 'context'
please help me out
RaisedButton(
child: Text('Login with Google'),
color: Colors.orange,
textColor: Colors.white,
elevation: 7.0,
onPressed: () {
authService.googleSignIn().then((firebaseUser){
Navigator.of(context).pushReplacementNamed('landingpage');
});
}
)

flutter firebase login not redirect after signInWithCredential

I'm using flutter and firebase auth with google sign in.
specifically I'm referring to this Tutorial
but, in that case no redirect after login success.
in my case, i want to redirect after login success.
I have made like this on AuthService
Future<FirebaseUser> googleSignIn() async {
loading.add(true);
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
FirebaseUser user = await _auth.signInWithCredential(credential);
updateUserData(user);
print("signed in : " + user.displayName);
loading.add(false);
return user;
}
initState
void initState() {
super.initState();
authService.profile.listen((state) {
print("ini statenya $state");
});
}
and button
CupertinoButton(
color: const Color(0xFFdd4b39),
child: const Text('Google'),
onPressed: () => authService.googleSignIn().then((user) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BeforeDashboard(
uuid: user,
)),
);
})),
But, it only work when i use debug mode. and not redirect on release mode.
After choose account popup will be gone but not redirect on dashboard

Resources