philomena/lib/philomena_web/plugs/require_user_plug.ex

22 lines
404 B
Elixir
Raw Normal View History

2019-11-17 19:18:21 +01:00
defmodule PhilomenaWeb.RequireUserPlug do
2019-10-09 17:51:14 +02:00
import Phoenix.Controller
import Plug.Conn
# No options
def init([]), do: false
# Redirect if not logged in
def call(conn, _opts) do
user = conn.assigns.current_user
2019-10-09 17:51:14 +02:00
if user do
conn
else
conn
2023-11-23 17:07:49 +01:00
|> put_flash(:alert, "You must be signed in to see this page.")
2019-10-09 17:51:14 +02:00
|> redirect(to: "/")
|> halt()
end
end
end