planner/lib/planner_web/controllers/user_session_controller.ex

26 lines
683 B
Elixir

defmodule PlannerWeb.UserSessionController do
use PlannerWeb, :controller
alias Planner.Accounts
alias PlannerWeb.UserAuth
def new(conn, _params) do
render(conn, "new.html", error_message: nil)
end
def create(conn, %{"user" => user_params}) do
%{"email" => email, "password" => password} = user_params
if user = Accounts.get_user_by_email_and_password(email, password) do
UserAuth.login_user(conn, user, user_params)
else
render(conn, "new.html", error_message: "Invalid e-mail or password")
end
end
def delete(conn, _params) do
conn
|> put_flash(:info, "Logged out successfully.")
|> UserAuth.logout_user()
end
end