pusher / chatkit-client-js

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

Having trouble getting currentUser #96

Closed Gabriel416 closed 6 years ago

Gabriel416 commented 6 years ago

I'm trying to set up chatkit for my app and everything seems to work until I try to connect and get the currentUser. Looks like one request from the server results in a 204 and the other request seems fine. The function below is fired once on mount of a component.

connect({ commit, state, rootState }) {
      let chatManager = new Chatkit.ChatManager({
        instanceLocator: 'v1:us1:56256341-7b34-4ec2-a238-be971f1ab0d0',
        userId: rootState.user.account.id.toString(),
        tokenProvider: new Chatkit.TokenProvider({
          url: 'http://localhost:3333/api/v1/token',
          headers: {
            Authorization: 'bearer ' + rootState.authentication.token,
          },
        }),
      });
      console.log(chatManager, 'chat manager');
      chatManager
        .connect()
        .then(currentUser => {
          console.log(currentUser, 'CURRENT USER');
        })
        .catch(error => console.error('error', error));
    },

screenshot 4

screenshot 6

screenshot 5

Gabriel416 commented 6 years ago

screenshot 8

Looks like the user was created in the pusher dashboard. But I can't seem to connect. Please let me know if I'm missing anything silly, or if more info is needed. Thanks.

kkcodes commented 6 years ago

Hi Gabriel416,

For the second last image, where you can see the Ajax response which has the status, body, and header. So, instead of sending those entire details, return the body only and set the status code as whatever you get.

So, the JSON returned will look like this:

{ "access_token":"receivedToken", "token_type":"bearer", "expires_in":86400 }

Also, hope that the rootState.authentication.token, generates the correct JWT.

Gabriel416 commented 6 years ago

Hello @kkcodes ! server side I'm sending the body now and I'm running into this

screenshot 10

This is what I'm getting back from my server

screenshot 9

rootState.authentication.token is an api token needed to make requests to my server and I have verified it is correct.

callum-oakley commented 6 years ago

Hi @Gabriel416,

The error is complaining that there is no user ID in your token, which the connection request needs to identify who the user is. Can I see the code you're using to generate the token?

Gabriel416 commented 6 years ago

screenshot 11

The register method is where I'm creating the chatkit user.

screenshot 12

this method is the one that gets hit for the tokenProvider

screenshot 13

this is what my ChatkitService looks like

callum-oakley commented 6 years ago

What version of the node SDK are you using? In the latest version at least, authenciate takes a single object with a userId. e.g.

app.post('/auth', (req, res) => {
  const authData = chatkit.authenticate({
    userId: req.query.user_id
  });

  res.status(authData.status)
     .send(authData.body);
})
Gabriel416 commented 6 years ago

@callum-oakley thank you works like a charm now! I'm using "@pusher/chatkit-server": "^0.12.2". I was loosely following https://pusher.com/tutorials/chat-adonisjs#get-user-details and it seems like there is some outdated stuff. Thanks again cheers.

callum-oakley commented 6 years ago

Great to hear! Thanks for letting us know :)