You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's impossible because websocket connections are stateless and have no headers.
You can pass your token in params on connect.
constphoenixSocket=newPhoenixSocket('ws://localhost:4000/socket',{params: ()=>{consttoken=localStorage.getItem('token')// Or where do you store itreturntoken ? { token } : {}},})
And then hold it inside connection context on a backend side while it is active
defmoduleYourAppWeb.UserSocketdousePhoenix.SocketuseAbsinthe.Phoenix.Socket,schema: YourAppWeb.Schemadefconnect(params,socket,_connect_info)docurrent_user=current_user(params)# Here you get user from params.tokensocket=Absinthe.Phoenix.Socket.put_options(socket,context: %{current_user: current_user}){:ok,socket}enddefpcurrent_user(%{token: token})do# How do you get userenddefpcurrent_user(_params),do: nildefid(_socket),do: nilend
Intended outcome:
I want to pass an authorization header
headers: { authorization: Bearer token } }
to my WS connectionI tried:
I also tried:
This is similar to the issue here.
The text was updated successfully, but these errors were encountered: