Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add Authentication headers? #1

Open
ericallam opened this issue Sep 13, 2024 · 1 comment
Open

Is it possible to add Authentication headers? #1

ericallam opened this issue Sep 13, 2024 · 1 comment

Comments

@ericallam
Copy link

ericallam commented Sep 13, 2024

We currently use partysocket and need to add an Authentication header with a bearer token, but the only way to do this is with the "Websocket Constructor" trick:

import { WebSocket } from "partysocket";
import type { ClientRequestArgs } from "node:http";
import { type ClientOptions, WebSocket as wsWebSocket } from "ws";

new WebSocket(websocketUrl.href, [], {
 WebSocket: WebsocketFactory(accessToken),
});

function WebsocketFactory(apiKey: string) {
  return class extends wsWebSocket {
    constructor(address: string | URL, options?: ClientOptions | ClientRequestArgs) {
      super(address, { ...(options ?? {}), headers: { Authorization: `Bearer ${apiKey}` } });
    }
  };
}

Is it possible using the params option to add headers? If not, it would be great to add this functionality 👍

@dev-badace
Copy link
Contributor

thankx @ericallam for opening the first issue on Socket :)

socket was primarily made with browsers in mind. so it very much also works similarly to partysocket in this case. as the header option is not something available on the client side (browsers).

i am thinking for a little more general purpose solution for this case. what do you think of this solution below?

  params: async (info) => {
    const protocols: string[] = [];
    const extraOptions = {
      headers: {
        Authorization: "Bearer token",
      },
    };
    return {
      //this gets added to the query
      query: {}, 
      
     //these are passed to the websocket constructor -> new Websocket(url, ...extraOptions)
      extraOptions: [protocols, extraOptions], 
    };
  },

or maybe something like this

new Socket(url, protocols, {
  extraOptions: [options]  // these are passed to the websocket constructor -> new Websocket(url, protocols, ...extraOptions)
})

or a new option altogether that makes sense for nodejs/server-side clients. the difference between this and the extraOptions above is. the one above is pretty much static. but this one is a function that also receives the data you return from params function and is more flexible~

new Socket(url, protocols, {
   //authData is the data you return from params. 
   extraOptions: (authData: any) => {
       return [options] //these are passed to the websocket constructor -> new Websocket(url, protocols, ...extraOptions)
   }
})

I personally prefer the last extraOptions . as it is more flexible and dynamic. let me know what you think!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants