Ash FrameworkAF
Ash Framework5mo ago
8 replies
Arvind S.

How do I use a custom assign in the AshAuthentication success callback to work with Ash.Scope?

I’m using AshAuthentication and I have a success/4 callback in my AuthController that looks like this that is attempting to set the current_user key in the Scope object as part of the authentication confirmation flow:

def success(conn, activity, user, _token) do
  return_to = AuthRedirect.post_login_path(user)

  message =
    case activity do
      {:confirm_new_user, :confirm} -> "Your email address has now been confirmed"
      {:password, :reset} -> "Your password has successfully been reset"
      _ -> "You are now signed in"
    end

  # This is a custom function that will return a %Scope{current_user: user} object
  scope = Scope.for_user(user)

  conn
  |> delete_session(:return_to)
  |> store_in_session(user)
  # If your resource has a different name, update the assign name here (i.e :current_admin)
  |> assign(:current_scope, scope) <--- This is what I'd like to do, the generated comment above makes it seem like this should be possible?
  |> put_flash(:info, message)
  |> redirect(to: return_to)
end


I tried changing the assign line to :current_scope, but it had no effect — conn.assigns still only contains :current_user.

What’s the recommended way to do this in AshAuth? Should I assign it once in the success/4 callback, or add a plug that derives it from :current_user on every request?
Was this page helpful?