Supabase.Auth.WeakPasswordError (supabase_auth v1.0.1)

View Source

Helpers for working with weak password errors from GoTrue.

GoTrue returns structured weak password data in error responses:

%{
  "error_code" => "weak_password",
  "msg" => "Password too short",
  "weak_password" => %{"reasons" => ["length"]}
}

The default Supabase.HTTPErrorParser preserves this in metadata.resp_body, but it's not ergonomic to pattern-match on. This module enriches the error's metadata with a weak_password_reasons key for easy pattern matching:

{:error, %Supabase.Error{metadata: %{weak_password_reasons: [:length]}}}

Reasons

  • :length — password is too short
  • :characters — password lacks required character diversity
  • :pwned — password was found in a known breach database

Examples

case Auth.sign_up(client, creds) do
  {:error, %Supabase.Error{metadata: %{weak_password_reasons: reasons}}} ->
    # handle weak password with reasons like [:length, :characters]

  {:error, %Supabase.Error{} = err} ->
    # handle other errors
end

Or using the helper functions:

case Auth.sign_up(client, creds) do
  {:error, err} when is_struct(err, Supabase.Error) ->
    if WeakPasswordError.weak_password?(err) do
      reasons = WeakPasswordError.reasons(err)
      # ...
    end
end

Summary

Functions

Enriches a Supabase.Error with weak password data if present in the response body.

Extracts the weak password reasons from an enriched error.

Returns true if the error contains weak password reasons in its metadata.

Types

reason()

@type reason() :: :length | :characters | :pwned

Functions

maybe_enrich(error)

@spec maybe_enrich(Supabase.Error.t()) :: Supabase.Error.t()

Enriches a Supabase.Error with weak password data if present in the response body.

Parses metadata.resp_body for weak password information and adds weak_password_reasons to the metadata map. Returns the error unchanged if it's not a weak password error.

reasons(error)

@spec reasons(Supabase.Error.t()) :: [reason()]

Extracts the weak password reasons from an enriched error.

Returns an empty list if the error is not a weak password error.

weak_password?(error)

@spec weak_password?(Supabase.Error.t()) :: boolean()

Returns true if the error contains weak password reasons in its metadata.