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 = Guardian.Channel.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.
When using :csrf token types, you need to pass the csrf token up with the join message.
Example
let socket = new Socket("/ws");
socket.connect();
let guardianToken = jQuery('meta[name="guardian_token"]').attr('content');
let csrfToken = jQuery('meta[name="csrf_token"]').attr('content');
let chan = socket.chan("pings", { guardian_token: guardianToken, csrf_token: csrfToken });
Summary↑
| claims(socket, key \\ :default) |
| current_resource(socket, key \\ :default) |