Skip to content

Commit 9583e85

Browse files
committed
fix after rebase
1 parent 5e64242 commit 9583e85

File tree

5 files changed

+72
-36
lines changed

5 files changed

+72
-36
lines changed

Sources/Auth/AuthClient.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@ public actor AuthClient {
11491149
let updatedUser = try await self.api.execute(
11501150
self.configuration.url.appendingPathComponent("user"),
11511151
method: .put,
1152+
headers: [.authorization(bearerToken: session.accessToken)],
11521153
query: (redirectTo ?? self.configuration.redirectToURL).map {
11531154
["redirect_to": $0.absoluteString]
11541155
},
@@ -1178,14 +1179,14 @@ public actor AuthClient {
11781179
credentials.linkIdentity = true
11791180

11801181
let session = try await api.execute(
1181-
.init(
1182-
url: configuration.url.appendingPathComponent("token"),
1183-
method: .post,
1184-
query: [URLQueryItem(name: "grant_type", value: "id_token")],
1185-
headers: [.authorization: "Bearer \(session.accessToken)"],
1186-
body: configuration.encoder.encode(credentials)
1187-
)
1188-
).decoded(as: Session.self, decoder: configuration.decoder)
1182+
configuration.url.appendingPathComponent("token"),
1183+
method: .post,
1184+
headers: [.authorization(bearerToken: session.accessToken)],
1185+
query: ["grant_type": "id_token"],
1186+
body: credentials
1187+
)
1188+
.serializingDecodable(Session.self, decoder: configuration.decoder)
1189+
.value
11891190

11901191
await sessionManager.update(session)
11911192
eventEmitter.emit(.userUpdated, session: session)

Sources/Auth/Internal/APIClient.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ struct APIClient: Sendable {
2525
private let urlQueryEncoder: any ParameterEncoding = URLEncoding.queryString
2626
private var defaultEncoder: any ParameterEncoder {
2727
JSONParameterEncoder(encoder: configuration.encoder)
28+
}
29+
2830
/// Error codes that should clean up local session.
2931
private let sessionCleanupErrorCodes: [ErrorCode] = [
3032
.sessionNotFound,
3133
.sessionExpired,
3234
.refreshTokenNotFound,
3335
.refreshTokenAlreadyUsed,
3436
]
35-
}
3637

3738
func execute<RequestBody: Encodable & Sendable>(
3839
_ url: URL,
@@ -98,7 +99,11 @@ struct APIClient: Sendable {
9899
// The `session_id` inside the JWT does not correspond to a row in the
99100
// `sessions` table. This usually means the user has signed out, has been
100101
// deleted, or their session has somehow been terminated.
101-
await sessionManager.remove()
102+
103+
// FIXME: ideally should not run on a new Task.
104+
Task {
105+
await sessionManager.remove()
106+
}
102107
eventEmitter.emit(.signedOut, session: nil)
103108
return .sessionMissing
104109
} else {

Sources/PostgREST/PostgrestQueryBuilder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final class PostgrestQueryBuilder: PostgrestBuilder, @unchecked Sendable
5959
if let returning {
6060
prefersHeaders.append("return=\(returning.rawValue)")
6161
}
62-
$0.request.httpBody = try configuration.encoder.encode(values)
62+
$0.request.httpBody = body
6363
if let count {
6464
prefersHeaders.append("count=\(count.rawValue)")
6565
}
@@ -110,7 +110,7 @@ public final class PostgrestQueryBuilder: PostgrestBuilder, @unchecked Sendable
110110
if let onConflict {
111111
$0.query["on_conflict"] = onConflict
112112
}
113-
$0.request.httpBody = try configuration.encoder.encode(values)
113+
$0.request.httpBody = body
114114
if let count {
115115
prefersHeaders.append("count=\(count.rawValue)")
116116
}
@@ -148,7 +148,7 @@ public final class PostgrestQueryBuilder: PostgrestBuilder, @unchecked Sendable
148148
mutableState.withValue {
149149
$0.request.method = .patch
150150
var preferHeaders = ["return=\(returning.rawValue)"]
151-
$0.request.httpBody = try configuration.encoder.encode(values)
151+
$0.request.httpBody = body
152152
if let count {
153153
preferHeaders.append("count=\(count.rawValue)")
154154
}

Sources/PostgREST/PostgrestTransformBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ public class PostgrestTransformBuilder: PostgrestBuilder, @unchecked Sendable {
171171
/// - value: The maximum number of rows that can be affected
172172
public func maxAffected(_ value: Int) -> PostgrestTransformBuilder {
173173
mutableState.withValue {
174-
$0.request.headers.appendOrUpdate(.prefer, value: "handling=strict")
175-
$0.request.headers.appendOrUpdate(.prefer, value: "max-affected=\(value)")
174+
$0.request.headers.appendOrUpdate("Prefer", value: "handling=strict")
175+
$0.request.headers.appendOrUpdate("Prefer", value: "max-affected=\(value)")
176176
}
177177
return self
178178
}

Tests/AuthTests/AuthClientTests.swift

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,15 @@ final class AuthClientTests: XCTestCase {
618618
try await sut.session(from: url)
619619
XCTFail("Expect failure")
620620
} catch {
621-
expectNoDifference(
622-
error as? AuthError,
621+
assertInlineSnapshot(of: error, as: .customDump) {
622+
"""
623623
AuthError.pkceGrantCodeExchange(
624624
message: "Identity is already linked to another user",
625625
error: "server_error",
626626
code: "422"
627627
)
628-
)
628+
"""
629+
}
629630
}
630631
}
631632

@@ -912,7 +913,7 @@ final class AuthClientTests: XCTestCase {
912913
.snapshotRequest {
913914
#"""
914915
curl \
915-
--header "Authorization: bearer accesstoken" \
916+
--header "Authorization: Bearer accesstoken" \
916917
--header "X-Client-Info: auth-swift/0.0.0" \
917918
--header "X-Supabase-Api-Version: 2024-01-01" \
918919
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
@@ -956,7 +957,7 @@ final class AuthClientTests: XCTestCase {
956957
.snapshotRequest {
957958
#"""
958959
curl \
959-
--header "Authorization: bearer accesstoken" \
960+
--header "Authorization: Bearer accesstoken" \
960961
--header "X-Client-Info: auth-swift/0.0.0" \
961962
--header "X-Supabase-Api-Version: 2024-01-01" \
962963
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
@@ -984,8 +985,12 @@ final class AuthClientTests: XCTestCase {
984985

985986
do {
986987
try await sut.session(from: url)
987-
} catch let AuthError.implicitGrantRedirect(message) {
988-
expectNoDifference(message, "Not a valid implicit grant flow URL: \(url)")
988+
} catch {
989+
assertInlineSnapshot(of: error, as: .customDump) {
990+
"""
991+
AuthError.implicitGrantRedirect(message: "Not a valid implicit grant flow URL: https://dummy-url.com/callback#invalid_key=accesstoken&expires_in=60&refresh_token=refreshtoken&token_type=bearer")
992+
"""
993+
}
989994
}
990995
}
991996

@@ -999,8 +1004,12 @@ final class AuthClientTests: XCTestCase {
9991004

10001005
do {
10011006
try await sut.session(from: url)
1002-
} catch let AuthError.implicitGrantRedirect(message) {
1003-
expectNoDifference(message, "Invalid code")
1007+
} catch {
1008+
assertInlineSnapshot(of: error, as: .customDump) {
1009+
"""
1010+
AuthError.implicitGrantRedirect(message: "Invalid code")
1011+
"""
1012+
}
10041013
}
10051014
}
10061015

@@ -1015,7 +1024,7 @@ final class AuthClientTests: XCTestCase {
10151024
.snapshotRequest {
10161025
#"""
10171026
curl \
1018-
--header "Authorization: bearer accesstoken" \
1027+
--header "Authorization: Bearer accesstoken" \
10191028
--header "X-Client-Info: auth-swift/0.0.0" \
10201029
--header "X-Supabase-Api-Version: 2024-01-01" \
10211030
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
@@ -1053,10 +1062,16 @@ final class AuthClientTests: XCTestCase {
10531062

10541063
do {
10551064
try await sut.session(from: url)
1056-
} catch let AuthError.pkceGrantCodeExchange(message, error, code) {
1057-
expectNoDifference(message, "Invalid code")
1058-
expectNoDifference(error, "invalid_grant")
1059-
expectNoDifference(code, "500")
1065+
} catch {
1066+
assertInlineSnapshot(of: error, as: .customDump) {
1067+
"""
1068+
AuthError.pkceGrantCodeExchange(
1069+
message: "Invalid code",
1070+
error: "invalid_grant",
1071+
code: "500"
1072+
)
1073+
"""
1074+
}
10601075
}
10611076
}
10621077

@@ -1070,10 +1085,16 @@ final class AuthClientTests: XCTestCase {
10701085

10711086
do {
10721087
try await sut.session(from: url)
1073-
} catch let AuthError.pkceGrantCodeExchange(message, error, code) {
1074-
expectNoDifference(message, "Error in URL with unspecified error_description.")
1075-
expectNoDifference(error, "invalid_grant")
1076-
expectNoDifference(code, "500")
1088+
} catch {
1089+
assertInlineSnapshot(of: error, as: .customDump) {
1090+
"""
1091+
AuthError.pkceGrantCodeExchange(
1092+
message: "Error in URL with unspecified error_description.",
1093+
error: "invalid_grant",
1094+
code: "500"
1095+
)
1096+
"""
1097+
}
10771098
}
10781099
}
10791100

@@ -2197,7 +2218,11 @@ final class AuthClientTests: XCTestCase {
21972218
_ = try await sut.user()
21982219
XCTFail("Expected failure")
21992220
} catch {
2200-
XCTAssertEqual(error as? AuthError, .sessionMissing)
2221+
assertInlineSnapshot(of: error, as: .customDump) {
2222+
"""
2223+
AuthError.sessionMissing
2224+
"""
2225+
}
22012226
}
22022227
},
22032228
expectedEvents: [.initialSession, .signedOut]
@@ -2236,7 +2261,13 @@ final class AuthClientTests: XCTestCase {
22362261
_ = try await sut.session
22372262
XCTFail("Expected failure")
22382263
} catch {
2239-
XCTAssertEqual(error as? AuthError, .sessionMissing)
2264+
assertInlineSnapshot(of: error, as: .customDump) {
2265+
"""
2266+
AFError.responseValidationFailed(
2267+
reason: .customValidationFailed(error: .sessionMissing)
2268+
)
2269+
"""
2270+
}
22402271
}
22412272
},
22422273
expectedEvents: [.signedOut]
@@ -2248,7 +2279,6 @@ final class AuthClientTests: XCTestCase {
22482279
private func makeSUT(flowType: AuthFlowType = .pkce) -> AuthClient {
22492280
let sessionConfiguration = URLSessionConfiguration.default
22502281
sessionConfiguration.protocolClasses = [MockingURLProtocol.self]
2251-
let session = URLSession(configuration: sessionConfiguration)
22522282

22532283
let encoder = AuthClient.Configuration.jsonEncoder
22542284
encoder.outputFormatting = [.sortedKeys]

0 commit comments

Comments
 (0)