Skip to content

Commit

Permalink
Update Auth.Google to use AdamE.Google.iOS.SignIn
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Feb 2, 2025
1 parent 2bc6cc1 commit 7281862
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Auth.Google/Auth.Google.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
<PackageReference Include="Xamarin.Google.iOS.SignIn" Version="5.0.2.4" />
<PackageReference Include="AdamE.Google.iOS.SignIn" Version="8.0.0" />
</ItemGroup>

</Project>
27 changes: 14 additions & 13 deletions src/Auth.Google/Platforms/iOS/FirebaseAuthGoogleImplementation.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
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;

public sealed class FirebaseAuthGoogleImplementation : DisposableBase, IFirebaseAuthGoogle
{
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> _googleAuth;

Expand All @@ -29,7 +30,7 @@ public FirebaseAuthGoogleImplementation()
_firebaseAuth = FirebaseAuth.DefaultInstance;
_googleAuth = new Lazy<GoogleAuth>(() => new GoogleAuth());
}

public async Task<IFirebaseUser> SignInWithGoogleAsync()
{
try {
Expand All @@ -39,13 +40,13 @@ public async Task<IFirebaseUser> SignInWithGoogleAsync()
throw GetFirebaseAuthException(e);
}
}

private async Task<IFirebaseUser> SignInWithCredentialAsync(AuthCredential credential)
{
var authResult = await _firebaseAuth.SignInWithCredentialAsync(credential);
return authResult.User.ToAbstract(authResult.AdditionalUserInfo);
}

private static FirebaseAuthException GetFirebaseAuthException(NSErrorException ex)
{
AuthErrorCode errorCode;
Expand All @@ -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<IFirebaseUser> LinkWithGoogleAsync()
{
try {
Expand All @@ -69,19 +70,19 @@ public async Task<IFirebaseUser> LinkWithGoogleAsync()
throw GetFirebaseAuthException(e);
}
}

private async Task<IFirebaseUser> 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;
Expand Down
16 changes: 6 additions & 10 deletions src/Auth.Google/Platforms/iOS/GoogleAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthCredential> _tcs;

public GoogleAuth()
{
SignIn.SharedInstance.Delegate = this;
}

public Task<AuthCredential> GetCredentialAsync(UIViewController viewController)
{
_viewController = viewController;
_tcs = new TaskCompletionSource<AuthCredential>();
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));
}
Expand Down

0 comments on commit 7281862

Please sign in to comment.