From 91df24ff32a1c5b78b0ce4519927e97d7dd8a744 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 17 Sep 2023 20:42:14 -0400 Subject: [PATCH] improvement: submit form in-line when sign_in_tokens_enabled (#274) * improvement: submit form in-line when sign_in_tokens_enabled * chore: update ash_authentication dependency for new feature --- .../components/password/register_form.ex | 41 +++++++++++++++---- mix.exs | 2 +- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/ash_authentication_phoenix/components/password/register_form.ex b/lib/ash_authentication_phoenix/components/password/register_form.ex index 28f4610..241db17 100644 --- a/lib/ash_authentication_phoenix/components/password/register_form.ex +++ b/lib/ash_authentication_phoenix/components/password/register_form.ex @@ -151,14 +151,39 @@ defmodule AshAuthentication.Phoenix.Components.Password.RegisterForm do def handle_event("submit", params, socket) do params = get_params(params, socket.assigns.strategy) - form = Form.validate(socket.assigns.form, params) - - socket = - socket - |> assign(:form, form) - |> assign(:trigger_action, form.valid?) - - {:noreply, socket} + if Map.get(socket.assigns.strategy, :sign_in_tokens_enabled?) do + case Form.submit(socket.assigns.form, + params: params, + read_one?: true, + before_submit: fn changeset -> + Ash.Changeset.set_context(changeset, %{token_type: :sign_in}) + end + ) do + {:ok, user} -> + validate_sign_in_token_path = + route_helpers(socket).auth_path( + socket.endpoint, + {socket.assigns.subject_name, Strategy.name(socket.assigns.strategy), + :sign_in_with_token}, + token: user.__metadata__.token + ) + + {:noreply, redirect(socket, to: validate_sign_in_token_path)} + + {:error, form} -> + {:noreply, + assign(socket, :form, Form.clear_value(form, socket.assigns.strategy.password_field))} + end + else + form = Form.validate(socket.assigns.form, params) + + socket = + socket + |> assign(:form, form) + |> assign(:trigger_action, form.valid?) + + {:noreply, socket} + end end defp get_params(params, strategy) do diff --git a/mix.exs b/mix.exs index 6954ce0..6aaa3fc 100644 --- a/mix.exs +++ b/mix.exs @@ -113,7 +113,7 @@ defmodule AshAuthentication.Phoenix.MixProject do # Run "mix help deps" to learn about dependencies. defp deps do [ - {:ash_authentication, "~> 3.10"}, + {:ash_authentication, "~> 3.11 and >= 3.11.9"}, {:ash_phoenix, "~> 1.1"}, {:ash, "~> 2.2"}, {:jason, "~> 1.0"},