Skip to content

Commit

Permalink
improvement: submit form in-line when sign_in_tokens_enabled (#274)
Browse files Browse the repository at this point in the history
* improvement: submit form in-line when sign_in_tokens_enabled

* chore: update ash_authentication dependency for new feature
  • Loading branch information
zachdaniel authored Sep 18, 2023
1 parent 9af7ff5 commit 91df24f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 91df24f

Please sign in to comment.