Skip to content

Commit 3946273

Browse files
committed
Add hook to prevent webcall until authorized
1 parent 46978ee commit 3946273

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

collab/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func handleMessages(
226226
log.Println("Authentication failed - no token attached")
227227
client.conn.WriteMessage(
228228
websocket.TextMessage,
229-
[]byte("Authentication failed - no JWT token"),
229+
[]byte("Authentication failed"),
230230
)
231231
client.conn.Close()
232232
break
@@ -248,12 +248,16 @@ func handleMessages(
248248
if !isSuccess {
249249
client.conn.WriteMessage(
250250
websocket.TextMessage,
251-
[]byte("Authentication failed - failed to find a matching room"),
251+
[]byte("Authentication failed"),
252252
)
253253
client.conn.Close()
254254
break
255255
}
256256
client.authenticated = true
257+
client.conn.WriteMessage(
258+
websocket.TextMessage,
259+
[]byte("Auth Success"),
260+
)
257261
log.Println("Client authenticated successfully")
258262
}
259263

peerprep/components/questionpage/CollabEditor.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export default function CollabEditor({
7373
const [value, setValue] = useState(questionSeed);
7474
const [socket, setSocket] = useState<WebSocket | null>(null);
7575
const [connected, setConnected] = useState(false);
76+
const [authenticated, setAuthenticated] = useState(false);
7677
const router = useRouter();
7778

7879
const generatePatch = (oldContent: string, newContent: string): string => {
@@ -157,16 +158,13 @@ export default function CollabEditor({
157158
newSocket.onmessage = (event) => {
158159
if (event.data == "Authentication failed") {
159160
window.alert("Authentication failed");
160-
if (socket) {
161-
socket.close();
162-
}
161+
newSocket.close();
163162
router.push("/questions");
164163
} else if (event.data == "The session has been closed by a user.") {
165-
window.alert("Session has ended");
166-
if (socket) {
167-
socket.close();
168-
}
169-
router.push("/questions");
164+
window.alert("Session has ended. If you leave the room now, this data will be lost.");
165+
newSocket.close();
166+
setAuthenticated(false);
167+
setConnected(false);
170168
} else {
171169
const message: Message = JSON.parse(event.data);
172170

@@ -221,7 +219,7 @@ export default function CollabEditor({
221219

222220
return (
223221
<>
224-
{connected && (
222+
{authenticated && (
225223
<CommsPanel className="flex flex-row justify-around" roomId={roomID} />
226224
)}
227225
<div className="m-4 flex items-center space-x-4 p-4">

0 commit comments

Comments
 (0)