-
-
Save hassox/1f8dabbc745011c17da5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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