Skip to content

Commit

Permalink
add ensure_authenticated callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bemesa21 committed May 17, 2022
1 parent c0cef77 commit bcc7b18
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
48 changes: 39 additions & 9 deletions priv/templates/phx.gen.auth/auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,53 @@ defmodule <%= inspect auth_module %> do
end

@doc """
Authenticates and mounts in the socket assigns the
<%= schema.singular %> by looking into the session.
#mount_current_<%= schema.singular %>:
Assigns current_<%= schema.singular %> to socket assigns based on <%= schema.singular %>_token.
Returns nil if there's no <%= schema.singular %>_token or if there's no matching <%= schema.singular %>.
#ensure_authenticated:
Authenticates the <%= schema.singular %> by looking into the session.
Assigns current_<%= schema.singular %> to socket assigns based on <%= schema.singular %>_token.
Redirects to login page if there's no logged <%= schema.singular %>.
##Examples
# In a LiveView file
defmodule <%= inspect context.web_module %>.PageLive do
use <%= inspect context.web_module %>, :live_view
on_mount {<%= inspect auth_module %>, :mount_current_<%= schema.singular %>}
#using live_session in router.ex
live_session :authenticated, on_mount: [{<%= inspect auth_module %>, :ensure_authenticated}] do
live "/profile", ProfileLive, :index
end
"""
def mount_current_<%= schema.singular %>(session, socket) do
def on_mount(:mount_current_<%= schema.singular %>, _params, session, socket) do
{:cont, mount_current_<%= schema.singular %>(session, socket)}
end

def on_mount(:ensure_authenticated, _params, session, socket) do
socket = mount_current_<%= schema.singular %>(session, socket)
case socket.assigns.current_<%= schema.singular %> do
nil ->
{:halt, LiveView.redirect(socket, to: Routes.<%= schema.singular %>_session_path(socket, :new))}

_ ->
{:cont, socket}
end
end

defp mount_current_<%= schema.singular %>(session, socket) do
case session do
%{"<%= schema.singular %>_token" => <%= schema.singular %>_token} ->
LiveView.assign_new(socket, :current_<%= schema.singular %>, fn -> <%= inspect context.alias %>.get_<%= schema.singular %>_by_session_token(<%= schema.singular %>_token) end)
LiveView.assign_new(socket, :current_<%= schema.singular %>, fn -> Accounts.get_<%= schema.singular %>_by_session_token(<%= schema.singular %>_token) end)

%{} ->
LiveView.assign(socket, :current_<%= schema.singular %>, nil)
LiveView.assign_new(socket, :current_<%= schema.singular %>, fn -> nil end)
end
end

def on_mount(:current_<%= schema.singular %>, _params, session, socket) do
{:cont, mount_current_<%= schema.singular %>(session, socket)}
end

@doc """
Used for routes that require the <%= schema.singular %> to not be authenticated.
"""
Expand Down
43 changes: 40 additions & 3 deletions priv/templates/phx.gen.auth/auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,50 @@ defmodule <%= inspect auth_module %>Test do
end
end
describe "mount_current_<%= schema.singular %>/2" do
test "validates and mount <%= schema.singular %> from session", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
describe "on_mount: mount_current_<%= schema.singular %>" do
test "assigns current_<%= schema.singular %> based on a valid <%= schema.singular %>_token ", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
<%= schema.singular %>_token = <%= inspect context.alias %>.generate_<%= schema.singular %>_session_token(<%= schema.singular %>)
session = conn |> put_session(:<%= schema.singular %>_token, <%= schema.singular %>_token) |> get_session()
updated_socket = <%= inspect schema.alias %>Auth.mount_current_<%= schema.singular %>(session, %Phoenix.LiveView.Socket{})
{:cont, updated_socket} = <%= inspect schema.alias %>Auth.on_mount(:mount_current_<%= schema.singular %>, %{}, session, %Phoenix.LiveView.Socket{})
assert updated_socket.assigns.current_<%= schema.singular %>.id == <%= schema.singular %>.id
end

test "assigns nil to current_user assign if there isn't a valid <%= schema.singular %>_token ", %{conn: conn} do
<%= schema.singular %>_token = "invalid_token"
session = conn |> put_session(:<%= schema.singular %>_token, <%= schema.singular %>_token) |> get_session()
{:cont, updated_socket} = <%= inspect schema.alias %>Auth.on_mount(:mount_current_<%= schema.singular %>, %{}, session, %Phoenix.LiveView.Socket{})
assert updated_socket.assigns.current_<%= schema.singular %> == nil
end

test "assigns nil to current_user assign if there isn't a <%= schema.singular %>_token", %{conn: conn} do
session = conn |> get_session()
{:cont, updated_socket} = <%= inspect schema.alias %>Auth.on_mount(:mount_current_<%= schema.singular %>, %{}, session, %Phoenix.LiveView.Socket{})
assert updated_socket.assigns.current_<%= schema.singular %> == nil
end
end

describe "on_mount: ensure_authenticated" do
test "authenticates current_<%= schema.singular %> based on a valid <%= schema.singular %>_token ", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
<%= schema.singular %>_token = <%= inspect context.alias %>.generate_<%= schema.singular %>_session_token(<%= schema.singular %>)
session = conn |> put_session(:<%= schema.singular %>_token, <%= schema.singular %>_token) |> get_session()
{:cont, updated_socket} = <%= inspect schema.alias %>Auth.on_mount(:ensure_authenticated, %{}, session, %Phoenix.LiveView.Socket{})
assert updated_socket.assigns.current_<%= schema.singular %>.id == <%= schema.singular %>.id
end

test "redirects to login page if there isn't a valid <%= schema.singular %>_token ", %{conn: conn} do
<%= schema.singular %>_token = "invalid_token"
session = conn |> put_session(:<%= schema.singular %>_token, <%= schema.singular %>_token) |> get_session()
socket = %Phoenix.LiveView.Socket{endpoint: <%= inspect context.web_module %>.Endpoint}
{:halt, updated_socket} = <%= inspect schema.alias %>Auth.on_mount(:ensure_authenticated, %{}, session, socket)
assert updated_socket.assigns.current_<%= schema.singular %> == nil
end

test "redirects to login page if there isn't a <%= schema.singular %>_token ", %{conn: conn} do
session = conn |> get_session()
socket = %Phoenix.LiveView.Socket{endpoint: <%= inspect context.web_module %>.Endpoint}
{:halt, updated_socket} = <%= inspect schema.alias %>Auth.on_mount(:ensure_authenticated, %{}, session, socket)
assert updated_socket.assigns.current_<%= schema.singular %> == nil
end
end

describe "redirect_if_<%= schema.singular %>_is_authenticated/2" do
Expand Down

0 comments on commit bcc7b18

Please sign in to comment.