Skip to content

Commit 08f020f

Browse files
authored
fix: ASWebAuthenticationPresentationContextProviding requires @mainactor (#108)
* chore: update dependencies * chore: fix issue with MainActor presentation context * chore: make requests numbered * chore: update tests --------- Co-authored-by: danthorpe <danthorpe@users.noreply.github.com>
1 parent 5642665 commit 08f020f

File tree

9 files changed

+25
-14
lines changed

9 files changed

+25
-14
lines changed

Examples/Examples.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/Spotify/App/SignedOut/SignedOutFeature.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ struct SignedOutFeature {
3737
state = .failed
3838
return .none
3939
case .view(.signInButtonTapped):
40-
return .run { send in
41-
try await spotify.signIn(nil)
40+
return .run { @MainActor send in
41+
try await spotify.signIn(presentationContext: DefaultPresentationContext())
4242
await send(.signInResponse(.success(true)))
4343
} catch: { error, send in
4444
await send(.signInResponse(.failure(error)))

Examples/Spotify/App/SpotifyClient/SpotifyClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extension Spotify {
4747
.logged(using: Logger(subsystem: "works.dan.networking.examples.spotify", category: "Spotify API"))
4848
.server(prefixPath: "v1")
4949
.server(authority: "api.spotify.com")
50+
.numbered()
5051
.authenticated(
5152
oauth: .spotify(
5253
clientId: "b4937bc99da547b4b90559f5024d8467",

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/OAuth/ASWebAuthenticationSession+.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ extension ASWebAuthenticationSession {
4343
}
4444
}
4545

46-
final class DefaultPresentationContext: NSObject, ASWebAuthenticationPresentationContextProviding {
47-
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
46+
@MainActor public final class DefaultPresentationContext: NSObject, ASWebAuthenticationPresentationContextProviding {
47+
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
4848
#if os(macOS)
4949
ASPresentationAnchor()
5050
#else

Sources/OAuth/OAuthDelegate.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension OAuth {
1111
let upstream: any NetworkingComponent
1212
var system: any OAuthSystem<Credentials>
1313

14-
var presentationContext: (any ASWebAuthenticationPresentationContextProviding) = DefaultPresentationContext()
14+
var presentationContext: (any ASWebAuthenticationPresentationContextProviding)?
1515

1616
@Dependency(\.webAuthenticationSession) var webAuthenticationSession
1717

@@ -46,6 +46,10 @@ extension OAuth.Delegate: AuthenticationDelegate {
4646
codeChallenge: codeChallenge
4747
)
4848

49+
guard let presentationContext else {
50+
throw OAuth.Error.presentationContextNotSet
51+
}
52+
4953
let callbackURL = try await webAuthenticationSession.start(
5054
state: state,
5155
authorizationURL: url,

Sources/OAuth/OAuthError.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Foundation
33
extension OAuth {
44
public enum Error: Swift.Error, Equatable {
55
case oauthNotInstalled
6+
case presentationContextNotSet
67
case invalidAuthorizationEndpoint(String)
78
case invalidTokenEndpoint(String)
89

Tests/OAuthTests/OAuthDelegateTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ final class OAuthDelegateTests: OAuthTestCase {
4343
.server(authenticationMethod: .stub)
4444
.authenticated(oauth: stub)
4545

46+
// Configure Network
47+
try await network.stubOAuthSystem {
48+
await $0.set(presentationContext: DefaultPresentationContext())
49+
}
50+
4651
// Make a request to protected resource
4752
var request = HTTPRequestData(path: "/protected-resource")
4853
request.authenticationMethod = .stub

Tests/OAuthTests/StubOAuthSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct StubOAuthSystem: StandardOAuthSystem {
2121

2222
extension NetworkingComponent {
2323
func stubOAuthSystem<ReturnValue>(
24-
perform: (any OAuthProxy<StubOAuthSystem.Credentials>) async throws -> ReturnValue
24+
perform: @MainActor (any OAuthProxy<StubOAuthSystem.Credentials>) async throws -> ReturnValue
2525
) async throws -> ReturnValue {
2626
try await oauth(of: StubOAuthSystem.Credentials.self, perform: perform)
2727
}

0 commit comments

Comments
 (0)