elm-phoenix-socket icon indicating copy to clipboard operation
elm-phoenix-socket copied to clipboard

Authenticate a socket ?

Open note89 opened this issue 8 years ago • 1 comments

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}"

note89 avatar Jul 18 '16 22:07 note89

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

mbaileys avatar Jul 25 '17 15:07 mbaileys