chatengine-io / react-chat-engine

React component for a cheap and easy chat API
138 stars 35 forks source link

Adding member to an existing chat returns 404 not found #109

Open AjayiMike opened 2 years ago

AjayiMike commented 2 years ago

I'm using a nodejs server as a proxy between my react app and the chat engine api, everything works well except adding a new member to an existing chat. I get a 404 error. I checked everywhere, I couldn't find help

part of the frontend where the request is sent to the backend

// get user if exist or create if not
const userResponse =  await fetch("http://localhost:8000/api/user/get-or-create", {
    method: "POST",
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({username, password})
  })

const userData = await userResponse.json()

// add this user to the chat
const chatResponse =  await fetch("http://localhost:8000/api/chat/add-member", {
    method: "POST",
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        chat_id: currentlyDisplayedPurseDetails.chatId,
        username: userData.username,
        password
    })
})

The backend code: getting or creating a user works well, so I'll only share the part of the code that is not working, which is adding new member to a chat

exports.addNewPurseMemberToChat = async (req, res) => {
    const {chat_id, username, password} = req.body
    if(!chat_id || !username) return res.status(401).json("chat id and username required")
    const requestConfig = {
        method: 'post',
        url: `https://api.chatengine.io/chats/${chat_id}/people/`,
        headers: {
            'Project-ID': process.env.CHATENGINE_PROJECT_ID,
            'User-Name': username,
            'User-Secret': password
        },
        data: {
            'username': username
        }
    };

axios(requestConfig)
  .then(response => {
      res.status(200).json(response.data)
  })
  .catch(error => {
      console.log(error)
      res.json(error)
  });

}

I am however receiving 404 not found error, and I'm sure that the chat with that chat id exist in my project

alamorre commented 2 years ago

The Owner of this chat needs to make the API call or you need to Auth with the Private-Key instead.

I user should not be able to add them-self to any chat by default. This is a permission/security feature.

FIX: Authenticate with the Private-Key on your server (in the headers) instead 👍

AjayiMike commented 2 years ago

I already solved the Issue. I kept guessing what the UserName and User-secret in the header means. It finally worked when I imputed the chat admin credentials. I suggest it should be renamed to tell whose credentials is supposed to be in the header to avoid guessing. Maybe chatAdmin-username and chatAdmin-user-secret would be better. Thanks