Class: Supabase::Auth::AdminApi

Inherits:
Api
  • Object
show all
Defined in:
lib/supabase/auth/admin_api.rb

Overview

Admin API for managing users with a service role key. Provides CRUD operations on users, link generation, and MFA management.

Direct Known Subclasses

Supabase::Auth::Async::AdminApi

Constant Summary

Constants inherited from Api

Supabase::Auth::Api::CONTENT_TYPE, Supabase::Auth::Api::UUID_REGEX

Instance Attribute Summary collapse

Attributes inherited from Api

#headers, #url

Instance Method Summary collapse

Methods inherited from Api

#_request, #_validate_uuid, #delete, #get, #post, #put

Constructor Details

#initialize(url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ AdminApi

Returns a new instance of AdminApi.

Parameters:

  • url (String)

    The GoTrue API base URL

  • headers (Hash) (defaults to: {})

    Headers including Authorization bearer token

  • http_client (Faraday::Connection, nil) (defaults to: nil)

    Optional custom Faraday client

  • verify (Boolean) (defaults to: true)

    Verify TLS certificates (default true)

  • proxy (String, nil) (defaults to: nil)

    HTTP proxy URL

  • timeout (Numeric, nil) (defaults to: nil)

    Per-request timeout in seconds



19
20
21
22
# File 'lib/supabase/auth/admin_api.rb', line 19

def initialize(url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil)
  super(url: url, headers: headers, http_client: http_client, verify: verify, proxy: proxy, timeout: timeout)
  @oauth = AdminOAuthApi.new(self)
end

Instance Attribute Details

#oauthAdminOAuthApi (readonly)

Returns OAuth 2.1 client administration accessor.

Returns:



11
12
13
# File 'lib/supabase/auth/admin_api.rb', line 11

def oauth
  @oauth
end

Instance Method Details

#_create_oauth_client(params) ⇒ Types::OAuthClientResponse

Creates a new OAuth client. Only relevant when the OAuth 2.1 server is enabled.

Parameters:

  • params (Hash)

    OAuth client attributes (client_name, redirect_uris, etc.)

Returns:



173
174
175
176
# File 'lib/supabase/auth/admin_api.rb', line 173

def _create_oauth_client(params)
  data = post("admin/oauth/clients", body: params)
  Types::OAuthClientResponse.new(client: Types::OAuthClient.from_hash(data))
end

#_delete_factor(params) ⇒ Types::AuthMFAAdminDeleteFactorResponse

Deletes an MFA factor for a user (admin).

Parameters:

  • params (Hash)

    :user_id and :id (both required)

Returns:



120
121
122
123
124
125
126
127
# File 'lib/supabase/auth/admin_api.rb', line 120

def _delete_factor(params)
  user_id = params[:user_id] || params["user_id"]
  factor_id = params[:id] || params["id"]
  _validate_uuid(user_id)
  _validate_uuid(factor_id)
  data = delete("admin/users/#{user_id}/factors/#{factor_id}")
  Types::AuthMFAAdminDeleteFactorResponse.from_hash(data)
end

#_delete_oauth_client(client_id) ⇒ Object

Deletes an OAuth client.

Parameters:

  • client_id (String)

    OAuth client UUID

Raises:

  • (ArgumentError)

    if client_id is not a valid UUID



202
203
204
205
# File 'lib/supabase/auth/admin_api.rb', line 202

def _delete_oauth_client(client_id)
  _validate_uuid(client_id)
  _request("DELETE", "admin/oauth/clients/#{client_id}")
end

#_get_oauth_client(client_id) ⇒ Types::OAuthClientResponse

Gets details of a specific OAuth client.

Parameters:

  • client_id (String)

    OAuth client UUID

Returns:

Raises:

  • (ArgumentError)

    if client_id is not a valid UUID



182
183
184
185
186
# File 'lib/supabase/auth/admin_api.rb', line 182

def _get_oauth_client(client_id)
  _validate_uuid(client_id)
  data = get("admin/oauth/clients/#{client_id}")
  Types::OAuthClientResponse.new(client: Types::OAuthClient.from_hash(data))
end

#_list_factors(params) ⇒ Types::AuthMFAAdminListFactorsResponse

Lists MFA factors for a user (admin).

Parameters:

  • params (Hash)

    :user_id (required)

Returns:



110
111
112
113
114
115
# File 'lib/supabase/auth/admin_api.rb', line 110

def _list_factors(params)
  user_id = params[:user_id] || params["user_id"]
  _validate_uuid(user_id)
  data = get("admin/users/#{user_id}/factors")
  Types::AuthMFAAdminListFactorsResponse.from_hash(data)
end

#_list_oauth_clients(params = nil) ⇒ Types::OAuthClientListResponse

Lists OAuth clients with optional pagination. Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.

Parameters:

  • params (Hash, Types::PageParams, nil) (defaults to: nil)

    optional :page and :per_page

Returns:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/supabase/auth/admin_api.rb', line 133

def _list_oauth_clients(params = nil)
  query = {}
  if params
    page = params[:page] || params["page"]
    per_page = params[:per_page] || params["per_page"]
    query[:page] = page if page
    query[:per_page] = per_page if per_page
  end

  response = _request("GET", "admin/oauth/clients", params: query, no_resolve_json: true)
  body = response.body.is_a?(String) ? JSON.parse(response.body) : (response.body || {})
  result = Types::OAuthClientListResponse.from_hash(body)

  total = response.headers["x-total-count"] || response.headers["X-Total-Count"]
  result.total = total.to_i if total

  links = response.headers["link"] || response.headers["Link"]
  if links
    links.split(",").each do |link|
      parts = link.split(";")
      next unless parts.length >= 2

      page_match = parts[0].split("page=")
      next unless page_match.length >= 2

      page_num = page_match[1].split("&")[0].sub(/>$/, "").to_i
      rel = parts[1].split("=")[1].to_s.delete('"').strip
      case rel
      when "next" then result.next_page = page_num
      when "last" then result.last_page = page_num
      end
    end
  end

  result
