WebAuthN auth
This commit is contained in:
65
lib/sso_bsn_web/live/user_login_live.ex
Normal file
65
lib/sso_bsn_web/live/user_login_live.ex
Normal file
@@ -0,0 +1,65 @@
|
||||
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
|
||||
108
lib/sso_bsn_web/live/user_registration_live.ex
Normal file
108
lib/sso_bsn_web/live/user_registration_live.ex
Normal file
@@ -0,0 +1,108 @@
|
||||
defmodule SsoBsnWeb.UserRegistrationLive do
|
||||
use SsoBsnWeb, :live_view
|
||||
|
||||
alias SsoBsn.Accounts
|
||||
alias SsoBsn.Accounts.User
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto max-w-sm">
|
||||
<.header class="text-center">
|
||||
Register for an account
|
||||
<:subtitle>
|
||||
Already registered?
|
||||
<.link navigate={~p"/users/log_in"} class="font-semibold text-brand hover:underline">
|
||||
Sign in
|
||||
</.link>
|
||||
to your account now.
|
||||
</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="registration_form"
|
||||
phx-submit="save"
|
||||
phx-change="validate"
|
||||
phx-hook="registrationHook"
|
||||
phx-trigger-action={@trigger_submit}
|
||||
action={~p"/users/log_in?_action=registered"}
|
||||
method="post"
|
||||
>
|
||||
<.error :if={@check_errors}>
|
||||
Oops, something went wrong! Please check the errors below.
|
||||
</.error>
|
||||
|
||||
<.input field={@form[:username]} type="text" label="Username" required />
|
||||
|
||||
<:actions>
|
||||
<.button phx-disable-with="Creating account..." class="w-full">Create an account</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
changeset = Accounts.change_user_registration(%User{})
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(trigger_submit: false, check_errors: false)
|
||||
|> assign_form(changeset)
|
||||
|
||||
{:ok, socket, temporary_assigns: [form: nil]}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"user" => user_params}, socket) do
|
||||
case Accounts.register_user(user_params) do
|
||||
{:ok, user} ->
|
||||
changeset = Accounts.change_user_registration(user)
|
||||
{challenge, challenge_client} = Accounts.registration_challenge(user)
|
||||
|
||||
{
|
||||
:noreply,
|
||||
socket
|
||||
# |> assign(trigger_submit: true)
|
||||
|> assign_form(changeset)
|
||||
|> assign(user: user)
|
||||
|> assign(challenge: challenge)
|
||||
|> push_event("registration-challenge", challenge_client)
|
||||
}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(check_errors: true) |> assign_form(changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("validate", %{"user" => user_params}, socket) do
|
||||
changeset = Accounts.change_user_registration(%User{}, user_params)
|
||||
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
|
||||
end
|
||||
|
||||
def handle_event("registration-complete", params, socket) do
|
||||
%{user: user, challenge: challenge} = socket.assigns
|
||||
|
||||
case Accounts.register_key(user, challenge, params) do
|
||||
{:ok, _key} ->
|
||||
login_token = Accounts.generate_user_login_token(user)
|
||||
{:noreply, socket |> redirect(to: ~p"/users/log_in/#{login_token}")}
|
||||
|
||||
{:error, error} ->
|
||||
dbg(error)
|
||||
{:noreply, socket |> put_flash(:error, "An error occured")}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("error", payload, socket),
|
||||
do: {:noreply, socket |> put_flash(:error, inspect(payload))}
|
||||
|
||||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
||||
form = to_form(changeset, as: "user")
|
||||
|
||||
if changeset.valid? do
|
||||
assign(socket, form: form, check_errors: false)
|
||||
else
|
||||
assign(socket, form: form)
|
||||
end
|
||||
end
|
||||
end
|
||||
69
lib/sso_bsn_web/live/user_settings_live.ex
Normal file
69
lib/sso_bsn_web/live/user_settings_live.ex
Normal file
@@ -0,0 +1,69 @@
|
||||
defmodule SsoBsnWeb.UserSettingsLive do
|
||||
use SsoBsnWeb, :live_view
|
||||
|
||||
alias SsoBsn.Accounts
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<.header class="text-center">
|
||||
Account Settings
|
||||
</.header>
|
||||
|
||||
<div class="space-y-12 divide-y">
|
||||
<div class="space-y-2">
|
||||
<.header>Keys</.header>
|
||||
<ol>
|
||||
<li :for={key <- @current_user.keys}><pre class="truncate"><%= key.key_id %></pre></li>
|
||||
</ol>
|
||||
<.button id="settings-add-key" phx-hook="registrationHook" phx-click="register">
|
||||
Register additional key
|
||||
</.button>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<%= if !@login_url do %>
|
||||
<.button id="show-login-url" phx-click="show-login-url">Show login url</.button>
|
||||
<% else %>
|
||||
<pre><%= @login_url %></pre>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket |> assign(login_url: nil)}
|
||||
end
|
||||
|
||||
def handle_event("register", _, socket) do
|
||||
{challenge, challenge_client} = Accounts.registration_challenge(socket.assigns.current_user)
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(challenge: challenge)
|
||||
|> push_event("registration-challenge", challenge_client)}
|
||||
end
|
||||
|
||||
def handle_event("registration-complete", params, socket) do
|
||||
%{current_user: user, challenge: challenge} = socket.assigns
|
||||
|
||||
case Accounts.register_key(user, challenge, params) do
|
||||
{:ok, _key} ->
|
||||
{:noreply,
|
||||
socket |> assign(current_user: Accounts.reload_user(socket.assigns.current_user))}
|
||||
|
||||
{:error, error} ->
|
||||
dbg(error)
|
||||
{:noreply, socket |> put_flash(:error, "An error occured")}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("show-login-url", %{}, socket) do
|
||||
token = Accounts.generate_user_login_token(socket.assigns.current_user)
|
||||
|
||||
{:noreply,
|
||||
socket |> assign(login_url: url(socket, ~p"/users/log_in/#{token}"))}
|
||||
end
|
||||
|
||||
def handle_event("error", payload, socket),
|
||||
do: {:noreply, socket |> put_flash(:error, inspect(payload))}
|
||||
end
|
||||
Reference in New Issue
Block a user