pusher / chatkit-client-js

JavaScript client SDK for Pusher Chatkit
https://pusher.com/chatkit
MIT License
90 stars 15 forks source link

access_token Authorisation issue (provide an example for access_token usage?) #91

Closed swhiber closed 6 years ago

swhiber commented 6 years ago

I've been trying to add a new room via a request to my server

fetch('http://localhost:3001/newRoom', {
                    method: 'POST',
                    headers: { 
                        'Content-Type': 'application/json',
                        'Authorization': 'Bearer '+btoa(this.state.accessToken)
                    },
                    body: JSON.stringify({
                            creatorId: (this.state.origin_username).toString(),
                            name: `${this.state.origin_username}&${this.state.dest_username}`,
                            userIds: [currentUser.id,this.state.dest_username]
                    }) 
                })
                .then((response) => {
                    console.log(response.json());
                })
                .catch(error => console.error('error', error))

However I receive this error error: 'services/chatkit_authorizer/authorization/invalid_token_subject', error_description: 'Missing token subject', error_uri: 'https://docs.pusher.com/errors/services/chatkit_authorizer/authorization/invalid_token_subject'

I'm unsure if I'm making a silly error or whether there is an issue with SDK. Some more documentation on the topic would probably resolve this either way :)

gianpaj commented 6 years ago

Looks like your Auth Pusher server is not returning the correct data to your ChatKit client.

Using express 4.x you can this in your Node.Js server:

   const auth = chatkit.authenticate({
      userId: user_id, // your app user id
      authPayload: req.body,
    });
    return res.json(auth.body);

See also this example: https://github.com/pusher/chatkit-server-node/blob/master/examples/auth_server.js

swhiber commented 6 years ago

So my server is returning a valid access_token, however the error states there is a 'Missing token subject' so I'm wondering whether I have to add anymore headers? Thanks for responding, here are my headers at the moment (same as in first post)

headers: { 
                        'Content-Type': 'application/json',
                        'Authorization': 'Bearer '+btoa(this.state.accessToken)
                    }
hamchapman commented 6 years ago

A missing subject means that there's no sub claim in the token that is being generated. We use the sub (subject) claim to store the user's ID, so it sounds like you're not providing a user ID when you generate your token(s).

swhiber commented 6 years ago

It was to do with the user_id! Thanks a lot for your help