elm-phoenix-socket
elm-phoenix-socket copied to clipboard
Authenticate a socket ?
It seems that there is a possibility to authenticate when connecting to a channel but not when connecting to a socket ?
let socket = new Socket("/socket", {
params: {token: window.userToken},
logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
})
want something like this on the backend
@max_age 2 * 7 * 24 * 60 * 60
def connect(%{"token" => token}, socket) do
case Phoenix.Token.verify(socket, "user socket", token, max_age: @max_age) do
{:ok, user_id} ->
{:ok, assign(socket, :user_id, user_id)}
{:error, _reason} ->
:error
end
end
def connect(_params, _socket), do: :error
def id(socket), do: "users_socket:#{socket.assigns.user_id}"
This is an old issue but I ran into this recently. To authenticate at socket connection time, you have to add the token as a query param of the URL. /websocket/socket?token=ygaz7KAanxhfbpR8kJGfc8ic