aboutsummaryrefslogtreecommitdiff
path: root/lib
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
parent88c0fbf10145ddd4ccd10ee432b4ca1aadd96a91 (diff)
downloadrook-09693b979efe3c9c7ea64f79d97a1f0b53f7c49e.tar.zst
rook-09693b979efe3c9c7ea64f79d97a1f0b53f7c49e.zip
Socket connection and token fetching
Diffstat (limited to 'lib')
-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
-rw-r--r--lib/rook_web/controllers/app_controller.ex10
-rw-r--r--lib/rook_web/templates/app/entrypoint.html.eex1
-rw-r--r--lib/rook_web/templates/app/request.html.eex2
-rw-r--r--lib/rook_web/templates/app/share.html.eex2
-rw-r--r--lib/rook_web/templates/home/index.html.eex2
-rw-r--r--lib/rook_web/views/app_view.ex4
9 files changed, 31 insertions, 18 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:
diff --git a/lib/rook_web/controllers/app_controller.ex b/lib/rook_web/controllers/app_controller.ex
index 4c36fda..9e36f3c 100644
--- a/lib/rook_web/controllers/app_controller.ex
+++ b/lib/rook_web/controllers/app_controller.ex
@@ -1,8 +1,6 @@
defmodule RookWeb.AppController do
use RookWeb, :controller
- plug :add_token
-
def share(conn, _params) do
render(conn, "share.html")
end
@@ -10,12 +8,4 @@ defmodule RookWeb.AppController do
def request(conn, _params) do
render(conn, "request.html")
end
-
- defp add_token(conn, _params) do
- if conn.assigns[:token] do
- conn
- else
- assign(conn, :token, Rook.Utils.Token.token())
- end
- end
end
diff --git a/lib/rook_web/templates/app/entrypoint.html.eex b/lib/rook_web/templates/app/entrypoint.html.eex
index bc7e84a..7551fdc 100644
--- a/lib/rook_web/templates/app/entrypoint.html.eex
+++ b/lib/rook_web/templates/app/entrypoint.html.eex
@@ -1,5 +1,4 @@
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/#{@entrypoint}.css") %>"/>
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/#{@entrypoint}.js") %>"></script>
-<script>window.token = "<%= @token %>";</script>
<div id="app"></div>
diff --git a/lib/rook_web/templates/app/request.html.eex b/lib/rook_web/templates/app/request.html.eex
index c09a843..b7ff7bd 100644
--- a/lib/rook_web/templates/app/request.html.eex
+++ b/lib/rook_web/templates/app/request.html.eex
@@ -1 +1 @@
-<%= render_app(@conn, @token, "request") %>
+<%= render_app(@conn, "request") %>
diff --git a/lib/rook_web/templates/app/share.html.eex b/lib/rook_web/templates/app/share.html.eex
index d72b2c4..95b8b08 100644
--- a/lib/rook_web/templates/app/share.html.eex
+++ b/lib/rook_web/templates/app/share.html.eex
@@ -1 +1 @@
-<%= render_app(@conn, @token, "share") %>
+<%= render_app(@conn, "share") %>
diff --git a/lib/rook_web/templates/home/index.html.eex b/lib/rook_web/templates/home/index.html.eex
index 9152474..0b82f07 100644
--- a/lib/rook_web/templates/home/index.html.eex
+++ b/lib/rook_web/templates/home/index.html.eex
@@ -1 +1 @@
-<p>Hi! Welcome to Walmart.</p>
+<%= link "Share", to: Routes.app_path(RookWeb.Endpoint, :share)%>
diff --git a/lib/rook_web/views/app_view.ex b/lib/rook_web/views/app_view.ex
index 66c7f6a..31f1d0a 100644
--- a/lib/rook_web/views/app_view.ex
+++ b/lib/rook_web/views/app_view.ex
@@ -1,7 +1,7 @@
defmodule RookWeb.AppView do
use RookWeb, :view
- def render_app(conn, token, entrypoint) do
- render("entrypoint.html", conn: conn, token: token, entrypoint: entrypoint)
+ def render_app(conn, entrypoint) do
+ render("entrypoint.html", conn: conn, entrypoint: entrypoint)
end
end