Skip to content

Commit 727cdbb

Browse files
authored
Merge pull request #4 from darrarski/feature/auth-handle-redirect-status
Return status from redirect hander
2 parents f66c94a + 43e1382 commit 727cdbb

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

Example/DropboxClientExampleApp/Dependencies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension DropboxClient.Client: DependencyKey {
5050
isSignedIn: { await isSignedIn.value },
5151
isSignedInStream: { isSignedIn.eraseToStream() },
5252
signIn: { await isSignedIn.setValue(true) },
53-
handleRedirect: { _ in },
53+
handleRedirect: { _ in false },
5454
signOut: { await isSignedIn.setValue(false) }
5555
),
5656
listFolder: .init { _ in

Example/DropboxClientExampleApp/ExampleView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ExampleView: View {
2525
.onOpenURL { url in
2626
Task<Void, Never> {
2727
do {
28-
try await client.auth.handleRedirect(url)
28+
_ = try await client.auth.handleRedirect(url)
2929
} catch {
3030
log.error("Auth.HandleRedirect failure", metadata: [
3131
"error": "\(error)",

Sources/DropboxClient/Auth.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public struct Auth: Sendable {
44
public typealias IsSignedIn = @Sendable () async -> Bool
55
public typealias IsSignedInStream = @Sendable () -> AsyncStream<Bool>
66
public typealias SignIn = @Sendable () async -> Void
7-
public typealias HandleRedirect = @Sendable (URL) async throws -> Void
7+
public typealias HandleRedirect = @Sendable (URL) async throws -> Bool
88
public typealias SignOut = @Sendable () async -> Void
99

1010
public enum Error: Swift.Error, Sendable, Equatable {
@@ -109,8 +109,8 @@ extension Auth {
109109
await openURL(url)
110110
},
111111
handleRedirect: { url in
112-
guard let codeVerifier = await codeVerifier.value else { return }
113-
guard url.absoluteString.starts(with: config.redirectURI) else { return }
112+
guard let codeVerifier = await codeVerifier.value else { return false }
113+
guard url.absoluteString.starts(with: config.redirectURI) else { return false }
114114

115115
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
116116
let code = components?.queryItems?.first(where: { $0.name == "code" })?.value
@@ -177,6 +177,8 @@ extension Auth {
177177
)
178178

179179
await saveCredentials(credentials)
180+
181+
return true
180182
},
181183
signOut: {
182184
await saveCredentials(nil)

Tests/DropboxClientTests/AuthTests.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ final class AuthTests: XCTestCase {
5555
pkceUtils: .unimplemented()
5656
)
5757

58-
try await auth.handleRedirect(URL(string: Config.test.redirectURI)!)
58+
let didHandle = try await auth.handleRedirect(URL(string: Config.test.redirectURI)!)
59+
60+
XCTAssertFalse(didHandle)
5961
}
6062

6163
func testIgnoreUnrelatedRedirects() async throws {
@@ -73,7 +75,9 @@ final class AuthTests: XCTestCase {
7375

7476
await auth.signIn()
7577

76-
try await auth.handleRedirect(URL(string: "https://darrarski.pl")!)
78+
let didHandle = try await auth.handleRedirect(URL(string: "https://darrarski.pl")!)
79+
80+
XCTAssertFalse(didHandle)
7781
}
7882

7983
func testHandleRedirectWithError() async throws {
@@ -93,7 +97,7 @@ final class AuthTests: XCTestCase {
9397
await auth.signIn()
9498

9599
do {
96-
try await auth.handleRedirect(url)
100+
_ = try await auth.handleRedirect(url)
97101
XCTFail("Expected to throw, but didn't")
98102
} catch {
99103
XCTAssertEqual(
@@ -120,7 +124,7 @@ final class AuthTests: XCTestCase {
120124
await auth.signIn()
121125

122126
do {
123-
try await auth.handleRedirect(url)
127+
_ = try await auth.handleRedirect(url)
124128
XCTFail("Expected to throw, but didn't")
125129
} catch {
126130
XCTAssertEqual(
@@ -175,7 +179,7 @@ final class AuthTests: XCTestCase {
175179
)
176180

177181
await auth.signIn()
178-
try await auth.handleRedirect(url)
182+
let didHandle = try await auth.handleRedirect(url)
179183

180184
await httpRequests.withValue {
181185
let url = URL(string: "https://api.dropboxapi.com/oauth2/token")!
@@ -208,6 +212,7 @@ final class AuthTests: XCTestCase {
208212
}
209213
let isSignedIn = await auth.isSignedIn()
210214
XCTAssertTrue(isSignedIn)
215+
XCTAssertTrue(didHandle)
211216
}
212217

213218
func testHandleRedirectErrorResponse() async {
@@ -238,7 +243,7 @@ final class AuthTests: XCTestCase {
238243
await auth.signIn()
239244

240245
do {
241-
try await auth.handleRedirect(url)
246+
_ = try await auth.handleRedirect(url)
242247
XCTFail("Expected to throw, but didn't")
243248
} catch {
244249
XCTAssertEqual(

0 commit comments

Comments
 (0)