Created
May 8, 2020 18:04
-
-
Save Leocardoso94/cac945c29b5014d00084aa0b5793f128 to your computer and use it in GitHub Desktop.
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 'package:apple_sign_in/apple_sign_in.dart'; | |
| import 'package:firebase_auth/firebase_auth.dart'; | |
| import 'package:flutter/services.dart'; | |
| class AuthService { | |
| final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; | |
| Future<FirebaseUser> signInWithApple() async { | |
| final result = await AppleSignIn.performRequests([ | |
| AppleIdRequest(requestedScopes: [Scope.fullName, Scope.email]) | |
| ]); | |
| switch (result.status) { | |
| case AuthorizationStatus.authorized: | |
| { | |
| final appleIdCredential = result.credential; | |
| final AuthCredential credential = | |
| OAuthProvider(providerId: 'apple.com').getCredential( | |
| accessToken: | |
| String.fromCharCodes(appleIdCredential.authorizationCode), | |
| idToken: String.fromCharCodes(appleIdCredential.identityToken), | |
| ); | |
| final firebaseUser = | |
| (await _firebaseAuth.signInWithCredential(credential)).user; | |
| return firebaseUser; | |
| } | |
| case AuthorizationStatus.error: | |
| { | |
| throw PlatformException( | |
| code: 'ERROR_AUTHORIZATION_DENIED', | |
| message: result.error.toString(), | |
| ); | |
| } | |
| case AuthorizationStatus.cancelled: | |
| throw PlatformException( | |
| code: 'ERROR_ABORTED_BY_USER', | |
| message: 'Sign in aborted by user', | |
| ); | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment