philomena/lib/philomena_web/plugs/require_user_plug.ex

22 lines
404 B
Elixir
Raw Permalink 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
|> put_flash(:error, "You must be signed in to see this page.")
|> redirect(to: "/")
|> halt()
end
end
end