Skip to content

Commit

Permalink
feat: more ways to plug signin and fix pagination issues and fix upda…
Browse files Browse the repository at this point in the history
…te user as admin issue
  • Loading branch information
zoedsoupe committed Apr 18, 2024
1 parent 86c9b36 commit 1769e2a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/supabase/go_true/admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ defmodule Supabase.GoTrue.Admin do
@impl true
def update_user_by_id(client, user_id, attrs) when is_client(client) do
with {:ok, client} <- Client.retrieve_client(client),
{:ok, params} <- AdminUserParams.parse(attrs),
{:ok, params} <- AdminUserParams.parse_update(attrs),
{:ok, response} <- AdminHandler.update_user(client, user_id, params) do
User.parse(response)
end
Expand Down
9 changes: 5 additions & 4 deletions lib/supabase/go_true/admin_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ defmodule Supabase.GoTrue.AdminHandler do
end

def list_users(%Client{} = client, params) do
body = %{
page: to_string(Map.get(params, :page, 0)),
per_page: to_string(Map.get(params, :per_page, 0))
query = URI.encode_query %{
page: to_string(Map.get(params, :page, 1)),
per_page: to_string(Map.get(params, :per_page, nil))
}

headers = Fetcher.apply_client_headers(client)

client
|> Client.retrieve_auth_url(@users)
|> Fetcher.get(body, headers, resolve_json: false)
|> URI.append_query(query)
|> Fetcher.get(nil, headers, resolve_json: false)
|> case do
{:ok, resp} when resp.status == 200 -> {:ok, Map.update!(resp, :body, &Jason.decode!/1)}
{:ok, resp} -> {:ok, Fetcher.format_response(resp)}
Expand Down
7 changes: 5 additions & 2 deletions lib/supabase/go_true/live_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ defmodule Supabase.GoTrue.LiveView do
alias Supabase.GoTrue.User

@client Application.compile_env!(:supabase_gotrue, :authentication_client)
@endpoint Application.compile_env!(:supabase_gotrue, :endpoint)
@signed_in_path Application.compile_env(:supabase_gotrue, :signed_in_path)

# just to ensure config:
Application.compile_env!(:supabase_gotrue, :endpoint)

@doc """
Logs out the user from the session and broadcasts a disconnect event.
Expand All @@ -55,7 +57,8 @@ defmodule Supabase.GoTrue.LiveView do
user_token = socket.assigns[:user_token]
session = %Session{access_token: user_token}
user_token && Admin.sign_out(@client, session, scope)
@endpoint.broadcast_from(self(), socket.id, "disconnect", %{user: user})
endpoint = Application.fetch_env!(:supabase_gotrue, :endpoint)
endpoint.broadcast_from(self(), socket.id, "disconnect", %{user: user})
end

@doc """
Expand Down
40 changes: 38 additions & 2 deletions lib/supabase/go_true/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ defmodule Supabase.GoTrue.Plug do
You can set up these config in your `config.exs`:
```
config :supabase_gotrue,
endpoint: YourApp.Endpoint,
signed_in_path: "/dashboard",
not_authenticated_path: "/login",
authentication_client: :my_supabase_potion_client_name
```
Expand Down Expand Up @@ -45,7 +45,36 @@ defmodule Supabase.GoTrue.Plug do
For more information on how Supabase login with email and password works, check `Supabase.GoTrue.sign_in_with_password/2`
"""
def log_in_with_password(conn, params \\ %{}) do
{:ok, session} = GoTrue.sign_in_with_password(@client, params)
with {:ok, session} <- GoTrue.sign_in_with_password(@client, params) do
do_login(conn, session, params)
end
end

def log_in_with_id_token(conn, params \\ %{}) do
with {:ok, session} <- GoTrue.sign_in_with_id_token(@client, params) do
do_login(conn, session, params)
end
end

def log_in_with_oauth(conn, params \\ %{}) do
with {:ok, session} <- GoTrue.sign_in_with_oauth(@client, params) do
do_login(conn, session, params)
end
end

def log_in_with_sso(conn, params \\ %{}) do
with {:ok, session} <- GoTrue.sign_in_with_sso(@client, params) do
do_login(conn, session, params)
end
end

def log_in_with_otp(conn, params \\ %{}) do
with {:ok, session} <- GoTrue.sign_in_with_otp(@client, params) do
do_login(conn, session, params)
end
end

defp do_login(conn, session, params) do
user_return_to = get_session(conn, :user_return_to)

conn
Expand Down Expand Up @@ -79,6 +108,13 @@ defmodule Supabase.GoTrue.Plug do
session = %Session{access_token: user_token}
user_token && Admin.sign_out(@client, session, scope)

live_socket_id = get_session(conn, :live_socket_id)
endpoint = Application.get_env(:supabase_gotrue, :endpoint)

if live_socket_id && endpoint do
endpoint.broadcast(live_socket_id, "disconnect", %{})
end

conn
|> renew_session()
|> redirect(to: @not_authenticated_path )
Expand Down
6 changes: 6 additions & 0 deletions lib/supabase/go_true/schemas/admin_user_params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ defmodule Supabase.GoTrue.Schemas.AdminUserParams do
|> validate_required_inclusion([:email, :phone])
|> apply_action(:parse)
end

def parse_update(attrs) do
{%{}, @types}
|> cast(attrs, Map.keys(@types))
|> apply_action(:parse)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule SupabaseAuth.MixProject do
use Mix.Project

@version "0.2.1"
@version "0.3.0"
@source_url "https://github.com/zoedsoupe/gotrue-ex"

def project do
Expand Down

0 comments on commit 1769e2a

Please sign in to comment.