bobfp

bobfp

Hi everyone,

I’m migrating a Magic Link authentication flow from Phoenix LiveView to a SolidJS SPA using Ash TypeScript, and I’m stuck on retrieving the authentication token.

Background

  • Using AshAuthentication’s built-in magic link resources (which worked perfectly out-of-the-box with LiveView)

  • Now building a JS SPA (SolidJS) that needs to interact with the same backend via the RPC client

  • The LiveView implementation handled the token automatically, but I want to manage it manually in the SPA

What I’m trying to do

Recreate the same Magic Link flow in my SPA:

  1. User requests a magic link via email

  2. User clicks the link and is authenticated

  3. The app receives and stores the authentication token for subsequent API calls

Current status

:white_check_mark: Successfully requesting the magic link using AshAuthentication’s request_magic_link action
:white_check_mark: Successfully calling the sign-in-with-magic-link action
:cross_mark: Cannot retrieve the authentication token after sign-in to store it client-side

The problem

From what I can tell, the token appears to be returned via metadata in the response, but the RPC client doesn’t seem to expose this metadata (or I’m not accessing it correctly).

Question

  1. How do I access response metadata (specifically the auth token) from AshAuthentication’s magic link sign-in action through the RPC client?

  2. Is there a different pattern I should be following when using AshAuthentication with a JS SPA instead of LiveView?

Any guidance on properly handling AshAuthentication tokens in a decoupled frontend would be greatly appreciated!

Showing Posts 1 to 2

zachdaniel

zachdaniel

Creator of Ash

Ah…right. I think the RPC client may need to be updated to support displaying metadata in its results. You’ll need to open an issue for this :smiley:

bobfp

bobfp OP

No worries, will do.
For anyone else that has a similar issue, I found a workaround by getting the token in a calculation:

defmodule FastricaServer.Accounts.User.Calculations.CurrentToken do
  @moduledoc """
  Calculation that retrieves the current authentication token for a user.

  This queries the Token resource to find the most recent valid token
  for the user, which is set during authentication via the sign_in_with_magic_link action.
  """
  use Ash.Resource.Calculation

  @impl true
  def calculate(records, _opts, _context) do
    # For each user record, generate a fresh JWT token
    Enum.map(records, fn user ->
      case AshAuthentication.Jwt.token_for_user(user, %{
        "purpose" => "user"
      }) do
        {:ok, jwt_token, _claims} -> jwt_token
        _ -> nil
      end
    end)
  end

  @impl true
  def load(_query, _opts, _context) do
    # No additional data needs to be loaded
    []
  end
end
— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews