memEx/lib/memex_web/controllers/user_session_controller.ex

27 lines
736 B
Elixir
Raw Permalink 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
2023-04-13 23:29:29 -04:00
render(conn, :new, 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
2023-04-14 19:19:58 -04:00
render(conn, :new, 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-11-27 22:53:08 -05:00
|> put_flash(:info, dgettext("prompts", "logged out successfully."))
2021-03-11 21:12:55 -05:00
|> UserAuth.log_out_user()
end
end