end

#_regenerate_oauth_client_secret(client_id) ⇒ Types::OAuthClientResponse

Regenerates the secret for an OAuth client.

Parameters:

  • client_id (String)

    OAuth client UUID

Returns:

Raises:

  • (ArgumentError)

    if client_id is not a valid UUID



211
212
213
214
215
# File 'lib/supabase/auth/admin_api.rb', line 211

def _regenerate_oauth_client_secret(client_id)
  _validate_uuid(client_id)
  data = post("admin/oauth/clients/#{client_id}/regenerate_secret")
  Types::OAuthClientResponse.new(client: Types::OAuthClient.from_hash(data))
end

#_update_oauth_client(client_id, params) ⇒ Types::OAuthClientResponse

Updates an OAuth client.

Parameters:

  • client_id (String)

    OAuth client UUID

  • params (Hash)

    attributes to update

Returns:

Raises:

  • (ArgumentError)

    if client_id is not a valid UUID



193
194
195
196
197
# File 'lib/supabase/auth/admin_api.rb', line 193

def _update_oauth_client(client_id, params)
  _validate_uuid(client_id)
  data = put("admin/oauth/clients/#{client_id}", body: params)
  Types::OAuthClientResponse.new(client: Types::OAuthClient.from_hash(data))
end

#create_user(attributes) ⇒ Types::UserResponse

Creates a new user via the admin API.

Parameters:

  • attributes (Hash)

    user attributes (email, password, user_metadata, app_metadata, etc.)

Returns:



27
28
29
30
# File 'lib/supabase/auth/admin_api.rb', line 27

def create_user(attributes)
  data = post("admin/users", body: attributes)
  Helpers.parse_user_response(data)
end

#delete_user(uid, should_soft_delete: false) ⇒ Object

Deletes a user by their ID.

Parameters:

  • uid (String)

    user UUID

  • should_soft_delete (Boolean) (defaults to: false)

    soft delete instead of hard delete

Raises:

  • (ArgumentError)

    if uid is not a valid UUID



70
71
72
73
# File 'lib/supabase/auth/admin_api.rb', line 70

def delete_user(uid, should_soft_delete: false)
  _validate_uuid(uid)
  _request("DELETE", "admin/users/#{uid}", body: { should_soft_delete: should_soft_delete })
end

Generates email links and OTPs.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/supabase/auth/admin_api.rb', line 76

def generate_link(params)
  options = params[:options] || params["options"] || {}
  body = {
    type: params[:type] || params["type"],
    email: params[:email] || params["email"],
    password: params[:password] || params["password"],
    new_email: params[:new_email] || params["new_email"],
    data: options[:data] || options["data"]
  }
  redirect_to = options[:redirect_to] || options["redirect_to"]
  query = {}
  query["redirect_to"] = redirect_to if redirect_to
  data = post("admin/generate_link", body: body, params: query)
  Helpers.parse_link_response(data)
end

#get_user_by_id(uid) ⇒ Types::UserResponse

Gets a user by their ID.

Parameters:

  • uid (String)

    user UUID

Returns:

Raises:

  • (ArgumentError)

    if uid is not a valid UUID



49
50
51
52
53
# File 'lib/supabase/auth/admin_api.rb', line 49

def get_user_by_id(uid)
  _validate_uuid(uid)
  data = get("admin/users/#{uid}")
  Helpers.parse_user_response(data)
end

#invite_user_by_email(email, options = {}) ⇒ Object

Invites a user by email.



93
94
95
96
97
98
99
100
# File 'lib/supabase/auth/admin_api.rb', line 93

def invite_user_by_email(email, options = {})
  body = { email: email, data: options[:data] || options["data"] }
  redirect_to = options[:redirect_to] || options["redirect_to"]
  query = {}
  query["redirect_to"] = redirect_to if redirect_to
  data = post("invite", body: body, params: query)
  Helpers.parse_user_response(data)
end

#list_users(page: nil, per_page: nil) ⇒ Array<Types::User>

Lists all users.

Parameters:

  • page (Integer, nil) (defaults to: nil)

    page number

  • per_page (Integer, nil) (defaults to: nil)

    users per page

Returns:



36
37
38
39
40
41
42
43
# File 'lib/supabase/auth/admin_api.rb', line 36

def list_users(page: nil, per_page: nil)
  params = {}
  params[:page] = page if page
  params[:per_page] = per_page if per_page
  data = get("admin/users", params: params)
  users = data["users"] || []
  users.map { |u| Types::User.from_hash(u) }
end

#sign_out(access_token, scope = "global") ⇒ Object

Signs out a user by revoking their session via the admin API.



103
104
105
# File 'lib/supabase/auth/admin_api.rb', line 103

def sign_out(access_token, scope = "global")
  _request("POST", "logout", jwt: access_token, params: { "scope" => scope }, no_resolve_json: true)
end

#update_user_by_id(uid, attributes) ⇒ Types::UserResponse

Updates a user by their ID.

Parameters:

  • uid (String)

    user UUID

  • attributes (Hash)

    attributes to update

Returns:

Raises:

  • (ArgumentError)

    if uid is not a valid UUID



60
61
62
63
64
# File 'lib/supabase/auth/admin_api.rb', line 60

def update_user_by_id(uid, attributes)
  _validate_uuid(uid)
  data = put("admin/users/#{uid}", body: attributes)
  Helpers.parse_user_response(data)
end