memEx/lib/memex_web/controllers/user_session_controller.ex

27 lines
748 B
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.UserSessionController do
use MemexWeb, :controller
2021-03-11 21:12:55 -05:00
2022-07-25 19:31:54 -04:00
alias Memex.Accounts
alias MemexWeb.UserAuth
2021-03-11 21:12:55 -05:00
def new(conn, _params) do
2022-11-17 22:30:01 -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