-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reset password and update user
- Loading branch information
Showing
7 changed files
with
189 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ defmodule Supabase.GoTrue do | |
alias Supabase.GoTrue.Schemas.SignInWithPassword | ||
alias Supabase.GoTrue.Schemas.SignInWithSSO | ||
alias Supabase.GoTrue.Schemas.SignUpWithPassword | ||
alias Supabase.GoTrue.Schemas.UserParams | ||
alias Supabase.GoTrue.Session | ||
alias Supabase.GoTrue.User | ||
alias Supabase.GoTrue.UserHandler | ||
|
@@ -189,6 +190,54 @@ defmodule Supabase.GoTrue do | |
end | ||
end | ||
|
||
@doc """ | ||
Sends a recovery password email for the given email address. | ||
## Parameters | ||
- `client` - The `Supabase` client to use for the request. | ||
- `email` - A valid user email address to recover password | ||
- `opts`: | ||
- `redirect_to`: the url where the user should be redirected to reset their password | ||
- `captcha_token` | ||
## Examples | ||
iex> Supabase.GoTrue.reset_password_for_email(client, "[email protected]", redirect_to: "http://localohst:4000/reset-pass") | ||
:ok | ||
""" | ||
@spec reset_password_for_email(Client.client(), String.t, opts) :: :ok | {:error, term} | ||
when opts: [redirect_to: String.t] | [captcha_token: String.t] | [redirect_to: String.t, captcha_token: String.t] | ||
def reset_password_for_email(client, email, opts) when is_client(client) do | ||
with {:ok, client} <- Client.retrieve_client(client) do | ||
UserHandler.recover_password(client, email, Map.new(opts)) | ||
end | ||
end | ||
|
||
@doc """ | ||
Updates the current logged in user. | ||
## Parameters | ||
- `client` - The `Supabase` client to use for the request. | ||
- `conn` - The current `Plug.Conn` or `Phoenix.LiveView.Socket` to get current user | ||
- `attrs` - Check `UserParams` | ||
## Examples | ||
iex> params = %{email: "[email protected]", password: "new-pass"} | ||
iex> Supabase.GoTrue.update_user(client, conn, params) | ||
{:ok, conn} | ||
""" | ||
@spec update_user(Client.client, conn, UserParams.t) :: {:ok, conn} | {:error, term} | ||
when conn: Plug.Conn.t | Phoenix.LiveView.Socket.t | ||
def update_user(client, conn, attrs) when is_client(client) do | ||
with {:ok, client} <- Client.retrieve_client(client), | ||
{:ok, params} <- UserParams.parse(attrs) do | ||
if conn.assigns.current_user do | ||
UserHandler.update_user(client, conn, params) | ||
else | ||
{:error, :no_user_logged_in} | ||
end | ||
end | ||
end | ||
|
||
defmacrop wrap_gotrue_functions(module) do | ||
quote unquote: false, bind_quoted: [module: module] do | ||
for {fun, arity} <- module.__info__(:functions) do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule Supabase.GoTrue.Schemas.UserParams do | ||
@moduledoc """ | ||
Uuser params schema. This schema is used to validate and parse the parameters for creating a new admin user. | ||
## Fields | ||
* `data` - The metadata to associate with the user. | ||
* `email` - The user's email. | ||
* `phone` - The user's phone. | ||
* `password` - The user's password. | ||
* `nonce` - The user's nonce. | ||
* `email_redirect_to` - The user's nonce. | ||
""" | ||
|
||
import Ecto.Changeset | ||
|
||
@types %{ | ||
data: :map, | ||
email: :string, | ||
phone: :string, | ||
password: :string, | ||
nonce: :string, | ||
email_redirect_to: :string | ||
} | ||
|
||
def parse(attrs) do | ||
{%{}, @types} | ||
|> cast(attrs, Map.keys(@types)) | ||
|> apply_action(:parse) | ||
end | ||
|
||
def parse_update(attrs) do | ||
{%{}, @types} | ||
|> cast(attrs, Map.keys(@types)) | ||
|> apply_action(:parse) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.