From 12e00524a728f2544388d2eb72719bb5d01fae73 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Wed, 27 Sep 2023 13:18:59 +0200 Subject: [PATCH] feat(authentication): support `serverAuthCode` for Google Sign-in (#442) --- .changeset/new-lemons-whisper.md | 5 ++++ packages/authentication/README.md | 3 ++- .../FirebaseAuthentication.java | 11 +++++--- .../FirebaseAuthenticationHelper.java | 26 ++++++++++++++++-- .../handlers/AppleAuthProviderHandler.java | 4 +-- .../handlers/FacebookAuthProviderHandler.java | 4 +-- .../handlers/GoogleAuthProviderHandler.java | 12 +++++---- .../handlers/OAuthProviderHandler.java | 4 +-- .../PlayGamesAuthProviderHandler.java | 10 +++---- .../ios/Plugin.xcodeproj/project.pbxproj | 2 ++ .../ios/Plugin/FirebaseAuthentication.swift | 27 +++++++++++++++---- .../Plugin/FirebaseAuthenticationHelper.swift | 13 +++++++-- .../Handlers/GoogleAuthProviderHandler.swift | 9 ++++--- packages/authentication/src/definitions.ts | 10 +++++++ 14 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 .changeset/new-lemons-whisper.md diff --git a/.changeset/new-lemons-whisper.md b/.changeset/new-lemons-whisper.md new file mode 100644 index 00000000..65bc0965 --- /dev/null +++ b/.changeset/new-lemons-whisper.md @@ -0,0 +1,5 @@ +--- +'@capacitor-firebase/authentication': minor +--- + +feat: support `forceCodeForRefreshToken` for Google Sign-in diff --git a/packages/authentication/README.md b/packages/authentication/README.md index 7d961dd9..16bebabe 100644 --- a/packages/authentication/README.md +++ b/packages/authentication/README.md @@ -1558,6 +1558,7 @@ Remove all listeners for this plugin. | **`nonce`** | string | The random string used to make sure that the ID token you get was granted specifically in response to your app's authentication request. | 0.1.0 | | **`providerId`** | string | The authentication provider ID for the credential. | 0.1.0 | | **`secret`** | string | The OAuth access token secret associated with the credential if it belongs to an OAuth 1.0 provider. | 0.1.0 | +| **`serverAuthCode`** | string | The server auth code. Only available for Google Sign-in and Play Games Sign-In on Android and iOS. | 5.2.0 | #### AdditionalUserInfo @@ -1633,7 +1634,7 @@ Remove all listeners for this plugin. | Prop | Type | Description | Default | Since | | ---------------------- | ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------- | ----- | | **`customParameters`** | SignInCustomParameter[] | Configures custom parameters to be passed to the identity provider during the OAuth sign-in flow. Supports Apple, Facebook, GitHub, Google, Microsoft, Twitter and Yahoo on Web. Supports Apple, GitHub, Microsoft, Twitter and Yahoo on Android. Supports Facebook, GitHub, Microsoft, Twitter and Yahoo on iOS. | | 1.1.0 | -| **`mode`** | 'popup' \| 'redirect' | Whether to use the popup-based OAuth authentication flow or the full-page redirect flow. If you choose `redirect`, you will get the result of the call via the `authStateChange` listener after the redirect. | 'popup' | 1.3.0 | +| **`mode`** | 'popup' \| 'redirect' | Whether to use the popup-based OAuth authentication flow or the full-page redirect flow. If you choose `redirect`, you will get the result of the call via the `authStateChange` listener after the redirect. Only available for Web. | 'popup' | 1.3.0 | | **`scopes`** | string[] | Scopes to request from provider. Supports Apple, Facebook, GitHub, Google, Microsoft, Twitter and Yahoo on Web. Supports Apple, GitHub, Google, Microsoft, Twitter, Yahoo and Play Games on Android. Supports Facebook, GitHub, Google, Microsoft, Twitter and Yahoo on iOS. | | 1.1.0 | diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java index ac6c08f9..53873bb1 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java @@ -667,6 +667,7 @@ public void handleSuccessfulSignIn( @Nullable String idToken, @Nullable String nonce, @Nullable String accessToken, + @Nullable String serverAuthCode, @Nullable AdditionalUserInfo additionalUserInfo ) { boolean skipNativeAuth = call.getBoolean("skipNativeAuth", this.config.getSkipNativeAuth()); @@ -677,6 +678,7 @@ public void handleSuccessfulSignIn( idToken, nonce, accessToken, + serverAuthCode, additionalUserInfo ); call.resolve(signInResult); @@ -732,7 +734,8 @@ public void handleSuccessfulLink( @Nullable AuthCredential credential, @Nullable String idToken, @Nullable String nonce, - @Nullable String accessToken + @Nullable String accessToken, + @Nullable String serverAuthCode ) { FirebaseUser user = firebaseAuthInstance.getCurrentUser(); if (user == null) { @@ -746,7 +749,7 @@ public void handleSuccessfulLink( task -> { if (task.isSuccessful()) { final AuthResult authResult = task.getResult(); - handleSuccessfulLink(call, authResult, idToken, nonce, accessToken); + handleSuccessfulLink(call, authResult, idToken, nonce, accessToken, serverAuthCode); } else { Exception exception = task.getException(); Logger.error(TAG, exception.getMessage(), exception); @@ -762,7 +765,8 @@ public void handleSuccessfulLink( final AuthResult authResult, @Nullable String idToken, @Nullable String nonce, - @Nullable String accessToken + @Nullable String accessToken, + @Nullable String serverAuthCode ) { JSObject linkResult = FirebaseAuthenticationHelper.createSignInResult( authResult.getUser(), @@ -770,6 +774,7 @@ public void handleSuccessfulLink( idToken, nonce, accessToken, + serverAuthCode, authResult.getAdditionalUserInfo() ); call.resolve(linkResult); diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationHelper.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationHelper.java index da16be18..fce28e24 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationHelper.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationHelper.java @@ -49,9 +49,27 @@ public static JSObject createSignInResult( @Nullable String nonce, @Nullable String accessToken, @Nullable AdditionalUserInfo additionalUserInfo + ) { + return FirebaseAuthenticationHelper.createSignInResult(user, credential, idToken, nonce, accessToken, null, additionalUserInfo); + } + + public static JSObject createSignInResult( + @Nullable FirebaseUser user, + @Nullable AuthCredential credential, + @Nullable String idToken, + @Nullable String nonce, + @Nullable String accessToken, + @Nullable String serverAuthCode, + @Nullable AdditionalUserInfo additionalUserInfo ) { JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user); - JSObject credentialResult = FirebaseAuthenticationHelper.createCredentialResult(credential, idToken, nonce, accessToken); + JSObject credentialResult = FirebaseAuthenticationHelper.createCredentialResult( + credential, + idToken, + nonce, + accessToken, + serverAuthCode + ); JSObject additionalUserInfoResult = FirebaseAuthenticationHelper.createAdditionalUserInfoResult(additionalUserInfo); JSObject result = new JSObject(); result.put("user", userResult); @@ -85,7 +103,8 @@ public static JSObject createCredentialResult( @Nullable AuthCredential credential, @Nullable String idToken, @Nullable String nonce, - @Nullable String accessToken + @Nullable String accessToken, + @Nullable String serverAuthCode ) { if (credential == null && idToken == null && nonce == null && accessToken == null) { return null; @@ -117,6 +136,9 @@ public static JSObject createCredentialResult( if (accessToken != null) { result.put("accessToken", accessToken); } + if (serverAuthCode != null) { + result.put("serverAuthCode", serverAuthCode); + } return result; } diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java index 1f18341c..41d188ea 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java @@ -86,13 +86,13 @@ private void startActivityForLink(final PluginCall call, OAuthProvider.Builder p pluginImplementation .getCurrentUser() .startActivityForLinkWithProvider(pluginImplementation.getPlugin().getActivity(), provider.build()) - .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, currentNonce, null)) + .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, currentNonce, null, null)) .addOnFailureListener(exception -> pluginImplementation.handleFailedLink(call, null, exception)); } private void finishActivityForLink(final PluginCall call, Task pendingResultTask) { pendingResultTask - .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, currentNonce, null)) + .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, currentNonce, null, null)) .addOnFailureListener(exception -> pluginImplementation.handleFailedLink(call, null, exception)); } diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java index f3c7b6c7..e23d147a 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java @@ -110,9 +110,9 @@ private void handleSuccessCallback(LoginResult loginResult) { return; } if (isLink) { - pluginImplementation.handleSuccessfulLink(savedCall, credential, null, null, accessTokenString); + pluginImplementation.handleSuccessfulLink(savedCall, credential, null, null, accessTokenString, null); } else { - pluginImplementation.handleSuccessfulSignIn(savedCall, credential, null, null, accessTokenString, null); + pluginImplementation.handleSuccessfulSignIn(savedCall, credential, null, null, accessTokenString, null, null); } } diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java index 3c7364fd..6cfac45a 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java @@ -57,6 +57,7 @@ public void handleOnActivityResult(@NonNull final PluginCall call, @NonNull Acti try { GoogleSignInAccount account = task.getResult(ApiException.class); String idToken = account.getIdToken(); + String serverAuthCode = account.getServerAuthCode(); AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null); // Get Access Token and resolve new Thread( @@ -77,9 +78,9 @@ public void handleOnActivityResult(@NonNull final PluginCall call, @NonNull Acti return; } if (isLink) { - pluginImplementation.handleSuccessfulLink(call, credential, idToken, null, accessToken); + pluginImplementation.handleSuccessfulLink(call, credential, idToken, null, accessToken, serverAuthCode); } else { - pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, accessToken, null); + pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, accessToken, serverAuthCode, null); } } ) @@ -98,8 +99,9 @@ private GoogleSignInClient buildGoogleSignInClient() { } private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) { - GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) + GoogleSignInOptions.Builder googleSignInOptionsBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id)) + .requestServerAuthCode(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id)) .requestEmail(); if (call != null) { @@ -108,7 +110,7 @@ private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) { try { List scopeList = scopes.toList(); for (String scope : scopeList) { - gsob = gsob.requestScopes(new Scope(scope)); + googleSignInOptionsBuilder = googleSignInOptionsBuilder.requestScopes(new Scope(scope)); } } catch (JSONException exception) { Log.e(FirebaseAuthenticationPlugin.TAG, "buildGoogleSignInClient failed.", exception); @@ -116,6 +118,6 @@ private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) { } } - return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build()); + return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), googleSignInOptionsBuilder.build()); } } diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java index e7f8c862..f3f3060d 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java @@ -63,13 +63,13 @@ private void startActivityForLink(final PluginCall call, OAuthProvider.Builder p pluginImplementation .getCurrentUser() .startActivityForLinkWithProvider(pluginImplementation.getPlugin().getActivity(), provider.build()) - .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, null, null)) + .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, null, null, null)) .addOnFailureListener(exception -> pluginImplementation.handleFailedLink(call, null, exception)); } private void finishActivityForLink(final PluginCall call, Task pendingResultTask) { pendingResultTask - .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, null, null)) + .addOnSuccessListener(authResult -> pluginImplementation.handleSuccessfulLink(call, authResult, null, null, null, null)) .addOnFailureListener(exception -> pluginImplementation.handleFailedLink(call, null, exception)); } diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java index cc980c12..1ca376dc 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java @@ -57,9 +57,9 @@ public void handleOnActivityResult(@NonNull final PluginCall call, @NonNull Acti AuthCredential credential = PlayGamesAuthProvider.getCredential(serverAuthCode); String idToken = account.getIdToken(); if (isLink) { - pluginImplementation.handleSuccessfulLink(call, credential, idToken, null, null); + pluginImplementation.handleSuccessfulLink(call, credential, idToken, null, null, serverAuthCode); } else { - pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, null, null); + pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, null, serverAuthCode, null); } } catch (ApiException exception) { if (isLink) { @@ -75,7 +75,7 @@ private GoogleSignInClient buildGoogleSignInClient() { } private GoogleSignInClient buildGoogleSignInClient(@Nullable final PluginCall call) { - GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) + GoogleSignInOptions.Builder googleSignInOptionsBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) .requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id)) .requestServerAuthCode(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id)) .requestEmail(); @@ -86,7 +86,7 @@ private GoogleSignInClient buildGoogleSignInClient(@Nullable final PluginCall ca try { List scopeList = scopes.toList(); for (String scope : scopeList) { - gsob = gsob.requestScopes(new Scope(scope)); + googleSignInOptionsBuilder = googleSignInOptionsBuilder.requestScopes(new Scope(scope)); } } catch (JSONException exception) { Log.e(FirebaseAuthenticationPlugin.TAG, "buildGoogleSignInClient failed.", exception); @@ -94,6 +94,6 @@ private GoogleSignInClient buildGoogleSignInClient(@Nullable final PluginCall ca } } - return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build()); + return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), googleSignInOptionsBuilder.build()); } } diff --git a/packages/authentication/ios/Plugin.xcodeproj/project.pbxproj b/packages/authentication/ios/Plugin.xcodeproj/project.pbxproj index 57a29954..a270071d 100644 --- a/packages/authentication/ios/Plugin.xcodeproj/project.pbxproj +++ b/packages/authentication/ios/Plugin.xcodeproj/project.pbxproj @@ -592,6 +592,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 13.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(FRAMEWORK_SEARCH_PATHS)\n$(FRAMEWORK_SEARCH_PATHS)\n$(FRAMEWORK_SEARCH_PATHS)"; ONLY_ACTIVE_ARCH = YES; + OTHER_SWIFT_FLAGS = "$(inherited) -D COCOAPODS -D RGCFA_INCLUDE_GOOGLE -D RGCFA_INCLUDE_FACEBOOK"; PRODUCT_BUNDLE_IDENTIFIER = com.getcapacitor.Plugin; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; @@ -618,6 +619,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 13.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(FRAMEWORK_SEARCH_PATHS)"; ONLY_ACTIVE_ARCH = NO; + OTHER_SWIFT_FLAGS = "$(inherited) -D COCOAPODS -D RGCFA_INCLUDE_GOOGLE -D RGCFA_INCLUDE_FACEBOOK"; PRODUCT_BUNDLE_IDENTIFIER = com.getcapacitor.Plugin; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; diff --git a/packages/authentication/ios/Plugin/FirebaseAuthentication.swift b/packages/authentication/ios/Plugin/FirebaseAuthentication.swift index 62de7733..4ab26396 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthentication.swift +++ b/packages/authentication/ios/Plugin/FirebaseAuthentication.swift @@ -494,13 +494,18 @@ public typealias AuthStateChangedObserver = () -> Void } func handleSuccessfulSignIn(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, displayName: String?, authorizationCode: String?) { + self.handleSuccessfulSignIn(credential: credential, idToken: idToken, nonce: nonce, accessToken: accessToken, displayName: nil, authorizationCode: nil, serverAuthCode: nil) + } + + func handleSuccessfulSignIn(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, displayName: String?, authorizationCode: String?, serverAuthCode: String?) { guard let savedCall = self.savedCall else { return } let skipNativeAuth = savedCall.getBool("skipNativeAuth", config.skipNativeAuth) if skipNativeAuth == true { - let result = FirebaseAuthenticationHelper.createSignInResult(credential: credential, user: nil, idToken: idToken, nonce: nonce, - accessToken: accessToken, additionalUserInfo: nil, displayName: displayName, + let result = FirebaseAuthenticationHelper.createSignInResult(credential: credential, user: nil, idToken: idToken, + nonce: nonce, accessToken: accessToken, serverAuthCode: serverAuthCode, + additionalUserInfo: nil, displayName: displayName, authorizationCode: authorizationCode) savedCall.resolve(result) return @@ -514,8 +519,9 @@ public typealias AuthStateChangedObserver = () -> Void return } let result = FirebaseAuthenticationHelper.createSignInResult(credential: authResult?.credential, user: authResult?.user, idToken: idToken, - nonce: nonce, accessToken: accessToken, additionalUserInfo: authResult?.additionalUserInfo, - displayName: displayName, authorizationCode: authorizationCode) + nonce: nonce, accessToken: accessToken, serverAuthCode: serverAuthCode, + additionalUserInfo: authResult?.additionalUserInfo, displayName: displayName, + authorizationCode: authorizationCode) savedCall.resolve(result) } } @@ -535,7 +541,17 @@ public typealias AuthStateChangedObserver = () -> Void accessToken: accessToken, displayName: nil, authorizationCode: nil) } + func handleSuccessfulLink(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, serverAuthCode: String?) { + self.handleSuccessfulLink(credential: credential, idToken: idToken, nonce: nonce, + accessToken: accessToken, displayName: nil, authorizationCode: nil) + } + func handleSuccessfulLink(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, displayName: String?, authorizationCode: String?) { + self.handleSuccessfulLink(credential: credential, idToken: idToken, nonce: nonce, + accessToken: accessToken, serverAuthCode: nil, displayName: nil, authorizationCode: nil) + } + + func handleSuccessfulLink(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, serverAuthCode: String?, displayName: String?, authorizationCode: String?) { guard let user = getCurrentUser() else { self.handleFailedLink(message: plugin.errorNoUserSignedIn, error: nil) return @@ -549,7 +565,8 @@ public typealias AuthStateChangedObserver = () -> Void return } let result = FirebaseAuthenticationHelper.createSignInResult(credential: authResult?.credential, user: authResult?.user, idToken: idToken, - nonce: nonce, accessToken: accessToken, additionalUserInfo: authResult?.additionalUserInfo, + nonce: nonce, accessToken: accessToken, + serverAuthCode: serverAuthCode, additionalUserInfo: authResult?.additionalUserInfo, displayName: displayName, authorizationCode: authorizationCode) savedCall.resolve(result) } diff --git a/packages/authentication/ios/Plugin/FirebaseAuthenticationHelper.swift b/packages/authentication/ios/Plugin/FirebaseAuthenticationHelper.swift index e0a8b632..9dfcfe40 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthenticationHelper.swift +++ b/packages/authentication/ios/Plugin/FirebaseAuthenticationHelper.swift @@ -35,8 +35,14 @@ public class FirebaseAuthenticationHelper { } public static func createSignInResult(credential: AuthCredential?, user: User?, idToken: String?, nonce: String?, accessToken: String?, additionalUserInfo: AdditionalUserInfo?, displayName: String?, authorizationCode: String?) -> JSObject { + return createSignInResult(credential: credential, user: user, idToken: idToken, nonce: nonce, accessToken: accessToken, + serverAuthCode: nil, additionalUserInfo: additionalUserInfo, displayName: nil, authorizationCode: nil) + } + + // swiftlint:disable function_parameter_count + public static func createSignInResult(credential: AuthCredential?, user: User?, idToken: String?, nonce: String?, accessToken: String?, serverAuthCode: String?, additionalUserInfo: AdditionalUserInfo?, displayName: String?, authorizationCode: String?) -> JSObject { let userResult = self.createUserResult(user, displayName: displayName) - let credentialResult = self.createCredentialResult(credential, idToken: idToken, nonce: nonce, accessToken: accessToken, authorizationCode: authorizationCode) + let credentialResult = self.createCredentialResult(credential, idToken: idToken, nonce: nonce, accessToken: accessToken, authorizationCode: authorizationCode, serverAuthCode: serverAuthCode) let additionalUserInfoResult = self.createAdditionalUserInfoResult(additionalUserInfo) var result = JSObject() result["user"] = userResult @@ -74,7 +80,7 @@ public class FirebaseAuthenticationHelper { return result } - public static func createCredentialResult(_ credential: AuthCredential?, idToken: String?, nonce: String?, accessToken: String?, authorizationCode: String?) -> JSObject? { + public static func createCredentialResult(_ credential: AuthCredential?, idToken: String?, nonce: String?, accessToken: String?, authorizationCode: String?, serverAuthCode: String?) -> JSObject? { if credential == nil && idToken == nil && nonce == nil && accessToken == nil && authorizationCode == nil { return nil } @@ -108,6 +114,9 @@ public class FirebaseAuthenticationHelper { if let authorizationCode = authorizationCode { result["authorizationCode"] = authorizationCode } + if let serverAuthCode = serverAuthCode { + result["serverAuthCode"] = serverAuthCode + } return result } diff --git a/packages/authentication/ios/Plugin/Handlers/GoogleAuthProviderHandler.swift b/packages/authentication/ios/Plugin/Handlers/GoogleAuthProviderHandler.swift index fb60cfaa..08c5c9fd 100644 --- a/packages/authentication/ios/Plugin/Handlers/GoogleAuthProviderHandler.swift +++ b/packages/authentication/ios/Plugin/Handlers/GoogleAuthProviderHandler.swift @@ -37,7 +37,7 @@ class GoogleAuthProviderHandler: NSObject { let scopes = call.getArray("scopes", String.self) ?? [] DispatchQueue.main.async { - GIDSignIn.sharedInstance.signIn(withPresenting: controller, scopes: scopes) { [unowned self] result, error in + GIDSignIn.sharedInstance.signIn(withPresenting: controller, hint: nil, additionalScopes: scopes) { [unowned self] result, error in if let error = error { if isLink == true { self.pluginImplementation.handleFailedLink(message: nil, error: error) @@ -53,11 +53,14 @@ class GoogleAuthProviderHandler: NSObject { return } let accessToken = user.accessToken.tokenString + let serverAuthCode = result?.serverAuthCode let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken) if isLink == true { - self.pluginImplementation.handleSuccessfulLink(credential: credential, idToken: idToken, nonce: nil, accessToken: accessToken) + self.pluginImplementation.handleSuccessfulLink(credential: credential, idToken: idToken, nonce: nil, accessToken: accessToken, + serverAuthCode: serverAuthCode) } else { - self.pluginImplementation.handleSuccessfulSignIn(credential: credential, idToken: idToken, nonce: nil, accessToken: accessToken) + self.pluginImplementation.handleSuccessfulSignIn(credential: credential, idToken: idToken, nonce: nil, accessToken: accessToken, + displayName: nil, authorizationCode: nil, serverAuthCode: serverAuthCode) } } } diff --git a/packages/authentication/src/definitions.ts b/packages/authentication/src/definitions.ts index cb27dfe1..6c004628 100644 --- a/packages/authentication/src/definitions.ts +++ b/packages/authentication/src/definitions.ts @@ -831,6 +831,8 @@ export interface SignInWithOAuthOptions extends SignInOptions { * Whether to use the popup-based OAuth authentication flow or the full-page redirect flow. * If you choose `redirect`, you will get the result of the call via the `authStateChange` listener after the redirect. * + * Only available for Web. + * * @default 'popup' * @since 1.3.0 */ @@ -1204,6 +1206,14 @@ export interface AuthCredential { * @since 0.1.0 */ secret?: string; + /** + * The server auth code. + * + * Only available for Google Sign-in and Play Games Sign-In on Android and iOS. + * + * @since 5.2.0 + */ + serverAuthCode?: string; } /**