|
1 | 1 | import _Helpers
|
2 |
| -import AuthenticationServices |
3 | 2 | import Foundation
|
4 | 3 |
|
| 4 | +#if canImport(AuthenticationServices) |
| 5 | + import AuthenticationServices |
| 6 | +#endif |
| 7 | + |
5 | 8 | #if canImport(FoundationNetworking)
|
6 | 9 | import FoundationNetworking
|
7 | 10 | #endif
|
@@ -652,75 +655,77 @@ public final class AuthClient: @unchecked Sendable {
|
652 | 655 | return try await session(from: resultURL)
|
653 | 656 | }
|
654 | 657 |
|
655 |
| - /// Sign-in an existing user via a third-party provider using ``ASWebAuthenticationSession``. |
656 |
| - /// |
657 |
| - /// - Parameters: |
658 |
| - /// - provider: The third-party provider. |
659 |
| - /// - redirectTo: A URL to send the user to after they are confirmed. |
660 |
| - /// - scopes: A space-separated list of scopes granted to the OAuth application. |
661 |
| - /// - queryParams: Additional query params. |
662 |
| - /// - configure: A configuration closure that you can use to customize the internal |
663 |
| - /// ``ASWebAuthenticationSession`` object. |
664 |
| - /// |
665 |
| - /// - Note: This method support the PKCE flow. |
666 |
| - /// - Warning: Do not call `start()` on the `ASWebAuthenticationSession` object inside the |
667 |
| - /// `configure` closure, as the method implementation calls it already. |
668 |
| - @available(watchOS 6.2, tvOS 16.0, *) |
669 |
| - @discardableResult |
670 |
| - public func signInWithOAuth( |
671 |
| - provider: Provider, |
672 |
| - redirectTo: URL? = nil, |
673 |
| - scopes: String? = nil, |
674 |
| - queryParams: [(name: String, value: String?)] = [], |
675 |
| - configure: @Sendable (_ session: ASWebAuthenticationSession) -> Void = { _ in } |
676 |
| - ) async throws -> Session { |
677 |
| - try await signInWithOAuth( |
678 |
| - provider: provider, |
679 |
| - redirectTo: redirectTo, |
680 |
| - scopes: scopes, |
681 |
| - queryParams: queryParams |
682 |
| - ) { @MainActor url in |
683 |
| - try await withCheckedThrowingContinuation { continuation in |
684 |
| - guard let callbackScheme = (configuration.redirectToURL ?? redirectTo)?.scheme else { |
685 |
| - continuation.resume(throwing: AuthError.invalidRedirectScheme) |
686 |
| - return |
687 |
| - } |
688 |
| - |
689 |
| - #if !os(tvOS) && !os(watchOS) |
690 |
| - var presentationContextProvider: DefaultPresentationContextProvider? |
691 |
| - #endif |
692 |
| - |
693 |
| - let session = ASWebAuthenticationSession( |
694 |
| - url: url, |
695 |
| - callbackURLScheme: callbackScheme |
696 |
| - ) { url, error in |
697 |
| - if let error { |
698 |
| - continuation.resume(throwing: error) |
699 |
| - } else if let url { |
700 |
| - continuation.resume(returning: url) |
701 |
| - } else { |
702 |
| - continuation.resume(throwing: AuthError.missingURL) |
| 658 | + #if canImport(AuthenticationServices) |
| 659 | + /// Sign-in an existing user via a third-party provider using ``ASWebAuthenticationSession``. |
| 660 | + /// |
| 661 | + /// - Parameters: |
| 662 | + /// - provider: The third-party provider. |
| 663 | + /// - redirectTo: A URL to send the user to after they are confirmed. |
| 664 | + /// - scopes: A space-separated list of scopes granted to the OAuth application. |
| 665 | + /// - queryParams: Additional query params. |
| 666 | + /// - configure: A configuration closure that you can use to customize the internal |
| 667 | + /// ``ASWebAuthenticationSession`` object. |
| 668 | + /// |
| 669 | + /// - Note: This method support the PKCE flow. |
| 670 | + /// - Warning: Do not call `start()` on the `ASWebAuthenticationSession` object inside the |
| 671 | + /// `configure` closure, as the method implementation calls it already. |
| 672 | + @available(watchOS 6.2, tvOS 16.0, *) |
| 673 | + @discardableResult |
| 674 | + public func signInWithOAuth( |
| 675 | + provider: Provider, |
| 676 | + redirectTo: URL? = nil, |
| 677 | + scopes: String? = nil, |
| 678 | + queryParams: [(name: String, value: String?)] = [], |
| 679 | + configure: @Sendable (_ session: ASWebAuthenticationSession) -> Void = { _ in } |
| 680 | + ) async throws -> Session { |
| 681 | + try await signInWithOAuth( |
| 682 | + provider: provider, |
| 683 | + redirectTo: redirectTo, |
| 684 | + scopes: scopes, |
| 685 | + queryParams: queryParams |
| 686 | + ) { @MainActor url in |
| 687 | + try await withCheckedThrowingContinuation { continuation in |
| 688 | + guard let callbackScheme = (configuration.redirectToURL ?? redirectTo)?.scheme else { |
| 689 | + continuation.resume(throwing: AuthError.invalidRedirectScheme) |
| 690 | + return |
703 | 691 | }
|
704 | 692 |
|
705 | 693 | #if !os(tvOS) && !os(watchOS)
|
706 |
| - // Keep a strong reference to presentationContextProvider until the flow completes. |
707 |
| - _ = presentationContextProvider |
| 694 | + var presentationContextProvider: DefaultPresentationContextProvider? |
708 | 695 | #endif
|
709 |
| - } |
710 |
| - |
711 |
| - configure(session) |
712 | 696 |
|
713 |
| - #if !os(tvOS) && !os(watchOS) |
714 |
| - if session.presentationContextProvider == nil { |
715 |
| - presentationContextProvider = DefaultPresentationContextProvider() |
716 |
| - session.presentationContextProvider = presentationContextProvider |
| 697 | + let session = ASWebAuthenticationSession( |
| 698 | + url: url, |
| 699 | + callbackURLScheme: callbackScheme |
| 700 | + ) { url, error in |
| 701 | + if let error { |
| 702 | + continuation.resume(throwing: error) |
| 703 | + } else if let url { |
| 704 | + continuation.resume(returning: url) |
| 705 | + } else { |
| 706 | + continuation.resume(throwing: AuthError.missingURL) |
| 707 | + } |
| 708 | + |
| 709 | + #if !os(tvOS) && !os(watchOS) |
| 710 | + // Keep a strong reference to presentationContextProvider until the flow completes. |
| 711 | + _ = presentationContextProvider |
| 712 | + #endif |
717 | 713 | }
|
718 |
| - #endif |
719 | 714 |
|
720 |
| - session.start() |
| 715 | + configure(session) |
| 716 | + |
| 717 | + #if !os(tvOS) && !os(watchOS) |
| 718 | + if session.presentationContextProvider == nil { |
| 719 | + presentationContextProvider = DefaultPresentationContextProvider() |
| 720 | + session.presentationContextProvider = presentationContextProvider |
| 721 | + } |
| 722 | + #endif |
| 723 | + |
| 724 | + session.start() |
| 725 | + } |
721 | 726 | }
|
722 | 727 | }
|
723 |
| - } |
| 728 | + #endif |
724 | 729 |
|
725 | 730 | /// Gets the session data from a OAuth2 callback URL.
|
726 | 731 | @discardableResult
|
@@ -1281,7 +1286,7 @@ extension AuthClient {
|
1281 | 1286 | public static let authChangeSessionInfoKey = "AuthClient.authChangeSession"
|
1282 | 1287 | }
|
1283 | 1288 |
|
1284 |
| -#if !os(tvOS) && !os(watchOS) |
| 1289 | +#if canImport(AuthenticationServices) && !os(tvOS) && !os(watchOS) |
1285 | 1290 | @MainActor
|
1286 | 1291 | final class DefaultPresentationContextProvider: NSObject,
|
1287 | 1292 | ASWebAuthenticationPresentationContextProviding
|
|
0 commit comments