xamarin / GoogleApisForiOSComponents

MIT License
225 stars 162 forks source link

[Bug/Request] Do not have any auth provider for Sign In with Apple. #477

Closed divyesh008 closed 3 years ago

divyesh008 commented 3 years ago

I have integrated Google login using Firebase in my mobile application. When I have submit the app to app store for review it has got rejected, because I have not implemented Login with Apple.

According to apple's new policy from iOS 13 if we provide any 3rd party login in app then we also have to provide login with apple option, it's mandatory. So I am trying to perform login with Apple using Firebase from my Xamarin.iOS project. I have used Xamarin.Firebase.iOS.Auth plugin version (6.9.2) but in that it does not have a option to login with Apple in that.

How I can achieve that in xamarin.iOS without Apple login provider?

Screenshot 2021-03-17 at 11 26 19 AM

divyesh008 commented 3 years ago

I have got the solution in the last. To register a user in Firebase after successful sign in with apple we can use this method.

[Export("authorizationController:didCompleteWithAuthorization:")]
public void DidComplete(ASAuthorizationController controller, ASAuthorization authorization)
{
    if (authorization.GetCredential<ASAuthorizationAppleIdCredential>() is ASAuthorizationAppleIdCredential appleIdCredential)
    {
        var userIdentifier = appleIdCredential.User;
        var fullName = appleIdCredential.FullName;
        var email = appleIdCredential.Email;
        var token = appleIdCredential.IdentityToken.ToString();

        var credentials = OAuthProvider.GetCredential("apple.com", IdToken: token, null);
        Auth.DefaultInstance.SignInWithCredential(credentials, this.HandleAuthDataResult);
    }
}

HandleAuthDataResult

private async void HandleAuthDataResult(AuthDataResult authResult, NSError error)
{
    try
    {
        if (error != null)
        {
            AuthErrorCode errorCode;
            if (IntPtr.Size == 8)
            {
                errorCode = (AuthErrorCode)((long)error.Code); // 64 bits devices
            }
            else
            {
                errorCode = (AuthErrorCode)((int)error.Code); // 32 bits devices
            }

            // Posible error codes that SignIn method with email and password could throw
            // Visit https://firebase.google.com/docs/auth/ios/errors for more information
            switch (errorCode)
            {
                case AuthErrorCode.OperationNotAllowed:
                case AuthErrorCode.InvalidEmail:
                case AuthErrorCode.UserDisabled:
                case AuthErrorCode.WrongPassword:
                default:
                    // Print error
                    break;
            }
            return;
        }
        else
        {
            if (authResult != null && authResult.User != null)
            {
                if (authResult.AdditionalUserInfo.IsNewUser)
                {
                    // Save the user details in your app where you want to store it.
                }
                else
                {
                    // User is already registered just get his details from firebase db

                }
            }
            // Write your logic here for the navigtion after successful login
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}