From f37d55439989baedef96bbaa12294bbd3c4b732b Mon Sep 17 00:00:00 2001 From: qdust41 Date: Thu, 9 Apr 2026 15:24:26 -0400 Subject: [PATCH] fixed auth userflow --- lib/mixer/accounts/user.ex | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/mixer/accounts/user.ex b/lib/mixer/accounts/user.ex index f5e0c29..2c66963 100644 --- a/lib/mixer/accounts/user.ex +++ b/lib/mixer/accounts/user.ex @@ -306,17 +306,22 @@ defmodule Mixer.Accounts.User do changeset username -> + # Set the attribute directly so the unique_username identity's + # eager_check_with fires during Form.validate, surfacing "already + # taken" errors in the UI before the action is submitted. + changeset = Ash.Changeset.change_attribute(changeset, :username, username) + + # Also update via after_action to handle existing users who have no + # username yet: for upserts, only upsert_fields are applied to the + # conflicting row, so change_attribute above won't touch them. Ash.Changeset.after_action(changeset, fn _cs, user -> if is_nil(user.username) do user - |> Ash.Changeset.for_update(:update_profile, %{username: username}, - authorize?: false - ) + |> Ash.Changeset.for_update(:update_profile, %{username: username},authorize?: false) |> Ash.update() |> case do {:ok, updated} -> {:ok, updated} - # Don't fail the sign-in just because username set failed - {:error, _} -> {:ok, user} + {:error, error} -> {:error, error} end else {:ok, user}