ConnectyCube / connectycube-reactnative-samples

Chat and Video Chat code samples for React Native, ConnectyCube
https://connectycube.com
Apache License 2.0
124 stars 111 forks source link

error 401 on create session #209

Closed JhosepIslam closed 3 years ago

JhosepIslam commented 3 years ago

After a period of non-use, an alert request failed with status code 401 appears and we detect that this message comes from its react-native-connectycube package.

We have Session expiration enabled in the configuration but it seems that it is not working, I hope you can help us detect the problem and solve it.

react-native-connectycube: 1.8.1 React Native: 0.61.5

DaveLomber commented 3 years ago

Are you following the session expiration guide?

https://developers.connectycube.com/js/authentication-and-users?id=session-expiration

Please show your implementation, we have to review it

JhosepIslam commented 3 years ago

Are you following the session expiration guide?

https://developers.connectycube.com/js/authentication-and-users?id=session-expiration

Please show your implementation, we have to review it

Yes, we follow the entire session expiration guide, in fact it has worked well for a long time, the problem started a few days ago

JhosepIslam commented 3 years ago

Are you following the session expiration guide?

https://developers.connectycube.com/js/authentication-and-users?id=session-expiration

Please show your implementation, we have to review it

The implementation is exactly the same from the documentation and we cannot catch the error because it is generated in the node_modules package of connectycube

JhosepIslam commented 3 years ago

any idea that could be causing the bug?

DaveLomber commented 3 years ago

@JhosepIslam I suggest to perform more tests and see how this code works, especially - the following flow:

1) add the above code in your app to manage session expiration (if not already added) 2) then run the app, normally 3) then, from logs, get the current user's session token 4) then in parallel, using REST API, remove the session https://developers.connectycube.com/server/auth?id=destroy-session 5) then back to app and see how it behaves. Does the session expiration flow work well? If not - can you spot the issue?

eastcoastcoder commented 3 years ago

Not to hijack this thread but I've been having issues as well. I'm continuing to use the config.json but I have done an object spread of a function to handle sessionExpired. Is this a valid approach? I've thrown breakpoints and log statements in but they don't seem to get hit either.

image image

DaveLomber commented 3 years ago

@ethanx94 object spread for ConnectyCube config is not an issue at all

Your code looks correct

Could you please do me a favour

Please enable logs (by passing debug: { mode: 1 }), e.g.

const CONFIG = {
  debug: { mode: 1 }, // enable DEBUG mode (mode 0 is logs off, mode 1 -> console.log())
};
ConnectyCube.init(CREDENTIALS, CONFIG);

and then post here the output at the tome when you expect a session expiration handler to be called

eastcoastcoder commented 3 years ago

I think I have it worked out. I was using some old 2.0 version where the check for isExpiredSessionError was looking for a different server response. I don't have the 2.0 code anymore but the offender was here error.info.errors.base[0], it was checking against something else in the old versions. image

Additionally I had to create a session using parameters (possibly because I'm using external identity provider with firebase) and keep my refreshed token, as this error interrupts the signIn method that gets hit on automatic login and would cause the XMPP connect call to fail.

image

I made this reInit method because occasionally my credentials appear to be missing. image

(The following code is in the signIn method) image

image

In general I think I'm running off some very old example code that I have changed a lot but I wanted to post here for reference in case others were experiencing the same issues.

The simple fix for most people would probably just be a quick npm i react-native-connectycube@latest

DaveLomber commented 3 years ago

@ethanx94 much appreciated for the update and the solution 🥇

eastcoastcoder commented 3 years ago

One more slight issue with this. The avatar links in all the User objects are still holding the old token as a query parameter. I think this can be changed in the getOccupants method in users-service. Maybe some check to replace this token if it has been refreshed?

DaveLomber commented 3 years ago

@ethanx94 I highly recommend not to store a full url as avatar Instead - store file uid and then build a full url on the flight using a helper const fileUrl = ConnectyCube.storage.privateUrl(fileUID); like described in a file attachments section https://developers.connectycube.com/reactnative/messaging?id=attachments-photo-video

eastcoastcoder commented 3 years ago

I think the issue here is that users are stored with their avatar URL's fully intact because of the model. When the token refreshes it would need to update all the users's avatar urls. https://github.com/ConnectyCube/connectycube-reactnative-samples/blob/master/RNChat/src/models/user.js

My workaround for the time being is to just clear all users out of the store when the token gets refreshed, at the end of my on session expired handler. That way when getOccupants is called it creates a new user object for each user and builds up the proper urls. It's probably some additional network requests but it's not too taxing. https://github.com/ConnectyCube/connectycube-reactnative-samples/blob/master/RNChat/src/services/users-service.js