2020-07-28 22:56:26 +02:00
|
|
|
defmodule PhilomenaWeb.SessionController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Users
|
|
|
|
alias PhilomenaWeb.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
|
|
|
|
|
|
|
|
user =
|
|
|
|
Users.get_user_by_email_and_password(
|
|
|
|
email,
|
|
|
|
password,
|
2024-06-06 22:28:35 +02:00
|
|
|
&url(~p"/unlocks/#{&1}")
|
2020-07-28 22:56:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
not is_nil(user) and is_nil(user.confirmed_at) ->
|
2020-09-27 06:03:34 +02:00
|
|
|
render(
|
|
|
|
conn,
|
|
|
|
"new.html",
|
|
|
|
error_message: "You must confirm your account before logging in."
|
|
|
|
)
|
2020-07-28 22:56:26 +02:00
|
|
|
|
|
|
|
not is_nil(user) ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Successfully logged in.")
|
|
|
|
|> UserAuth.log_in_user(user, user_params)
|
|
|
|
|
|
|
|
true ->
|
2020-09-27 06:03:34 +02:00
|
|
|
render(
|
|
|
|
conn,
|
|
|
|
"new.html",
|
|
|
|
error_message:
|
|
|
|
"Invalid email or password. If you're seeing this more than usual, your account may be locked."
|
|
|
|
)
|
2020-07-28 22:56:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Logged out successfully.")
|
|
|
|
|> UserAuth.log_out_user()
|
|
|
|
end
|
|
|
|
end
|