Last active
March 21, 2024 11:54
-
-
Save hmorneau/50b97c9931c15ff214212024855e4d3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def require_admin_user(conn, _opts) do | |
current_user = conn.assigns[:current_user] | |
if current_user.admin? do | |
conn | |
else | |
conn | |
|> put_flash(:error, "You must be an admin to access this page.") | |
|> redirect(to: Routes.admin_session_path(conn, :new)) | |
|> halt() | |
end | |
end | |
def on_mount(:require_admin_user, _params, session, socket) do | |
socket = mount_current_user(session, socket) | |
if socket.assigns.current_user.admin? do | |
{:cont, socket} | |
else | |
{:halt, Phoenix.LiveView.redirect(socket, to: Routes.user_session_path(socket, :new))} | |
end | |
end |
This file contains hidden or 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
def require_authenticated_user(conn, _opts) do | |
cond do | |
conn.assigns[:current_user] && conn.assigns[:current_user].confirmed_at -> | |
conn | |
conn.assigns[:current_user] && !conn.assigns[:current_user].confirmed_at -> | |
conn | |
|> put_flash(:error, "You must confirm your email before you can access this page.") | |
|> redirect(to: Routes.user_confirm_path(conn, :index)) | |
|> halt() | |
true -> | |
conn | |
|> put_flash(:error, "You must log in to access this page.") | |
|> maybe_store_return_to() | |
|> redirect(to: Routes.user_session_path(conn, :new)) | |
|> halt() | |
end | |
end | |
def require_unconfirmed_user(conn, _opts) do | |
cond do | |
conn.assigns[:current_user] && !conn.assigns[:current_user].confirmed_at -> | |
conn | |
conn.assigns[:current_user] && conn.assigns[:current_user].confirmed_at -> | |
conn | |
|> put_flash(:error, "Your email address is already confirmed.") | |
|> redirect(to: Routes.page_path(conn, :index)) | |
|> halt() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment