Skip to content

Commit eedd2ba

Browse files
grdsdevGuilherme Souza
authored andcommitted
wip
1 parent 43fbb6e commit eedd2ba

File tree

4 files changed

+67
-41
lines changed

4 files changed

+67
-41
lines changed

Sources/Auth/AuthAdmin.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public struct AuthAdmin: Sendable {
173173
/// - Throws: An error if the link generation fails.
174174
/// - Returns: The generated link.
175175
public func generateLink(params: GenerateLinkParams) async throws -> GenerateLinkResponse {
176-
let response = try await api.execute(
176+
try await api.execute(
177177
HTTPRequest(
178178
url: configuration.url.appendingPathComponent("admin/generate_link").appendingQueryItems(
179179
[
@@ -188,12 +188,7 @@ public struct AuthAdmin: Sendable {
188188
method: .post,
189189
body: encoder.encode(params.body)
190190
)
191-
).decoded(as: AnyJSON.self, decoder: configuration.decoder)
192-
193-
let properties = try response.decode(as: GenerateLinkProperties.self)
194-
let user = try response.decode(as: User.self)
195-
196-
return GenerateLinkResponse(properties: properties, user: user)
191+
).decoded(decoder: configuration.decoder)
197192
}
198193
}
199194

Sources/Auth/Types.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ public struct UserIdentity: Codable, Hashable, Identifiable, Sendable {
249249
self.updatedAt = updatedAt
250250
}
251251

252+
enum CodingKeys: CodingKey {
253+
case id
254+
case identityId
255+
case userId
256+
case identityData
257+
case provider
258+
case createdAt
259+
case lastSignInAt
260+
case updatedAt
261+
}
262+
252263
public init(from decoder: any Decoder) throws {
253264
let container = try decoder.container(keyedBy: CodingKeys.self)
254265

@@ -975,12 +986,19 @@ public struct GenerateLinkParams: Sendable {
975986

976987
}
977988

978-
/// The response from the `generateLink` function.
979-
public struct GenerateLinkResponse: Hashable, Sendable {
989+
/// The response from the ``AuthAdmin/generateLink(params:)`` function.
990+
public struct GenerateLinkResponse: Hashable, Sendable, Decodable {
980991
/// The properties related to the email link generated.
981992
public let properties: GenerateLinkProperties
982993
/// The user that the email link is associated to.
983994
public let user: User
995+
996+
public init(from decoder: any Decoder) throws {
997+
let container = try decoder.singleValueContainer()
998+
999+
self.user = try container.decode(User.self)
1000+
self.properties = try container.decode(GenerateLinkProperties.self)
1001+
}
9841002
}
9851003

9861004
/// The properties related to the email link generated.

Sources/Helpers/AnyJSON/AnyJSON+Codable.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ import Foundation
99

1010
extension AnyJSON {
1111
/// The decoder instance used for transforming AnyJSON to some Codable type.
12-
public static let decoder: JSONDecoder = {
13-
JSONDecoder.supabase()
14-
}()
12+
@TaskLocal public static var decoder: JSONDecoder = JSONDecoder.supabase()
1513

1614
/// The encoder instance used for transforming AnyJSON to some Codable type.
17-
public static let encoder: JSONEncoder = {
18-
JSONEncoder.supabase()
19-
}()
15+
@TaskLocal public static var encoder: JSONEncoder = JSONEncoder.supabase()
2016
}
2117

2218
extension AnyJSON {

Tests/AuthTests/AuthClientTests.swift

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ final class AuthClientTests: XCTestCase {
410410
{
411411
"url": "\(url)"
412412
}
413-
""".utf8)
413+
""".utf8
414+
)
414415
]
415416
)
416417
.snapshotRequest {
@@ -454,7 +455,8 @@ final class AuthClientTests: XCTestCase {
454455
{
455456
"url": "\(url)"
456457
}
457-
""".utf8)
458+
""".utf8
459+
)
458460
]
459461
)
460462
.snapshotRequest {
@@ -805,7 +807,8 @@ final class AuthClientTests: XCTestCase {
805807
func testGetOAuthSignInURL() async throws {
806808
let sut = makeSUT(flowType: .implicit)
807809
let url = try sut.getOAuthSignInURL(
808-
provider: .github, scopes: "read,write",
810+
provider: .github,
811+
scopes: "read,write",
809812
redirectTo: URL(string: "https://dummy-url.com/redirect")!,
810813
queryParams: [("extra_key", "extra_value")]
811814
)
@@ -1496,7 +1499,8 @@ final class AuthClientTests: XCTestCase {
14961499
"id": "12345",
14971500
"type": "totp"
14981501
}
1499-
""".utf8)
1502+
""".utf8
1503+
)
15001504
]
15011505
)
15021506
.snapshotRequest {
@@ -1541,7 +1545,8 @@ final class AuthClientTests: XCTestCase {
15411545
"id": "12345",
15421546
"type": "totp"
15431547
}
1544-
""".utf8)
1548+
""".utf8
1549+
)
15451550
]
15461551
)
15471552
.snapshotRequest {
@@ -1586,7 +1591,8 @@ final class AuthClientTests: XCTestCase {
15861591
"id": "12345",
15871592
"type": "phone"
15881593
}
1589-
""".utf8)
1594+
""".utf8
1595+
)
15901596
]
15911597
)
15921598
.snapshotRequest {
@@ -1634,7 +1640,8 @@ final class AuthClientTests: XCTestCase {
16341640
"type": "totp",
16351641
"expires_at": 12345678
16361642
}
1637-
""".utf8)
1643+
""".utf8
1644+
)
16381645
]
16391646
)
16401647
.snapshotRequest {
@@ -1680,7 +1687,8 @@ final class AuthClientTests: XCTestCase {
16801687
"type": "phone",
16811688
"expires_at": 12345678
16821689
}
1683-
""".utf8)
1690+
""".utf8
1691+
)
16841692
]
16851693
)
16861694
.snapshotRequest {
@@ -1800,7 +1808,8 @@ final class AuthClientTests: XCTestCase {
18001808
"type": "totp",
18011809
"expires_at": 12345678
18021810
}
1803-
""".utf8)
1811+
""".utf8
1812+
)
18041813
]
18051814
)
18061815
.snapshotRequest {
@@ -1955,7 +1964,7 @@ final class AuthClientTests: XCTestCase {
19551964
--header "X-Client-Info: auth-swift/0.0.0" \
19561965
--header "X-Supabase-Api-Version: 2024-01-01" \
19571966
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
1958-
"http://localhost:54321/auth/v1/admin/users/859f402d-b3de-4105-a1b9-932836d9193b"
1967+
"http://localhost:54321/auth/v1/admin/users/859F402D-B3DE-4105-A1B9-932836D9193B"
19591968
"""#
19601969
}
19611970
.register()
@@ -1966,7 +1975,7 @@ final class AuthClientTests: XCTestCase {
19661975
}
19671976

19681977
func testUpdateUserById() async throws {
1969-
let id = UUID(uuidString:"859f402d-b3de-4105-a1b9-932836d9193b")!
1978+
let id = UUID(uuidString: "859f402d-b3de-4105-a1b9-932836d9193b")!
19701979
let sut = makeSUT()
19711980

19721981
Mock(
@@ -1984,7 +1993,7 @@ final class AuthClientTests: XCTestCase {
19841993
--header "X-Supabase-Api-Version: 2024-01-01" \
19851994
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
19861995
--data "{\"phone\":\"1234567890\",\"user_metadata\":{\"full_name\":\"John Doe\"}}" \
1987-
"http://localhost:54321/auth/v1/admin/users/859f402d-b3de-4105-a1b9-932836d9193b"
1996+
"http://localhost:54321/auth/v1/admin/users/859F402D-B3DE-4105-A1B9-932836D9193B"
19881997
"""#
19891998
}
19901999
.register()
@@ -2037,21 +2046,26 @@ final class AuthClientTests: XCTestCase {
20372046
func testGenerateLink_signUp() async throws {
20382047
let sut = makeSUT()
20392048

2049+
let user = User(fromMockNamed: "user")
2050+
let encoder = JSONEncoder.supabase()
2051+
encoder.keyEncodingStrategy = .convertToSnakeCase
2052+
2053+
let userData = try encoder.encode(user)
2054+
var json = try JSONSerialization.jsonObject(with: userData, options: []) as! [String: Any]
2055+
2056+
json["action_link"] = "https://example.com/auth/v1/verify?type=signup&token={hashed_token}&redirect_to=https://example.com"
2057+
json["email_otp"] = "123456"
2058+
json["hashed_token"] = "hashed_token"
2059+
json["redirect_to"] = "https://example.com"
2060+
json["verification_type"] = "signup"
2061+
2062+
let responseData = try JSONSerialization.data(withJSONObject: json)
2063+
20402064
Mock(
20412065
url: clientURL.appendingPathComponent("admin/generate_link"),
20422066
statusCode: 200,
20432067
data: [
2044-
.post: try! AuthClient.Configuration.jsonEncoder.encode([
2045-
"properties": [
2046-
"action_link":
2047-
"https://example.com/auth/v1/verify?type=signup&token={hashed_token}&redirect_to=https://example.com",
2048-
"email_otp": "123456",
2049-
"hashed_token": "hashed_token",
2050-
"redirect_to": "https://example.com",
2051-
"verification_type": "signup",
2052-
],
2053-
"user": AnyJSON(User(fromMockNamed: "user")),
2054-
])
2068+
.post: responseData
20552069
]
20562070
)
20572071
.register()
@@ -2190,13 +2204,16 @@ extension HTTPResponse {
21902204

21912205
enum MockData {
21922206
static let listUsersResponse = try! Data(
2193-
contentsOf: Bundle.module.url(forResource: "list-users-response", withExtension: "json")!)
2207+
contentsOf: Bundle.module.url(forResource: "list-users-response", withExtension: "json")!
2208+
)
21942209

21952210
static let session = try! Data(
2196-
contentsOf: Bundle.module.url(forResource: "session", withExtension: "json")!)
2211+
contentsOf: Bundle.module.url(forResource: "session", withExtension: "json")!
2212+
)
21972213

21982214
static let user = try! Data(
2199-
contentsOf: Bundle.module.url(forResource: "user", withExtension: "json")!)
2215+
contentsOf: Bundle.module.url(forResource: "user", withExtension: "json")!
2216+
)
22002217

22012218
static let anonymousSignInResponse = try! Data(
22022219
contentsOf: Bundle.module.url(forResource: "anonymous-sign-in-response", withExtension: "json")!

0 commit comments

Comments
 (0)