2021-04-12 15:44:12 +02:00
|
|
|
defmodule MetaWeb.UserSessionController do
|
|
|
|
use MetaWeb, :controller
|
2020-12-15 14:24:14 +01:00
|
|
|
|
2021-04-12 15:44:12 +02:00
|
|
|
alias Meta.Accounts
|
|
|
|
alias MetaWeb.UserAuth
|
2020-12-15 14:24:14 +01:00
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
render(conn, "new.html", error_message: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"user" => user_params}) do
|
2020-12-21 15:40:19 +01:00
|
|
|
%{"username" => username, "password" => password} = user_params
|
2020-12-15 14:24:14 +01:00
|
|
|
|
2020-12-21 15:40:19 +01:00
|
|
|
if user = Accounts.get_user_by_username_and_password(username, password) do
|
2020-12-15 14:24:14 +01:00
|
|
|
UserAuth.log_in_user(conn, user, user_params)
|
|
|
|
else
|
2020-12-21 15:40:19 +01:00
|
|
|
render(conn, "new.html", error_message: "Invalid username or password")
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Logged out successfully.")
|
|
|
|
|> UserAuth.log_out_user()
|
|
|
|
end
|
|
|
|
end
|