about summary refs log tree commit diff
path: root/lib/rook_web/channels
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2021-05-08 23:41:03 +0200
committerMelonai <einebeere@gmail.com>2021-05-08 23:41:03 +0200
commit09693b979efe3c9c7ea64f79d97a1f0b53f7c49e (patch)
tree13719bb4800c1510a70f96f3e81f96f80ef960a2 /lib/rook_web/channels
parent88c0fbf10145ddd4ccd10ee432b4ca1aadd96a91 (diff)
downloadrook-09693b979efe3c9c7ea64f79d97a1f0b53f7c49e.tar.zst
rook-09693b979efe3c9c7ea64f79d97a1f0b53f7c49e.zip
Socket connection and token fetching
Diffstat (limited to 'lib/rook_web/channels')
-rw-r--r--lib/rook_web/channels/share_channel.ex11
-rw-r--r--lib/rook_web/channels/token_channel.ex11
-rw-r--r--lib/rook_web/channels/user_socket.ex6
3 files changed, 26 insertions, 2 deletions
diff --git a/lib/rook_web/channels/share_channel.ex b/lib/rook_web/channels/share_channel.ex
new file mode 100644
index 0000000..fde0096
--- /dev/null
+++ b/lib/rook_web/channels/share_channel.ex
@@ -0,0 +1,11 @@
+defmodule RookWeb.ShareChannel do
+  use Phoenix.Channel
+
+  def join("share:" <> token, _params, socket) do
+    if token == socket.assigns[:token] do
+      {:ok, socket}
+    else
+      {:error, %{reason: "Wrong token."}}
+    end
+  end
+end
diff --git a/lib/rook_web/channels/token_channel.ex b/lib/rook_web/channels/token_channel.ex
new file mode 100644
index 0000000..72cabb1
--- /dev/null
+++ b/lib/rook_web/channels/token_channel.ex
@@ -0,0 +1,11 @@
+defmodule RookWeb.TokenChannel do
+  use Phoenix.Channel
+
+  def join("token", _params, socket) do
+    {:ok, socket}
+  end
+
+  def handle_in("get_token", _attrs, socket) do
+    {:reply, {:ok, %{token: socket.assigns[:token]}}, socket}
+  end
+end
diff --git a/lib/rook_web/channels/user_socket.ex b/lib/rook_web/channels/user_socket.ex
index 66637af..a036c4a 100644
--- a/lib/rook_web/channels/user_socket.ex
+++ b/lib/rook_web/channels/user_socket.ex
@@ -2,7 +2,8 @@ defmodule RookWeb.UserSocket do
   use Phoenix.Socket
 
   ## Channels
-  # channel "room:*", RookWeb.RoomChannel
+  channel "token", RookWeb.TokenChannel
+  channel "share:*", RookWeb.ShareChannel
 
   # Socket params are passed from the client and can
   # be used to verify and authenticate a user. After
@@ -17,7 +18,8 @@ defmodule RookWeb.UserSocket do
   # performing token verification on connect.
   @impl true
   def connect(_params, socket, _connect_info) do
-    {:ok, socket}
+    token = Rook.Utils.Token.token()
+    {:ok, assign(socket, :token, token)}
   end
 
   # Socket id's are topics that allow you to identify all sockets for a given user: