If you store the user in a socket, as described in https://hexdocs.pm/phoenix/Phoenix.Token.html
defmodule MyApp.UserSocket do
use Phoenix.Socket
def connect(%{"token" => token}, socket) do
case Phoenix.Token.verify(socket, "user", token, max_age: 1209600) do
{:ok, user_id} ->
socket = assign(socket, :user, Repo.get!(User, user_id))
{:ok, socket}
{:error, _} ->
end
end
end
Then you can check the admin status of admin in the function handle_outfor your channel described here :
defmodule HelloPhoenix.RoomChannel do
intercept ["new_msg"]
...
def handle_out("new_msg", payload, socket) do
if socket.assigns.user.admin do
push socket, "new_msg", payload
end
{:noreply, socket}
end
end
Depending on the amount of messages and the number of administrators, you might think that there is a channel specific to the administrator for these events. This will prevent processes from sending messages to non-admin users, rather than simply ignoring them.
source
share