pokemon-couture/lib/pokemon_couture_web/controllers/user_session_controller.ex

27 lines
727 B
Elixir
Raw Normal View History

2021-06-28 11:14:14 +02:00
defmodule PokemonCoutureWeb.UserSessionController do
use PokemonCoutureWeb, :controller
alias PokemonCouture.Accounts
alias PokemonCoutureWeb.UserAuth
def new(conn, _params) do
render(conn, "new.html", error_message: nil)
end
def create(conn, %{"user" => user_params}) do
%{"username" => username, "password" => password} = user_params
2021-06-28 11:14:14 +02:00
if user = Accounts.get_user_by_username_and_password(username, password) do
2021-06-28 11:14:14 +02:00
UserAuth.log_in_user(conn, user, user_params)
else
render(conn, "new.html", error_message: "Invalid username or password")
2021-06-28 11:14:14 +02:00
end
end
def delete(conn, _params) do
conn
|> put_flash(:info, "Logged out successfully.")
|> UserAuth.log_out_user()
end
end