cannery/lib/cannery_web/controllers/user_session_controller.ex

27 lines
756 B
Elixir
Raw Normal View History

2023-02-25 15:47:37 -05:00
defmodule CanneryWeb.UserSessionController do
use CanneryWeb, :controller
2021-03-11 21:12:55 -05:00
2023-02-25 15:47:37 -05:00
alias Cannery.Accounts
alias CanneryWeb.UserAuth
2021-03-11 21:12:55 -05:00
def new(conn, _params) do
2022-02-25 21:53:04 -05:00
render(conn, "new.html", error_message: nil, page_title: gettext("Log in"))
2021-03-11 21:12:55 -05:00
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.log_in_user(conn, user, user_params)
else
2022-02-25 21:53:04 -05:00
render(conn, "new.html", error_message: dgettext("errors", "Invalid email or password"))
2021-03-11 21:12:55 -05:00
end
end
def delete(conn, _params) do
conn
2022-02-25 21:53:04 -05:00
|> put_flash(:info, dgettext("prompts", "Logged out successfully."))
2021-03-11 21:12:55 -05:00
|> UserAuth.log_out_user()
end
end