From 0b0562cf63e37109714eefaec3c8dd5ef1b83a3c Mon Sep 17 00:00:00 2001 From: Borut Tomazin Date: Mon, 9 Dec 2024 14:56:12 +0100 Subject: [PATCH] Google/ClientId (#34) Co-authored-by: Egzon Arifi --- .github/workflows/Tests.yml | 9 ++++-- Sources/Google/GoogleAuthenticator.swift | 38 +++++++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 326c034..2692d89 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -1,9 +1,12 @@ name: Tests -on: - push: +on: pull_request: - types: [opened] + types: [opened, synchronize, reopened] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true jobs: Tests: diff --git a/Sources/Google/GoogleAuthenticator.swift b/Sources/Google/GoogleAuthenticator.swift index 367eb46..1242a19 100644 --- a/Sources/Google/GoogleAuthenticator.swift +++ b/Sources/Google/GoogleAuthenticator.swift @@ -23,14 +23,24 @@ extension GoogleAuthenticator: Authenticator { /// SignIn user. /// /// Will asynchronously return the `Response` object on success or `Error` on error. - public func signIn(from presentingViewController: UIViewController, - hint: String? = .none, - additionalScopes: [String]? = .none) async throws -> Response { + public func signIn( + from presentingViewController: UIViewController, + clientId: String? = nil, + hint: String? = .none, + additionalScopes: [String]? = .none + ) async throws -> Response { guard !provider.hasPreviousSignIn() else { return try await restorePreviousSignIn() } - return try await signInUser(from: presentingViewController, hint: hint, additionalScopes: additionalScopes) + // set clientId if provided (clientId is needed when doing auth via firebase) + clientId.map { provider.configuration = .init(clientID: $0) } + + return try await signInUser( + from: presentingViewController, + hint: hint, + additionalScopes: additionalScopes + ) } /// Clears the signIn footprint and logs out the user immediatelly. @@ -46,7 +56,11 @@ extension GoogleAuthenticator: Authenticator { /// Boolean if given `url` should be handled. /// /// Call this from UIApplicationDelegate’s `application:openURL:options:` method. - public func canOpenUrl(_ url: URL, application: UIApplication, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool { + public func canOpenUrl( + _ url: URL, + application: UIApplication, + options: [UIApplication.OpenURLOptionsKey : Any] + ) -> Bool { GIDSignIn.sharedInstance.handle(url) } } @@ -66,11 +80,19 @@ private extension GoogleAuthenticator { } } } - - func signInUser(from presentingViewController: UIViewController, hint: String?, additionalScopes: [String]?) async throws -> Response { + + func signInUser( + from presentingViewController: UIViewController, + hint: String?, + additionalScopes: [String]? + ) async throws -> Response { try await withCheckedThrowingContinuation { continuation in provider - .signIn(withPresenting: presentingViewController, hint: hint, additionalScopes: additionalScopes) { result, error in + .signIn( + withPresenting: presentingViewController, + hint: hint, + additionalScopes: additionalScopes + ) { result, error in switch (result, error) { case (let signInResult?, _): continuation.resume(returning: signInResult.user.authResponse)