Skip to content

Instantly share code, notes, and snippets.

@bnhansn
Created October 22, 2016 12:13
Show Gist options
  • Save bnhansn/58a2cdb3d1e1c68664db3f086ec3d09f to your computer and use it in GitHub Desktop.
Save bnhansn/58a2cdb3d1e1c68664db3f086ec3d09f to your computer and use it in GitHub Desktop.
defmodule Sling.UserSocket do
use Phoenix.Socket
channel "rooms:*", Sling.RoomChannel
transport :websocket, Phoenix.Transports.WebSocket
def connect(%{"token" => token}, socket) do
case Guardian.decode_and_verify(token) do
{:ok, claims} ->
case Sling.GuardianSerializer.from_token(claims["sub"]) do
{:ok, user} ->
{:ok, assign(socket, :current_user, user)}
{:error, _reason} ->
:error
end
{:error, _reason} ->
:error
end
end
def connect(_params, _socket), do: :error
def id(socket), do: "users_socket:#{socket.assigns.current_user.id}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment