pusher / pusher-websocket-react-native

React Native official Pusher SDK
MIT License
62 stars 55 forks source link

How do I handle auth? #115

Closed nandorojo closed 1 year ago

nandorojo commented 1 year ago

I'm a bit confused about how to handle auth with Pusher's native library here.

It's working fine on Web with this code with pusher-js and our auth endpoint:

new Pusher(PUSHER_KEY, {
  cluster: 'mt1',
  authEndpoint: PUSHER_AUTH_ENDPOINT,
  auth: {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  },
})

I tried to use similar code for pusher-websocket-react-native:

instance.init({
  apiKey: PUSHER_TOKEN,
  cluster: 'mt1',
  authEndpoint: PUSHER_AUTH_ENDPOINT,
  async onAuthorizer() {
    return {
      auth: `Bearer ${token}`,
    }
  },
})

However, this isn't working. Our backend isn't receiving the right header. What's the recommended way to do this? Is there a way to add headers?

benw-pusher commented 1 year ago

The onAuthorizer should return the full authentication JSON, as detailed at https://github.com/pusher/pusher-websocket-react-native#onauthorizer.

If you are looking to customize the headers sent when requesting an auth token from your endpoint then you will need to use the onAuthorizer callback to make the auth request manually. see https://github.com/pusher/pusher-websocket-react-native/issues/40#issuecomment-1322408009 for detail.