Skip to content

Commit

Permalink
fixes for whom want to inject Plug or LiveView code
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Sep 24, 2024
1 parent d58b1f3 commit 2dedcc8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
16 changes: 13 additions & 3 deletions lib/supabase/go_true/live_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
"""

defmacro __using__(opts) do
Supabase.GoTrue.MissingConfig.ensure_opts!(opts, __CALLER__.module)
module = __CALLER__.module
Supabase.GoTrue.MissingConfig.ensure_opts!(opts, module)

Check warning on line 35 in lib/supabase/go_true/live_view.ex

View workflow job for this annotation

GitHub Actions / lint (1.17.0, 27)

Nested modules could be aliased at the top of the invoking module.

client = opts[:client]
signed_in_path = opts[:signed_in_path]
Expand All @@ -47,6 +48,11 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
alias Supabase.GoTrue.Session
alias Supabase.GoTrue.User

Code.ensure_loaded!(unquote(client))
if not function_exported?(unquote(client), :get_client, 0) do
raise Supabase.GoTrue.MissingConfig, key: :client, module: unquote(module)
end

@client unquote(client)
@signed_in_path unquote(signed_in_path)
@not_authenticated_path unquote(not_authenticated_path)
Expand All @@ -67,7 +73,8 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
user = socket.assigns.current_user
user_token = socket.assigns[:user_token]
session = %Session{access_token: user_token}
user_token && Admin.sign_out(@client, session, scope)
{:ok, client} = @client.get_client()
user_token && Admin.sign_out(client, session, scope)
# avoid compilation warnings
apply(unquote(endpoint), :broadcast_from, [

Check warning on line 79 in lib/supabase/go_true/live_view.ex

View workflow job for this annotation

GitHub Actions / lint (1.17.0, 27)

Avoid `apply/2` and `apply/3` when the number of arguments is known.
self(),
Expand Down Expand Up @@ -105,6 +112,7 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
end
"""
def on_mount(:mount_current_user, _params, session, socket) do
IO.inspect(session, label: "SESSION")

Check warning on line 115 in lib/supabase/go_true/live_view.ex

View workflow job for this annotation

GitHub Actions / lint (1.17.0, 27)

There should be no calls to `IO.inspect/1`.
{:cont, mount_current_user(session, socket)}
end

Expand Down Expand Up @@ -144,7 +152,9 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
end

defp maybe_get_current_user(session) do
case GoTrue.get_user(@client, session) do
{:ok, client} = @client.get_client()

case GoTrue.get_user(client, session) do
{:ok, %User{} = user} -> user
_ -> nil
end
Expand Down
12 changes: 12 additions & 0 deletions lib/supabase/go_true/missing_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ defmodule Supabase.GoTrue.MissingConfig do
end

@impl true
def exception(key: :client, module: module) do
desc = get_missing_desc(:client, module)

message = """
The `:client` option need to be a module that implements the `Supabase.Client.Behaviour` behaviour.
#{desc}
"""

%__MODULE__{message: message}
end

def exception(key: :auth_module) do
message = """
You must set up the `:auth_module` option in your config.exs file. This should be the module that contains your authentication handler.
Expand Down
49 changes: 34 additions & 15 deletions lib/supabase/go_true/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ if Code.ensure_loaded?(Plug) do
"""

defmacro __using__(opts) do
Supabase.GoTrue.MissingConfig.ensure_opts!(opts, __CALLER__.module)
module = __CALLER__.module
Supabase.GoTrue.MissingConfig.ensure_opts!(opts, module)

Check warning on line 23 in lib/supabase/go_true/plug.ex

View workflow job for this annotation

GitHub Actions / lint (1.17.0, 27)

Nested modules could be aliased at the top of the invoking module.

client = opts[:client]
signed_in_path = opts[:signed_in_path]
Expand All @@ -37,6 +38,11 @@ if Code.ensure_loaded?(Plug) do
alias Supabase.GoTrue.Session
alias Supabase.GoTrue.User

Code.ensure_loaded!(unquote(client))
if not function_exported?(unquote(client), :get_client, 0) do
raise Supabase.GoTrue.MissingConfig, key: :client, module: unquote(module)
end

@client unquote(client)
@signed_in_path unquote(signed_in_path)
@not_authenticated_path unquote(not_authenticated_path)
Expand All @@ -49,39 +55,49 @@ if Code.ensure_loaded?(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
with {:ok, session} <- GoTrue.sign_in_with_password(@client, params) do
do_login(conn, session, params)
{:ok, client} = @client.get_client()

with {:ok, session} <- GoTrue.sign_in_with_password(client, params) do
do_login(client, 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)
{:ok, client} = @client.get_client()

with {:ok, session} <- GoTrue.sign_in_with_id_token(client, params) do
do_login(client, 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)
{:ok, client} = @client.get_client()

with {:ok, session} <- GoTrue.sign_in_with_oauth(client, params) do
do_login(client, 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)
{:ok, client} = @client.get_client()

with {:ok, session} <- GoTrue.sign_in_with_sso(client, params) do
do_login(client, 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)
{:ok, client} = @client.get_client()

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

defp do_login(conn, session, params) do
defp do_login(%Supabase.Client{} = client, conn, session, params) do
user_return_to = get_session(conn, :user_return_to)

:ok = Supabase.Client.update_access_token(@client, session.access_token)
:ok = Supabase.Client.update_access_token(client, session.access_token)

conn
|> renew_session()
Expand Down Expand Up @@ -112,9 +128,10 @@ if Code.ensure_loaded?(Plug) do
Logs out the user from the application, clearing session data
"""
def log_out_user(%Plug.Conn{} = conn, scope) do
{:ok, client} = @client.get_client()
user_token = get_session(conn, :user_token)
session = %Session{access_token: user_token}
user_token && Admin.sign_out(@client, session, scope)
user_token && Admin.sign_out(client, session, scope)

live_socket_id = get_session(conn, :live_socket_id)

Expand Down Expand Up @@ -149,7 +166,9 @@ if Code.ensure_loaded?(Plug) do
end

defp fetch_user_from_session_token(user_token) do
case GoTrue.get_user(@client, %Session{access_token: user_token}) do
{:ok, client} = @client.get_client()

case GoTrue.get_user(client, %Session{access_token: user_token}) do
{:ok, %User{} = user} -> user
_ -> nil
end
Expand Down
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.3.9"
@version "0.3.10"
@source_url "https://github.com/zoedsoupe/gotrue-ex"

def project do
Expand Down

0 comments on commit 2dedcc8

Please sign in to comment.