Skip to content

Instantly share code, notes, and snippets.

@shankardevy
Last active November 21, 2017 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankardevy/67499760b510233ef1cd29a89cfd9e6f to your computer and use it in GitHub Desktop.
Save shankardevy/67499760b510233ef1cd29a89cfd9e6f to your computer and use it in GitHub Desktop.
defmodule MangoWeb.Admin.SessionController do
use MangoWeb, :controller
plug :set_layout
alias Mango.Administration
def new(conn, _params) do
conn
|> render("new.html")
end
def send_link(conn, %{"session" => %{"email" => email}}) do
user = Administration.get_admin_by_email(email)
conn = case user do
nil ->
conn
|> put_flash(:error, "Authentication error")
user ->
link = generate_login_link(conn, user)
conn
|> put_flash(:info, "You magic login link is #{link}")
end
conn |> render("new.html")
end
def create(conn, %{"token" => token}) do
case verify_token(token) do
{:ok, user_id} ->
user = Administration.get_user!(user_id)
conn
|> assign(:current_admin, user)
|> put_session(:admin_id, user.id)
|> configure_session(renew: true)
|> put_flash(:info, "Successfully logged in!")
|> redirect(to: admin_user_path(conn, :index))
{:error, _} ->
conn
|> put_flash(:error, "Authentication error")
|> render(:new)
end
end
@max_age 600 # 10 mins
defp verify_token(token) do
Phoenix.Token.verify(MangoWeb.Endpoint, "user", token, max_age: @max_age)
end
defp generate_login_link(conn, user) do
token = Phoenix.Token.sign(MangoWeb.Endpoint, "user", user.id)
admin_session_url(conn, :create, %{token: token})
end
defp set_layout(conn, _) do
conn
|> put_layout("admin_login.html")
end
end
@thebrianemory
Copy link

On version 1.1 of the book, line 35 is different than what is in the book.

In the Admin I section for Listing 18, it says |> redirect(to: admin_dashboard_path(conn, :index)).

@schlenks
Copy link

For quick reference, it's on page 301 that the admin_dashboard_path typo is found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment