pusher / chatkit-server-php

PHP SDK for Pusher Chatkit
https://pusher.com/chatkit
MIT License
35 stars 20 forks source link

Instance ID in the path and the access token don't match #24

Closed liamandrewmaddison closed 6 years ago

liamandrewmaddison commented 6 years ago

What?

I am currently building a react native app with chatkit, however the authentication seems to fail and keeps bringing back the following message: Instance ID in the path and the access token don't match

I'm using Laravel as a backend and have the following route: Route::post('/chatkit-auth-user', 'API\MessagesController@authenticateChatkitUser');

Within this method I authenticate the user depending on their user ID like so:

public function authenticateChatkitUser(Request $request)
{
    $userId = $request->input('user_id');
    $authData = $this->chatkit->authenticate([
        'user_id' => "{$userId}",
    ]);

    return response()
        ->json(
            $authData['body'],
            $authData['status']
        );
}

The token is generate which is then obviously passed into the TokenProvider instance like so:

const url = 'https://my-endpoint.com/api/chatkit-auth-user';
const userToken = this.state.user.authToken;
const tokenProvider = new Chatkit.TokenProvider({
  url,
  headers: {
    Authorization: 'Bearer ' + userToken,
    'Content-Type': 'application/json',
  },
});
const chatManager = new Chatkit.ChatManager({
  instanceLocator: '<MY_INSTANCE_LOCATOR_ID>',
  userId: `${this.state.user.userId}`,
  tokenProvider: tokenProvider
});

chatManager.connect().then(currentUser => {
  this.state.currentUser = currentUser;
  console.log(currentUser);
  this.currentUser.subscribeToRoom({
    roomId: CHATKIT_ROOM_ID,
    hooks: {
      onNewMessage: this.onReceive.bind(this)
    }
  });
});

currentUser never gets consoled and always returns a 403 with the message in the title. From the docs, I think I've implemented everything correctly, I have even span up a heroku app with https://github.com/pusher/chatkit-server-node, the same issue still occurs. I have spent a couple of days trying to figure this out and would appreciate if I could get some guidance if I'm doing anything wrong!

hamchapman commented 6 years ago

The error suggests that you're using a different instance locator (of which an instance ID is a component part) in your client and your server. Are you definitely using the same in both places?

liamandrewmaddison commented 6 years ago

@hamchapman, thank you for your reply. The ID's were perfectly fine, I got it working by deleting my instance which was slightly annoying, alas, it worked after using the new key and ID.

So you can close this :)

hamchapman commented 6 years ago

Okay, thanks for following up 👍

wisthsb1519 commented 4 years ago

i had the same error. deleting the instance and passing in the new values worked for me as well