@@ -6,7 +6,7 @@ import AuthenticationServices
66 import FoundationNetworking
77#endif
88
9- public actor AuthClient {
9+ public final class AuthClient : Sendable {
1010 /// FetchHandler is a type alias for asynchronous network request handling.
1111 public typealias FetchHandler = @Sendable (
1212 _ request: URLRequest
@@ -111,7 +111,7 @@ public actor AuthClient {
111111 /// - encoder: The JSON encoder to use for encoding requests.
112112 /// - decoder: The JSON decoder to use for decoding responses.
113113 /// - fetch: The asynchronous fetch handler for network requests.
114- public init (
114+ public convenience init (
115115 url: URL ,
116116 headers: [ String : String ] = [ : ] ,
117117 flowType: AuthFlowType = AuthClient . Configuration. defaultFlowType,
@@ -141,7 +141,7 @@ public actor AuthClient {
141141 ///
142142 /// - Parameters:
143143 /// - configuration: The client configuration.
144- public init ( configuration: Configuration ) {
144+ public convenience init ( configuration: Configuration ) {
145145 let api = APIClient . live (
146146 configuration: configuration,
147147 http: HTTPClient (
@@ -1190,3 +1190,41 @@ extension AuthClient {
11901190 /// ``AuthClient/didChangeAuthStateNotification`` notification.
11911191 public static let authChangeSessionInfoKey = " AuthClient.authChangeSession "
11921192}
1193+
1194+ #if canImport(SwiftUI)
1195+ import SwiftUI
1196+
1197+ extension AuthClient {
1198+ @available ( iOS 16 . 4 , * )
1199+ @discardableResult
1200+ public func signInWithOAuth(
1201+ provider: Provider ,
1202+ using webAuthenticationSession: WebAuthenticationSession ,
1203+ preferredBrowserSession: WebAuthenticationSession . BrowserSession ? = nil ,
1204+ redirectTo: URL ? = nil ,
1205+ scopes: String ? = nil ,
1206+ queryParams: [ ( name: String , value: String ? ) ] = [ ]
1207+ ) async throws -> Session {
1208+ guard let redirectTo = ( redirectTo ?? configuration. redirectToURL) ,
1209+ let callbackScheme = redirectTo. scheme
1210+ else {
1211+ throw AuthError . invalidRedirectScheme
1212+ }
1213+
1214+ let url = try getOAuthSignInURL (
1215+ provider: provider,
1216+ scopes: scopes,
1217+ redirectTo: redirectTo,
1218+ queryParams: queryParams
1219+ )
1220+
1221+ let resultURL = try await webAuthenticationSession. authenticate (
1222+ using: url,
1223+ callbackURLScheme: callbackScheme,
1224+ preferredBrowserSession: preferredBrowserSession
1225+ )
1226+
1227+ return try await session ( from: resultURL)
1228+ }
1229+ }
1230+ #endif
0 commit comments