cometchat / cometchat-sample-app-react-native

Voice & Video Calling and Text Chat App for React Native
https://www.cometchat.com
Other
328 stars 158 forks source link

False API_KEY_NOT_SET #72

Closed lance-zigazoo closed 1 year ago

lance-zigazoo commented 1 year ago

Describe the problem

I am seeing a API_KEY_NOT_SET when creating a user and then logging them in with the React Native SDK.

What was the expected behavior?

Create a user and then log them in.

Reproduction

Detail the steps taken to reproduce this error, and whether this issue can be reproduced consistently or if it is intermittent.

  1. Init CometChat, `const appSettings = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion(REGION) .build();

    await CometChat.init(COMET_CHAT_APP_ID, appSettings);`

  2. Create a new user, const ccUserObj = new CometChat.User({ uid: userId, name: username }); newCometChatUser = await CometChat.createUser(ccUserObj, COMET_AUTH_KEY);
  3. Log the user in, const loggedInUser = await CometChat.login(cometChatUserId, COMET_AUTH_KEY);
  4. Optional delete the user and start again (use the delete endpoint)

Full code snippet

const COMET_AUTH_KEY = 'the_key';
const COMET_CHAT_APP_ID = 'whatever';
// other consts omitted
const initCometChat = async () => {
  try {
    if (!cometInited) {
      console.log('Initializing CometChat');
      const appSettings = new CometChat.AppSettingsBuilder()
        .subscribePresenceForAllUsers()
        .setRegion(REGION)
        .build();

      await CometChat.init(COMET_CHAT_APP_ID, appSettings);
    }
  } catch (err) {
    console.err('[initCometChat]', 'Could not initialize CometChat', err);
  }
};

try {
    loggedInUser = await CometChat.login(cometChatUserId, COMET_AUTH_KEY);
  } catch (err) {
    const { code } = err;
    switch(code) {
      case LOGIN_IN_PROGRESS:
        console.debug(LOGIN_IN_PROGRESS);
        await new Promise((r) => setTimeout(r, 1000));
        loggedInUser = await CometChat.login(cometChatUserId, COMET_AUTH_KEY);
      case ERR_UID_NOT_FOUND:
        console.debug(context, 'ERR_UID_NOT_FOUND');
        await createCometChatUser(cometChatUserId, username);
        await new Promise((r) => setTimeout(r, 1000));
        loggedInUser = await CometChat.login(cometChatUserId, COMET_AUTH_KEY);
      case API_KEY_NOT_SET:
        console.log(context, 'API_KEY_NOT_SET');
        await new Promise((r) => setTimeout(r, 1000));
        await initCometChat();
        loggedInUser = await CometChat.login(cometChatUserId, COMET_AUTH_KEY);
      default: await CometChat.login(cometChatUserId, COMET_AUTH_KEY);
        console.log('Unhandled comet error', code);
    }

You will see an error come back asAPI_KEY_NOT_SET, when it is in fact set. Sometimes, the error comes back as {"code": "-1", "details": {}, "message": "please initialize the cometchat before using login method.", "name": "COMETCHAT_INITIALIZATION_NOT_DONE"}. This is also not the case, because I'm 100% certain that initialization has taken place.

Please provide the following:

-Version used: "@cometchat-pro/react-native-chat": "3.0.2"

cometchat-helpcenter-bot commented 1 year ago

Keagan Ellis (CometChat Team) replied:

Hello Lance,

Thanks for sharing these details. We will investigate and will reach out with an update shortly.

Keagan Ellis CometChat

cometchat-helpcenter-bot commented 1 year ago

Jitvar (CometChat Team) replied:

Hi, I am from the Implementation team. We will check and get back to you.

Jitvar Patil Principal software Developer CometChat

cometchat-helpcenter-bot commented 1 year ago

Jitvar (CometChat Team) replied:

Hi,

Can you let us know where exactly are you calling CometChat.login() method. From the above snippet you have shared it looks like your CometChat.init() is being called only when you get an error when calling CometChat.login() method which is the incorrect approach.

The ideal flow should be as followed: CometChat.init() -> CometChat.login() {If the user does not exist, then call CometChat.createUser() -> CometChat.login()}. They do not have to call CometChat.login() in any other case.

Also, we noticed that you are using an older version of SDK, It's always recommended to be on the latest version as the latest version has all the optimizations and fixes. The latest version of our Chat SDK is v3.0.11.

Hope this was helpful Regards

Jitvar Patil Principal software Developer CometChat