Skip to content

Commit 9395f05

Browse files
author
Guilherme Souza
committed
wip
1 parent 04a1382 commit 9395f05

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

Examples/Examples/Auth/GoogleSignInWithWebFlow.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ struct GoogleSignInWithWebFlow: View {
2222
@MainActor
2323
private func signInWithGoogleButtonTapped() async {
2424
do {
25-
let contextProvider = DefaultPresentationContextProvider()
26-
27-
try await supabase.auth.signInWithOAuth(provider: .google) {
28-
$0.presentationContextProvider = contextProvider
29-
}
25+
try await supabase.auth.signInWithOAuth(provider: .google, using: webAuthenticationSession)
3026
} catch {
3127
print("failed to sign in with Google: \(error)")
3228
}
@@ -36,21 +32,3 @@ struct GoogleSignInWithWebFlow: View {
3632
#Preview {
3733
GoogleSignInWithWebFlow()
3834
}
39-
40-
final class DefaultPresentationContextProvider: NSObject,
41-
ASWebAuthenticationPresentationContextProviding, Sendable
42-
{
43-
func presentationAnchor(for _: ASWebAuthenticationSession) -> ASPresentationAnchor {
44-
if Thread.isMainThread {
45-
return MainActor.assumeIsolated {
46-
ASPresentationAnchor()
47-
}
48-
} else {
49-
return DispatchQueue.main.sync {
50-
MainActor.assumeIsolated {
51-
ASPresentationAnchor()
52-
}
53-
}
54-
}
55-
}
56-
}

Sources/Auth/AuthClient.swift

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Sources/Auth/Deprecated.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extension AuthClient {
105105
deprecated,
106106
message: "Replace usages of this initializer with new init(url:headers:flowType:localStorage:logger:encoder:decoder:fetch)"
107107
)
108-
public init(
108+
public convenience init(
109109
url: URL,
110110
headers: [String: String] = [:],
111111
flowType: AuthFlowType = Configuration.defaultFlowType,

Sources/Auth/Internal/Dependencies.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ var Current: Dependencies {
3131
}
3232

3333
@propertyWrapper
34-
struct Dependency<Value> {
34+
struct Dependency<Value: Sendable>: Sendable {
3535
var wrappedValue: Value {
36-
Current[keyPath: keyPath]
36+
Current[keyPath: keyPath.value]
3737
}
3838

39-
let keyPath: KeyPath<Dependencies, Value>
39+
let keyPath: UncheckedSendable<KeyPath<Dependencies, Value>>
4040

4141
init(_ keyPath: KeyPath<Dependencies, Value>) {
42-
self.keyPath = keyPath
42+
self.keyPath = UncheckedSendable(keyPath)
4343
}
4444
}

0 commit comments

Comments
 (0)