Skip to content

Instantly share code, notes, and snippets.

@hassox hassox/session_controller.ex Secret
Created Dec 21, 2015

Embed
What would you like to do?
defmodule SecretKeeper.SessionController do
use SecretKeeper.Web, :controller
alias SecretKeeper.User
alias SecretKeeper.Repo
def new(conn, params) do
changeset = User.login_changeset(%User{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, params) do
changeset = User.login_changeset(%User{}, params)
if changeset.valid? do
case Repo.get_by(email: params["user"]["email"]) do
nil ->
conn
|> put_status(404)
|> put_flash(:error, "Could not login")
|> redirect(to: session_path(conn, :new))
user ->
case User.checkpw(user, params["user"]["password"]) ->
:ok ->
conn
|> Guardian.Plug.sign_in(user, :token, perms: %{ default: Guardian.Permissions.max })
|> redirect(to: user_path(conn, index))
:error ->
conn
|> put_status(401)
|> put_flash(:error, "Could not login")
|> redirect(to: session_path(conn, :new))
end
end
else
conn
|> render("new.html", changeset: changeset)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.