Skip to content

Commit b9c9947

Browse files
authored
allow read-only access to YDoc to anonymous users (#550)
1 parent 060c7ba commit b9c9947

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

apps/web-yjs/src/yjs-server.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,31 @@ export async function startWsServer({ jwtSecret, port }) {
6666
const docName = request.url.slice(1).split("?")[0];
6767
const token = url.searchParams.get("token");
6868
const role = url.searchParams.get("role");
69+
let userId = "";
6970
if (token) {
7071
const decoded = jwt.verify(token, jwtSecret) as TokenInterface;
71-
const userId = decoded.id;
72-
const permission = await checkPermission({ docName, userId });
73-
switch (permission) {
74-
case "read":
75-
// TODO I should disable editing in the frontend as well.
76-
wss.handleUpgrade(request, socket, head, function done(ws) {
77-
wss.emit("connection", ws, request, { readOnly: true, role });
78-
});
79-
break;
80-
case "write":
81-
wss.handleUpgrade(request, socket, head, function done(ws) {
82-
wss.emit("connection", ws, request, { readOnly: false, role });
83-
});
84-
break;
85-
case "none":
86-
// This should not happen. This should be blocked by frontend code.
87-
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
88-
socket.destroy();
89-
return;
90-
}
91-
return;
72+
userId = decoded.id;
9273
}
74+
const permission = await checkPermission({ docName, userId });
75+
switch (permission) {
76+
case "read":
77+
// TODO I should disable editing in the frontend as well.
78+
wss.handleUpgrade(request, socket, head, function done(ws) {
79+
wss.emit("connection", ws, request, { readOnly: true, role });
80+
});
81+
break;
82+
case "write":
83+
wss.handleUpgrade(request, socket, head, function done(ws) {
84+
wss.emit("connection", ws, request, { readOnly: false, role });
85+
});
86+
break;
87+
case "none":
88+
// This should not happen. This should be blocked by frontend code.
89+
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
90+
socket.destroy();
91+
return;
92+
}
93+
return;
9394
}
9495
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
9596
socket.destroy();

0 commit comments

Comments
 (0)