#Authentication on custom Websocket Server

2 messages · Page 1 of 1 (latest)

kindred sequoia
#

Hi,

I want to use a custom Websocket Server (using Bun) for my application. However I cant really figure out authentication on custom servers. Session cookies seem to be HTTP and secure only, which prevents me from reading them in localhost.

How would I implement the authentication here?

Bun.serve({
  port: parseInt(process.env.PORT as string),
  hostname: "localhost",
  development: true,
  async fetch(req, server) {

    // ... authenticate here

    if (server.upgrade(req)) {
      return undefined;
    }

    return new Response("Upgrade failed", { status: 500 });
  },
  websocket: {
    // ...
  },
});
ivory gulch
#

the way discord handle websocket is that :

  • the user connects to the websocket
  • then sends a message with the auth infos (e.g. the discord token)
  • if the server receive the auth infos, it checks it and start the handle for the messages
  • if the user does not send the auth message in 60s, the server disconnect the user