Drab: remote controlled frontend framework for Phoenix

There will be one more thing to change, if you want to pass the external token connect/2 callback: you will need to pass the token somehow from the client. In this case, instead of adding the following to app.html.exs:

<%= Drab.Client.js(@conn) %>

(which BTW I just renamed to Drab.Client.run), you should generate Drab client’s code first, and connect with additional parameters then:

  <%= Drab.Client.generate(@conn) %>
  <script>
    Drab.connect({auth_token: window.my_token});
  </script>

There is also the third way :slight_smile: pass the token via Drab.Client.js/2 as additional assign, like:

<%= Drab.Client.js(@conn, my_token: token) %>

In this case socket returned by Drab.Socket.verify/2 would contain it as an assign, so you may authenticate like:

def connect(token, socket) do
  {:ok, drabbed_socket} = Drab.Socket.verify(socket, token)
  Guardian.Phoenix.Socket.authenticate(drabbed_socket, MyApp.Guardian, drabbed_socket.assigns["my_token"])
end
1 Like