Created
October 22, 2016 12:13
-
-
Save bnhansn/58a2cdb3d1e1c68664db3f086ec3d09f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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