66 lines
2.0 KiB
Elixir
66 lines
2.0 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)}
|
|
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: ~p"/users/log_in/#{login_token}")}
|
|
{:error, error} ->
|
|
{:noreply, socket |> put_flash(:error, inspect(error))}
|
|
end
|
|
end
|
|
end
|