Update API Token using function called from handle_event

Hello!
I have a LiveView that uploads a CSV, processes it with some information from external APIs, and creates a download.
To use the external APIs, I have to get a token. The token expires eventually, and I have code to catch the response and get a new token.
I’m really failing at adding the token to the state and making it available for future calls.
I know I could persist the token in a DB, but this seems a little heavy. I have been trying to add the token to socket assigns (so generally available in the app), but I can’t seem to make it work.
The user clicks a button, which starts handle_event(“process”). If one of the API calls fails a certain way, I have a def get_token() function that gets a new token.

defp get_token(base_url, api_key, socket) do
    IO.puts("GETTING TOKEN")

    get_token_task =
      Task.async(fn ->
        resp =
          Req.post!("#{base_url}/api/v1/1/prod/get_token", json: %{api_key: api_key})

        error = resp.body["err"]
        token = resp.body["resp"]["SecurityToken"]

        case error do
          error when error == true ->
            {:error, error}

          _ ->
            {:ok, token}
            IO.puts("TOKEN RECEIVED")
            ######################################
            # What am I supposed to do with it?
            ######################################
        end
      end)

    Task.await(get_token_task)
  end

Is there really no way to just add the token to assigns? I have tried every method I could find, including just assigning, {:noreply, assign(socket, token: token)}, send self() + handle_info()…
What’s the right way to do this?
It does not need to be inside the Task. I’m just returning the token now and passing that around. The problem is how to update the value from within a function.
Is there some other way to persist state in the LiveView that I don’t know about yet? I feel like I’m missing something pretty basic here!
Looking forward to other people’s input.
Thank you in advance!

You need to keep in mind that:

  • All values are immutable
  • 2 processes share nothing, such as the LV process and the get_token_task

So you need to pass the pid of the LV process into the task, then send the token back as part of a message, then have a handle_info() at the LV process to update the socket.

The Task.await below will get the value returned by this code, which right now is the return value of IO.puts. You likely want:

          _ ->
            IO.puts("TOKEN RECEIVED")
            {:ok, token}

That way the Task.await receives the token.

I’d recommend keeping the “get a token” and “put that token into assigns” parts separate; having a side-effecting function named get_* seems odd. So you’d store the token into socket wherever is calling get_token.


Mostly-unrelated question: what’s the purpose of the Task.async followed immediately by a Task.await? Is it for the 5s timeout?