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

Creating private chat returns error "Forbidden. Need user" #207

Closed aravi365 closed 3 years ago

aravi365 commented 3 years ago

Tried to create a dialog but it gives me and error: 403: "Forbidden. Need user"

const params = {
                      type: 3,
                      occupants_ids: [3527882],
                    };
                    ConnectyCube.chat.dialog
                      .create(params)
                      .then((dialog) => {
                      })
                      .catch((error) => { 
                      });

I am stuck here without any ways to troubleshoot what went wrong!

aravi365 commented 3 years ago

Here is how it looks like:

Screenshot 2021-02-22 at 11 54 44 AM
aravi365 commented 3 years ago

Is there anything I missed? These are the codes i used for initialising and signing in

async function initChat() {
      const CREDENTIALS = {
        appId: 4xxx,
        authKey: xxxx,
        authSecret: xxxx',
      };
      const CONFIG = {
        debug: {mode: 1}, // enable DEBUG mode (mode 0 is logs off, mode 1 -> console.log())
      };
      ConnectyCube.init(CREDENTIALS, CONFIG);
    }
    async function signin() {
      ConnectyCube.createSession()
        .then((session) => {
          console.log('session', session);
        })
        .catch((error) => {
          console.log('session err', error);
        });
      const userCredentials = {
        userId: 35278xx,
        password: 'xxxxx',
      };
      ConnectyCube.chat
        .connect(userCredentials)
        .then(() => {})
        .catch((error) => {
          console.warn(error);
        });
    }
DaveLomber commented 3 years ago

There are 2 issues: 1) you have to login or create a session with user, e.g.

either create a session with user creds in one go

 const userCredentials = { login: "cubeuser", password: "awesomepwd" };
 ConnectyCube.createSession(userCredentials)
      .then((session) => {
        console.log('session', session);
      })
      .catch((error) => {
        console.log('session err', error);
      });

or create session and then login:

 ConnectyCube.createSession()
      .then((session) => {
        console.log('session', session);

        const userCredentials = { login: "cubeuser", password: "awesomepwd" };
        ConnectyCube.login(userCredentials)
         .then((user) => {})
         .catch((error) => {});
        })
    .catch((error) => {
      console.log('session err', error);
    });

https://developers.connectycube.com/js/authentication-and-users?id=upgrade-session-token-user-login

  1. to create a chat - you have to wait until you got a user session, e.g.
 const userCredentials = { login: "cubeuser", password: "awesomepwd" };
 ConnectyCube.createSession(userCredentials)
      .then((session) => {
        console.log('session', session);

         /// HERE YOU CAN CREATE A CHAT NOW
      })
      .catch((error) => {
        console.log('session err', error);
      });
aravi365 commented 3 years ago

It worked but when sending messages, the connection status shows false and message is not sent, but i am getting a message id in the response. Here is my helper method for sending message

export const sendMessage = async (messageText) => {
  const opponentId = xyxyxyxy;
  const dialogId = 'xxxxxxxxxxxxxxxxxxxxx';
  const message = {
    type: 3,
    body: messageText,
    extension: {
      save_to_history: 1,
      dialog_id: dialogId,
    },
    markable: 1,
  };

  const userCredentials = {
    login: 'xxxx@xxxxxx.xxx',
    password: 'xxxxxx',
  };

  try {
    const isConnected = await ConnectyCube.chat.isConnected;
    console.log('CONNECTION STATUS', isConnected);
    let msgId = await ConnectyCube.chat.send(opponentId, message);
    console.log('response after send', msgId);
  } catch (err) {
    console.log(err);
  }
};

Here are the logs @DaveLomber

Screenshot 2021-02-22 at 2 59 23 PM