Skip to content

Commit c827d5f

Browse files
committed
feat: add AdminAPI and deleteUser method
1 parent 148e3dc commit c827d5f

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Sources/Auth/AuthAdmin.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// AuthAdmin.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 25/01/24.
6+
//
7+
8+
import Foundation
9+
@_spi(Internal) import _Helpers
10+
11+
public actor AuthAdmin {
12+
private var configuration: AuthClient.Configuration {
13+
Dependencies.current.value!.configuration
14+
}
15+
16+
private var api: APIClient {
17+
Dependencies.current.value!.api
18+
}
19+
20+
public init() {}
21+
22+
/// Delete a user. Requires `service_role` key.
23+
/// - Parameter id: The id of the user you want to delete.
24+
/// - Parameter shouldSoftDelete: If true, then the user will be soft-deleted (setting
25+
/// `deleted_at` to the current timestamp and disabling their account while preserving their data)
26+
/// from the auth schema.
27+
///
28+
/// - Warning: Never expose your `service_role` key on the client.
29+
@discardableResult
30+
public func deleteUser(id: String, shouldSoftDelete: Bool = true) async throws -> User {
31+
try await api.execute(
32+
Request(
33+
path: "/admin/users/\(id)",
34+
method: .delete,
35+
body: configuration.encoder.encode(DeleteUserRequest(shouldSoftDelete: shouldSoftDelete))
36+
)
37+
)
38+
.decoded(decoder: configuration.decoder)
39+
}
40+
}

Sources/Auth/AuthClient.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public actor AuthClient {
9595
/// Namespace for accessing multi-factor authentication API.
9696
public let mfa: AuthMFA
9797

98+
/// Namespace for the GoTrue admin methods.
99+
/// - Warning: This methods requires `service_role` key, be careful to never expose `service_role`
100+
/// key in the client.
101+
public let admin: AuthAdmin
102+
98103
/// Initializes a AuthClient with optional parameters.
99104
///
100105
/// - Parameters:
@@ -162,6 +167,7 @@ public actor AuthClient {
162167
logger: SupabaseLogger?
163168
) {
164169
mfa = AuthMFA()
170+
admin = AuthAdmin()
165171

166172
Dependencies.current.setValue(
167173
Dependencies(

Sources/Auth/Types.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,7 @@ public struct WeakPassword: Codable, Hashable, Sendable {
646646
/// `pwned`.
647647
public let reasons: [String]
648648
}
649+
650+
struct DeleteUserRequest: Encodable {
651+
let shouldSoftDelete: Bool
652+
}

0 commit comments

Comments
 (0)