Skip to content

Commit

Permalink
feat: add AdminAPI and deleteUser method
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Jan 25, 2024
1 parent 148e3dc commit c827d5f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Sources/Auth/AuthAdmin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// AuthAdmin.swift
//
//
// Created by Guilherme Souza on 25/01/24.
//

import Foundation
@_spi(Internal) import _Helpers

public actor AuthAdmin {
private var configuration: AuthClient.Configuration {
Dependencies.current.value!.configuration
}

private var api: APIClient {
Dependencies.current.value!.api
}

public init() {}

/// Delete a user. Requires `service_role` key.
/// - Parameter id: The id of the user you want to delete.
/// - Parameter shouldSoftDelete: If true, then the user will be soft-deleted (setting
/// `deleted_at` to the current timestamp and disabling their account while preserving their data)
/// from the auth schema.
///
/// - Warning: Never expose your `service_role` key on the client.
@discardableResult
public func deleteUser(id: String, shouldSoftDelete: Bool = true) async throws -> User {
try await api.execute(
Request(
path: "/admin/users/\(id)",
method: .delete,
body: configuration.encoder.encode(DeleteUserRequest(shouldSoftDelete: shouldSoftDelete))
)
)
.decoded(decoder: configuration.decoder)
}
}
6 changes: 6 additions & 0 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public actor AuthClient {
/// Namespace for accessing multi-factor authentication API.
public let mfa: AuthMFA

/// Namespace for the GoTrue admin methods.
/// - Warning: This methods requires `service_role` key, be careful to never expose `service_role`
/// key in the client.
public let admin: AuthAdmin

/// Initializes a AuthClient with optional parameters.
///
/// - Parameters:
Expand Down Expand Up @@ -162,6 +167,7 @@ public actor AuthClient {
logger: SupabaseLogger?
) {
mfa = AuthMFA()
admin = AuthAdmin()

Dependencies.current.setValue(
Dependencies(
Expand Down
4 changes: 4 additions & 0 deletions Sources/Auth/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,7 @@ public struct WeakPassword: Codable, Hashable, Sendable {
/// `pwned`.
public let reasons: [String]
}

struct DeleteUserRequest: Encodable {
let shouldSoftDelete: Bool
}

0 comments on commit c827d5f

Please sign in to comment.