Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Remove websocket_user_token.
Browse files Browse the repository at this point in the history
This was hacky and not used anymore. Take a look at the next commit for
a better solution to get the current user inside LiveViews.
  • Loading branch information
totorigolo committed Jan 31, 2021
1 parent d6fdeb6 commit 8897569
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 36 deletions.
3 changes: 1 addition & 2 deletions assets/js/hooks/image_canvas_hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { HookInterface } from "../phoenix/types";
import { getUserToken } from "../utils";
import { ImageSocket } from "../image_socket";

interface CustomWindow extends Window {
Expand All @@ -26,7 +25,7 @@ export const image_canvas_hook: HookInterface = {
async function connectToLobby(lobby_id: string) {
await disconnectWindow();

window.imageSocket = new ImageSocket(getUserToken(), document.querySelector("#image-canvas"));
window.imageSocket = new ImageSocket(document.querySelector("#image-canvas"));
await window.imageSocket.connectToLobby(lobby_id);
}

Expand Down
7 changes: 3 additions & 4 deletions assets/js/image_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ export class ImageSocket {
private batch_queue: Array<Uint8Array> = [];
private last_batch_version: number = undefined;

constructor(user_token: string, image_canvas: HTMLCanvasElement) {
this.socket = ImageSocket.createSocket(user_token);
constructor(image_canvas: HTMLCanvasElement) {
this.socket = ImageSocket.createSocket();
this.canvas = new PixelCanvas(image_canvas);

this.socket.connect();
this.state = ImageSocketState.Connected;
}

private static createSocket(user_token: string): AsyncSocket {
private static createSocket(): AsyncSocket {
return new AsyncSocket("/msgpack-socket", {
params: { user_token: user_token },
// logger: (kind, msg, data) => console.log(`${kind}: ${msg}`, data),
decode: (packed_payload: unknown, callback: <T>(decoded: T) => void) => {
const decoded: PhxMessage = msgpack.decode(new Uint8Array(packed_payload as ArrayBuffer)) as PhxMessage;
Expand Down
5 changes: 0 additions & 5 deletions assets/js/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,3 @@ export function getUint40(dataView: DataView, byteOffset: number): number {
const right = dataView.getUint32(byteOffset + 1);
return 2 ** 32 * left + right;
}

export function getUserToken(): string {
const user_token_meta: HTMLMetaElement = document.querySelector("meta[name=\"websocket_user_token\"]");
return user_token_meta && user_token_meta.content || null;
}
20 changes: 0 additions & 20 deletions lib/pixel_forum_web/channels/msg_pack_user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ defmodule PixelForumWeb.MsgPackUserSocket do
use Phoenix.Socket,
serializer: PixelForumWeb.Transports.MessagePackSerializer

# One day in seconds
@user_token_validity 86_400

## Channels
channel "image:*", PixelForumWeb.ImageChannel

Expand All @@ -22,23 +19,6 @@ defmodule PixelForumWeb.MsgPackUserSocket do
@impl true
def connect(params, socket, _connect_info)

def connect(%{"user_token" => user_token}, socket, _connect_info) do
#
# This is not ideal, as Pow sessions have limited lifetimes that we are ignoring here.
# See: https://github.com/danschultzer/pow/issues/271
#
case Phoenix.Token.verify(socket, "websocket_user_token", user_token, max_age: @user_token_validity) do
{:ok, user_id} ->
{:ok,
socket
|> assign(:current_user, PixelForum.Repo.get!(PixelForum.Users.User, user_id))
|> assign(:unique_id, user_id)}

{:error, _} ->
:error
end
end

def connect(_params, socket, _connect_info) do
{:ok, assign(socket, :unique_id, System.unique_integer())}
end
Expand Down
5 changes: 0 additions & 5 deletions lib/pixel_forum_web/templates/layout/root.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
<%= live_title_tag assigns[:page_title] || "Home", suffix: " · Pixel Forum" %>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>

<%= if Pow.Plug.current_user(@conn) do %>
<%= tag :meta, name: "websocket_user_token",
content: Phoenix.Token.sign(@conn, "websocket_user_token", @current_user.id) %>
<% end %>
</head>
<body>
<header>
Expand Down

0 comments on commit 8897569

Please sign in to comment.