Skip to content

Commit

Permalink
Merge pull request #16 from darrarski/feature/auth-handle-redirect-st…
Browse files Browse the repository at this point in the history
…atus

Return status from redirect hander
  • Loading branch information
darrarski authored Jul 7, 2023
2 parents f3b450d + 360bcec commit 3aeb7ce
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Example/GoogleDriveClientExampleApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ContentView: View {
.onOpenURL { url in
Task<Void, Never> {
do {
try await client.auth.handleRedirect(url)
_ = try await client.auth.handleRedirect(url)
} catch {
log.error("Auth.HandleRedirect failure", metadata: [
"error": "\(error)",
Expand Down
2 changes: 1 addition & 1 deletion Example/GoogleDriveClientExampleApp/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension GoogleDriveClient.Client: DependencyKey {
isSignedIn: { await isSignedIn.value },
isSignedInStream: { isSignedIn.eraseToStream() },
signIn: { await isSignedIn.setValue(true) },
handleRedirect: { _ in },
handleRedirect: { _ in false },
refreshToken: {},
signOut: { await isSignedIn.setValue(false) }
),
Expand Down
6 changes: 4 additions & 2 deletions Sources/GoogleDriveClient/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public struct Auth: Sendable {
public typealias IsSignedIn = @Sendable () async -> Bool
public typealias IsSignedInStream = @Sendable () -> AsyncStream<Bool>
public typealias SignIn = @Sendable () async -> Void
public typealias HandleRedirect = @Sendable (URL) async throws -> Void
public typealias HandleRedirect = @Sendable (URL) async throws -> Bool
public typealias RefreshToken = @Sendable () async throws -> Void
public typealias SignOut = @Sendable () async -> Void

Expand Down Expand Up @@ -104,7 +104,7 @@ extension Auth {
await openURL(url)
},
handleRedirect: { url in
guard url.absoluteString.starts(with: config.redirectURI) else { return }
guard url.absoluteString.starts(with: config.redirectURI) else { return false }

let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
let code = components?.queryItems?.first(where: { $0.name == "code" })?.value
Expand Down Expand Up @@ -164,6 +164,8 @@ extension Auth {
)

await saveCredentials(credentials)

return true
},
refreshToken: {
guard let credentials = await loadCredentials() else { return }
Expand Down
13 changes: 8 additions & 5 deletions Tests/GoogleDriveClientTests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ final class AuthTests: XCTestCase {
httpClient: .unimplemented()
)

try await auth.handleRedirect(URL(string: "https://darrarski.pl")!)
let didHandle = try await auth.handleRedirect(URL(string: "https://darrarski.pl")!)

XCTAssertFalse(didHandle)
}

func testHandleRedirectWithError() async throws {
Expand All @@ -60,7 +62,7 @@ final class AuthTests: XCTestCase {
)

do {
try await auth.handleRedirect(url)
_ = try await auth.handleRedirect(url)
XCTFail("Expected to throw, but didn't")
} catch {
XCTAssertEqual(
Expand All @@ -81,7 +83,7 @@ final class AuthTests: XCTestCase {
)

do {
try await auth.handleRedirect(url)
_ = try await auth.handleRedirect(url)
XCTFail("Expected to throw, but didn't")
} catch {
XCTAssertEqual(
Expand Down Expand Up @@ -128,7 +130,7 @@ final class AuthTests: XCTestCase {
}
)

try await auth.handleRedirect(url)
let didHandle = try await auth.handleRedirect(url)

await httpRequests.withValue {
let url = URL(string: "https://www.googleapis.com/oauth2/v4/token")!
Expand Down Expand Up @@ -157,6 +159,7 @@ final class AuthTests: XCTestCase {
}
let isSignedIn = await auth.isSignedIn()
XCTAssertTrue(isSignedIn)
XCTAssertTrue(didHandle)
}

func testHandleRedirectErrorResponse() async {
Expand All @@ -181,7 +184,7 @@ final class AuthTests: XCTestCase {
)

do {
try await auth.handleRedirect(url)
_ = try await auth.handleRedirect(url)
XCTFail("Expected to throw, but didn't")
} catch {
XCTAssertEqual(
Expand Down

0 comments on commit 3aeb7ce

Please sign in to comment.