From 09693b979efe3c9c7ea64f79d97a1f0b53f7c49e Mon Sep 17 00:00:00 2001 From: Melonai Date: Sat, 8 May 2021 23:41:03 +0200 Subject: Socket connection and token fetching --- lib/rook_web/channels/share_channel.ex | 11 +++++++++++ lib/rook_web/channels/token_channel.ex | 11 +++++++++++ lib/rook_web/channels/user_socket.ex | 6 ++++-- lib/rook_web/controllers/app_controller.ex | 10 ---------- lib/rook_web/templates/app/entrypoint.html.eex | 1 - lib/rook_web/templates/app/request.html.eex | 2 +- lib/rook_web/templates/app/share.html.eex | 2 +- lib/rook_web/templates/home/index.html.eex | 2 +- lib/rook_web/views/app_view.ex | 4 ++-- 9 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 lib/rook_web/channels/share_channel.ex create mode 100644 lib/rook_web/channels/token_channel.ex (limited to 'lib') 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 @@ "/> -
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 @@ -

Hi! Welcome to Walmart.

+<%= 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 -- cgit 1.4.1