Files
sso-bsn/lib/sso_bsn_web/live/user_login_live.ex
2023-11-09 17:03:20 +00:00

66 lines
2.1 KiB
Elixir

defmodule SsoBsnWeb.UserLoginLive do
use SsoBsnWeb, :live_view
alias SsoBsn.Accounts
def render(assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">
Sign in to account
<:subtitle>
Don't have an account?
<.link navigate={~p"/users/register"} class="font-semibold text-brand hover:underline">
Sign up
</.link>
for an account now.
</:subtitle>
</.header>
<.simple_form
for={@form}
id="login_form"
action={~p"/users/log_in"}
phx-update="ignore"
phx-submit="login"
phx-hook="authenticationHook"
>
<.input field={@form[:username]} type="text" label="Username" />
<:actions>
<.input field={@form[:remember_me]} type="checkbox" label="Keep me logged in" />
</:actions>
<:actions>
<.button phx-disable-with="Signing in..." class="w-full" disabled={@authenticating}>
Sign in <span aria-hidden="true">→</span>
</.button>
</:actions>
</.simple_form>
</div>
"""
end
def mount(params, _session, socket) do
{:ok, socket |> assign(form: to_form(%{"username" => "", "remember_me" => false}), authenticating: false, next: params["next"])}
end
def handle_event("login", %{"username" => username}, socket) do
{challenge, challenge_client} = Accounts.authentication_challenge(username)
{:noreply,
socket
|> assign(challenge: challenge, authenticating: true)
|> push_event("authentication-challenge", challenge_client)}
end
def handle_event("authentication-credential", params, socket) do
case Accounts.authenticate_user(socket.assigns.challenge, params) do
{:ok, user} ->
login_token = Accounts.generate_user_login_token(user)
{:noreply, socket |> redirect(to: if next = socket.assigns.next do ~p"/users/log_in/#{login_token}?next=#{next}" else ~p"/users/log_in/#{login_token}" end)}
{:error, error} ->
{:noreply, socket |> put_flash(:error, inspect(error))}
end
end
end