2021-03-11 21:12:55 -05:00
|
|
|
defmodule CanneryWeb.UserSessionController do
|
|
|
|
use CanneryWeb, :controller
|
|
|
|
|
|
|
|
alias Cannery.Accounts
|
|
|
|
alias CanneryWeb.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.log_in_user(conn, user, user_params)
|
|
|
|
else
|
2022-02-08 23:58:54 -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-09 00:13:01 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "Logged out successfully."))
|
2021-03-11 21:12:55 -05:00
|
|
|
|> UserAuth.log_out_user()
|
|
|
|
end
|
|
|
|
end
|