From 72818626729eb666b4d23bf7e16645f0d215bbe5 Mon Sep 17 00:00:00 2001 From: Oleksandr Liakhevych Date: Sun, 2 Feb 2025 20:58:33 +0200 Subject: [PATCH] Update Auth.Google to use AdamE.Google.iOS.SignIn --- src/Auth.Google/Auth.Google.csproj | 2 +- .../iOS/FirebaseAuthGoogleImplementation.cs | 27 ++++++++++--------- src/Auth.Google/Platforms/iOS/GoogleAuth.cs | 16 +++++------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/Auth.Google/Auth.Google.csproj b/src/Auth.Google/Auth.Google.csproj index 0872930..cf28412 100644 --- a/src/Auth.Google/Auth.Google.csproj +++ b/src/Auth.Google/Auth.Google.csproj @@ -90,7 +90,7 @@ - + diff --git a/src/Auth.Google/Platforms/iOS/FirebaseAuthGoogleImplementation.cs b/src/Auth.Google/Platforms/iOS/FirebaseAuthGoogleImplementation.cs index 1a41c6b..7532c22 100644 --- a/src/Auth.Google/Platforms/iOS/FirebaseAuthGoogleImplementation.cs +++ b/src/Auth.Google/Platforms/iOS/FirebaseAuthGoogleImplementation.cs @@ -1,10 +1,10 @@ using Firebase.Auth; +using Google.SignIn; using Plugin.Firebase.Auth.Platforms.iOS.Extensions; +using Plugin.Firebase.Auth.Platforms.iOS.Google; using Plugin.Firebase.Core; using Plugin.Firebase.Core.Exceptions; using FirebaseAuth = Firebase.Auth.Auth; -using Google.SignIn; -using Plugin.Firebase.Auth.Platforms.iOS.Google; namespace Plugin.Firebase.Auth.Google; @@ -12,15 +12,16 @@ public sealed class FirebaseAuthGoogleImplementation : DisposableBase, IFirebase { public static void Initialize() { - var googleServiceDictionary = NSDictionary.FromFile("GoogleService-Info.plist"); - SignIn.SharedInstance.ClientId = googleServiceDictionary["CLIENT_ID"].ToString(); + var googleServiceDictionary = NSMutableDictionary.FromFile("GoogleService-Info.plist"); + var clientId = googleServiceDictionary["CLIENT_ID"].ToString(); + SignIn.SharedInstance.Configuration = new Configuration(clientId); } - + public static bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) { return SignIn.SharedInstance.HandleUrl(url); } - + private readonly FirebaseAuth _firebaseAuth; private static Lazy _googleAuth; @@ -29,7 +30,7 @@ public FirebaseAuthGoogleImplementation() _firebaseAuth = FirebaseAuth.DefaultInstance; _googleAuth = new Lazy(() => new GoogleAuth()); } - + public async Task SignInWithGoogleAsync() { try { @@ -39,13 +40,13 @@ public async Task SignInWithGoogleAsync() throw GetFirebaseAuthException(e); } } - + private async Task SignInWithCredentialAsync(AuthCredential credential) { var authResult = await _firebaseAuth.SignInWithCredentialAsync(credential); return authResult.User.ToAbstract(authResult.AdditionalUserInfo); } - + private static FirebaseAuthException GetFirebaseAuthException(NSErrorException ex) { AuthErrorCode errorCode; @@ -58,7 +59,7 @@ private static FirebaseAuthException GetFirebaseAuthException(NSErrorException e Enum.TryParse(errorCode.ToString(), out FIRAuthError authError); return new FirebaseAuthException(authError, ex.Error.LocalizedDescription); } - + public async Task LinkWithGoogleAsync() { try { @@ -69,19 +70,19 @@ public async Task LinkWithGoogleAsync() throw GetFirebaseAuthException(e); } } - + private async Task LinkWithCredentialAsync(AuthCredential credential) { var authResult = await _firebaseAuth.CurrentUser.LinkAsync(credential); return authResult.User.ToAbstract(authResult.AdditionalUserInfo); } - + public Task SignOutAsync() { _googleAuth.Value.SignOut(); return Task.CompletedTask; } - + private static UIViewController ViewController { get { var rootViewController = UIApplication.SharedApplication.KeyWindow.RootViewController; diff --git a/src/Auth.Google/Platforms/iOS/GoogleAuth.cs b/src/Auth.Google/Platforms/iOS/GoogleAuth.cs index ae108a5..79d3fc9 100644 --- a/src/Auth.Google/Platforms/iOS/GoogleAuth.cs +++ b/src/Auth.Google/Platforms/iOS/GoogleAuth.cs @@ -3,29 +3,25 @@ namespace Plugin.Firebase.Auth.Platforms.iOS.Google; -public sealed class GoogleAuth : NSObject, ISignInDelegate +public sealed class GoogleAuth : NSObject { private UIViewController _viewController; private TaskCompletionSource _tcs; - public GoogleAuth() - { - SignIn.SharedInstance.Delegate = this; - } - public Task GetCredentialAsync(UIViewController viewController) { _viewController = viewController; _tcs = new TaskCompletionSource(); - SignIn.SharedInstance.PresentingViewController = viewController; - SignIn.SharedInstance.SignInUser(); + SignIn.SharedInstance.SignInWithPresentingViewController(viewController, DidSignIn); return _tcs.Task; } - public void DidSignIn(SignIn signIn, GoogleUser user, NSError error) + public void DidSignIn(SignInResult signIn, NSError error) { + var user = signIn.User; + if(user != null && error == null) { - _tcs?.SetResult(GoogleAuthProvider.GetCredential(user.Authentication.IdToken, user.Authentication.AccessToken)); + _tcs?.SetResult(GoogleAuthProvider.GetCredential(user.IdToken.TokenString, user.AccessToken.TokenString)); } else { _tcs?.SetException(new NSErrorException(error)); }