feat(server): session-based auth plug + login controller/template
This commit is contained in:
parent
3123743c1c
commit
4538945b85
4 changed files with 95 additions and 0 deletions
19
server/lib/server_web/plugs/require_auth.ex
Normal file
19
server/lib/server_web/plugs/require_auth.ex
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
defmodule ServerWeb.Plugs.RequireAuth do
|
||||
@moduledoc "Redirects to /login unless the session is authenticated."
|
||||
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
|
||||
def init(opts), do: opts
|
||||
|
||||
def call(conn, _opts) do
|
||||
if get_session(conn, :authenticated) do
|
||||
conn
|
||||
else
|
||||
conn
|
||||
|> put_flash(:error, "Please sign in.")
|
||||
|> redirect(to: "/login")
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue