Last active
October 20, 2019 17:14
-
-
Save stargazing-dino/8bbc37556c1b899a24ec989498f8d0e2 to your computer and use it in GitHub Desktop.
An example of dealing with auth methods in Flutter with FB and Google sign in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:appname/services/firestore.dart'; | |
import 'package:appname/utils/get_status_code_error.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter_facebook_login/flutter_facebook_login.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ['email']); | |
final auth = FirebaseAuth.instance; | |
final _facebookLogin = FacebookLogin(); | |
Future<void> signOut() async { | |
await Future.wait([ | |
_googleSignIn.signOut(), | |
auth.signOut(), | |
_facebookLogin.logOut(), | |
]); | |
} | |
Future<void> facebookSignIn() async { | |
try { | |
final FacebookLoginResult result = await _facebookLogin.logIn(['email']); | |
if (result.status == FacebookLoginStatus.cancelledByUser) | |
throw 'User Canceled Permissions'; | |
if (result.status == FacebookLoginStatus.error) | |
throw 'Error Authenticating User'; | |
final accessToken = result.accessToken.token; | |
final AuthCredential credential = FacebookAuthProvider.getCredential( | |
accessToken: accessToken, | |
); | |
await auth.signInWithCredential(credential).then((authResult) { | |
updateUserData(authResult.user); | |
}); | |
} catch (error) { | |
throw _errorToHumanReadable(error.toString()); | |
} | |
} | |
Future<void> googleSignIn() async { | |
try { | |
GoogleSignInAccount googleUser = await _googleSignIn.signIn(); | |
if (googleUser == null) throw 'User Canceled Permissions'; | |
GoogleSignInAuthentication googleAuth = await googleUser.authentication; | |
final signInMethods = | |
await auth.fetchSignInMethodsForEmail(email: googleUser.email); | |
if (signInMethods.length != 0 && !signInMethods.contains('google.com')) | |
throw 'An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.'; | |
final AuthCredential credential = GoogleAuthProvider.getCredential( | |
accessToken: googleAuth.accessToken, | |
idToken: googleAuth.idToken, | |
); | |
await auth.signInWithCredential(credential).then((authResult) { | |
updateUserData(authResult.user); | |
}); | |
} catch (error) { | |
throw _errorToHumanReadable(error.toString()); | |
} | |
} | |
Future<void> anonymousSignIn() async { | |
try { | |
await auth.signInAnonymously(); | |
} catch (error) { | |
throw _errorToHumanReadable(error.toString()); | |
} | |
} | |
/// | |
/// Maps an error string to typical `developer` readable error messages. | |
/// See [getStatusCodeError]. | |
String _errorToHumanReadable(String errorMessage) { | |
final exp = RegExp(r':\s([0-9]+):'); | |
final match = exp.firstMatch(errorMessage); | |
if (match == null) { | |
return errorMessage; | |
} else { | |
final group = match.group(1); | |
final statusCode = int.tryParse(group) ?? 99; | |
return getStatusCodeError(statusCode: statusCode); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment