Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 9a0c2b5

Browse files
committed
Require UIViewController for presentation in getOAuthToken
This is a step towards no longer requiring `NewGoogleAuthenticator` to store the `contextProvider`.
1 parent de07fd2 commit 9a0c2b5

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Demo/AuthenticatorDemo/ViewController+WordPressAuthenticator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ extension ViewController {
8787
func getAuthTokenFromGoogle() {
8888
Task { @MainActor in
8989
do {
90-
let token = try await self.googleAuthenticator.getOAuthToken()
90+
let token = try await self.googleAuthenticator.getOAuthToken(from: self)
9191

9292
presentAlert(
9393
title: "🎉",

WordPressAuthenticator/GoogleSignIn/NewGoogleAuthenticator.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,28 @@ public class NewGoogleAuthenticator: NSObject {
5656
}
5757

5858
/// Get the user's OAuth token from their Google account. This token can be used to authenticate with the WordPress backend.
59-
public func getOAuthToken() async throws -> IDToken {
59+
///
60+
/// The app will present the browser to hand over authentication to Google from the given `UIViewController`.
61+
public func getOAuthToken(from viewController: UIViewController) async throws -> IDToken {
62+
return try await getOAuthToken(
63+
from: WebAuthenticationPresentationContext(viewController: viewController)
64+
)
65+
}
66+
67+
/// Get the user's OAuth token from their Google account. This token can be used to authenticate with the WordPress backend.
68+
///
69+
/// The app will present the browser to hand over authentication to Google using the given
70+
/// `ASWebAuthenticationPresentationContextProviding`.
71+
public func getOAuthToken(
72+
from contextProvider: ASWebAuthenticationPresentationContextProviding
73+
) async throws -> IDToken {
6074
let pkce = try ProofKeyForCodeExchange()
61-
let url = try await getURL(clientId: clientId, scheme: scheme, pkce: pkce, contextProvider: contextProvider)
75+
let url = try await getURL(
76+
clientId: clientId,
77+
scheme: scheme,
78+
pkce: pkce,
79+
contextProvider: contextProvider
80+
)
6281
return try await requestOAuthToken(url: url, clientId: clientId, audience: audience, pkce: pkce)
6382
}
6483

WordPressAuthenticator/Unified Auth/GoogleAuthenticator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ extension GoogleAuthenticator {
319319
)
320320

321321
await SVProgressHUD.show()
322-
return try await sdkLessGoogleAuthenticator.getOAuthToken()
322+
return try await sdkLessGoogleAuthenticator.getOAuthToken(from: viewController)
323323
}
324324
}
325325

0 commit comments

Comments
 (0)