Skip to content

Commit

Permalink
Fix race condition of disconnecting during auth (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusLongmuir authored Aug 2, 2024
1 parent 45912e3 commit aa6d74b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/3d-web-user-networking/src/UserNetworkingServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class UserNetworkingServer {
if (!client.authenticatedUser) {
if (parsed.type === USER_NETWORKING_USER_AUTHENTICATE_MESSAGE_TYPE) {
this.handleUserAuth(client, parsed).then((authResult) => {
if (client.socket.readyState !== WebSocketOpenStatus) {
// The client disconnected before the authentication was completed
return;
}
if (!authResult) {
// If the user is not authorized, disconnect the client
const serverError = JSON.stringify({
Expand All @@ -147,6 +151,7 @@ export class UserNetworkingServer {
}

const userData = authResult;
client.authenticatedUser = userData;

// Give the client its own profile
const userProfileMessage = JSON.stringify({
Expand Down Expand Up @@ -262,7 +267,6 @@ export class UserNetworkingServer {
}

console.log("Client authenticated", client.id, resolvedUserData);
client.authenticatedUser = resolvedUserData;

return resolvedUserData;
}
Expand Down

0 comments on commit aa6d74b

Please sign in to comment.