Skip to content

Instantly share code, notes, and snippets.

@diego-lipinski-de-castro
Created July 18, 2020 21:53
Show Gist options
  • Save diego-lipinski-de-castro/c8af7ecbc49129d545f494cf1415a590 to your computer and use it in GitHub Desktop.
Save diego-lipinski-de-castro/c8af7ecbc49129d545f494cf1415a590 to your computer and use it in GitHub Desktop.
Flutte Facebook and Google auth
import 'dart:convert';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_login_facebook/flutter_login_facebook.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
class AuthService {
static final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
static final GoogleSignIn _googleSignIn = GoogleSignIn();
static final FirebaseAnalytics _analytics = FirebaseAnalytics();
static Future<bool> loginWithFacebook() async {
try {
final facebookLogin = FacebookLogin();
// bool isLoggedIn = await facebookLogin.isLoggedIn;
final FacebookLoginResult result = await facebookLogin.logIn(
permissions: [
FacebookPermission.publicProfile,
FacebookPermission.email,
],
);
// await facebookLogin.getProfileImageUrl(width: 80);
// await facebookLogin.getUserEmail();
// await facebookLogin.getUserProfile();
switch (result.status) {
case FacebookLoginStatus.Success:
// result.accessToken.declinedPermissions
// result.accessToken.expires
// result.accessToken.isValid()
// result.accessToken.permissions
// result.accessToken.userId
String token = result.accessToken.token;
final AuthCredential credential =
FacebookAuthProvider.getCredential(accessToken: token);
await _firebaseAuth.signInWithCredential(credential);
await _analytics.logLogin(loginMethod: 'facebook');
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
await sharedPreferences.setBool('logged_in', true);
break;
case FacebookLoginStatus.Cancel:
break;
case FacebookLoginStatus.Error:
print(result.error);
break;
}
return true;
} catch (error) {
return false;
}
}
static Future<bool> loginWithGoogle() async {
try {
final GoogleSignInAccount googleSignInAccount =
await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await _firebaseAuth.signInWithCredential(credential);
await _analytics.logLogin(loginMethod: 'google');
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
await sharedPreferences.setBool('logged_in', true);
return true;
} catch (error) {
return false;
}
}
static isLoggedIn() async {
try {
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
return sharedPreferences.containsKey('logged_in') &&
sharedPreferences.getBool('logged_in') == true;
} catch (error) {
return false;
}
}
}
@mattbara
Copy link

Hey Diego,
I have used your code for my FB auth with this flutter-fb dependency.
But now my Google Sign doesn't work anymore. When I click to sign in, it kicks me out.
I decided to change the google sign and use yours. (as above)
Same issue.

Can you show me the structure of your button onPressed? Maybe there is something in mine which is not suitable.
Thanks

@diego-lipinski-de-castro
Copy link
Author

My button only calls this method, check if the return is true of false in order to redirect the user or show some message, if your project is public, you can link it here and I can check it. First you must check if you correctly setup your project in firebase and gcp, and for facebook, you must configure your app in the facebook developer app, and in the firebase project

@mattbara
Copy link

mattbara commented Aug 10, 2020

I found the issue. It was the info.plist.
By following the instruction from Google & Facebook login, then you will paste a CFBundleURLTypes section for Google and one for Facebook. Only the last one will be picked up. So the FB, in my case, was the one working and did override Google.
If I swapped them around, FB wasn't working anymore but Google was.
The solution is to move the fb?????????????? inside the Google CFBundleURLTypes, and completely remove the facebook one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment