Guardian v0.13.0 Guardian.Channel

Provides integration for channels to use Guardian tokens.

Example

defmodule MyApp.MyChannel do
  use Phoenix.Channel
  use Guardian.Channel

  def join(_room, %{ claims: claims, resource: resource }, socket) do
    {:ok, %{ message: "Joined" }, socket}
  end

  def join(room, _, socket) do
    {:error,  :authentication_required}
  end

  def handle_in("ping", _payload, socket) do
    user = current_resource(socket)
    broadcast(socket, "pong", %{message: "pong", from: user.email})
    {:noreply, socket}
  end
end

Tokens will be parsed and the claims and resource assigned to the socket.

Example

let socket = new Socket("/ws")
socket.connect()

let guardianToken = jQuery('meta[name="guardian_token"]').attr('content')
let chan = socket.chan("pings", { guardian_token: guardianToken })

Consider using Guardian.Phoenix.Socket helpers directly and authenticating the connection rather than the channel.