Skip to content

Instantly share code, notes, and snippets.

@Leocardoso94
Created May 8, 2020 18:04
Show Gist options
  • Select an option

  • Save Leocardoso94/cac945c29b5014d00084aa0b5793f128 to your computer and use it in GitHub Desktop.

Select an option

Save Leocardoso94/cac945c29b5014d00084aa0b5793f128 to your computer and use it in GitHub Desktop.
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