0

i am trying to integrate a third party chat api cometchat into my project. i basically need to send an http request with a header containing my appId and apiKey programmatically to create my app users on chat. my app uses mongodb to store user info and jwt for authentication. but whenever i send the request through the react front end,it returns an error 401 instead of returning a token for chat.

import express from "express";
import axios from "axios";
import authenticate from "../middlewares/authenticate";
const router = express.Router();
const User = require('../models/User');

const appID = "********";
const apiKey = "************";

const url = 'https://api.cometchat.com/v1';

const headers = {
  'Content-Type': 'application/json',
  appid: appID,
  apikey: apiKey,

};

router.get('/create',authenticate, (req, res) => {
  const data = {
    uid: req.currentUser.id,
    name: req.currentUser.name
  };

  axios.post(`${url}/users`, JSON.stringify(data), {
      headers,
    })
    .then(response => {
      requestAuthToken(response.data.data.uid)
        .then(token => {
          console.log('Success:' + JSON.stringify(token));
          res.json(token);
        })
        .catch(error => console.error('Error:', error));
    })
    .catch(error => console.error('Error:', error));
});

router.get('/auth', (req, res) => {
  const uid = req.query.uid;
  requestAuthToken(uid)
    .then(token => {
      console.log('Success:' + JSON.stringify(token));
      res.json(token);
    })
    .catch(error => console.error('Error:', error));
});

const requestAuthToken = uid => {
  return new Promise((resolve, reject) => {
    axios.post(`${url}/users/${uid}/auth_tokens`, null, {
        headers,
      })
      .then(response => {
        console.log('New Auth Token:', response.data);
        resolve(response.data.data);
      })
      .catch(error => reject(error));
  });
};

export default router;

in the above code, "currentUser" is the logged in user who wants to initiate a chat and "authenticate" middleware helps to extract the users info.

see the server error message below after sending the server request:

Error: { Error: getaddrinfo ENOTFOUND api.cometchat.com api.cometchat.com:443 at errnoException (dns.js:50:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26) code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'api.cometchat.com', host: 'api.cometchat.com', port: 443, config: { url: 'https://api.cometchat.com/v1/users/undefined/auth_tokens', method: 'post', data: null, headers: { Accept: 'application/json, text/plain, /', 'Content-Type': 'application/json', appid: '*************', apikey: '**********************', 'User-Agent': 'axios/0.19.0' },

See React Front end error below:

New message incoming! ffffff
ClientChat.js:139 Message sending failed with error: 
  • Your UID looks incorrect when you are calling the API. See ```https://api.cometchat.com/v1/users/undefined/auth_tokens```. ```undefined``` should be the actual UID. – Alec Smart Aug 15 '19 at 22:33
  • @Alec Smart, could you please assist with the right code snippet while calling the API? Thanks – echelon umeh Aug 17 '19 at 02:11

0 Answers0