APIs & Integrations

sali6
Member

how to set jwt in my api route in node js

I am new to Nodejs. I tried to create an API that connect CRM to my website, during which it will be sending data and I get no token error. I tried to solve that but I can't. This is my route file : unionwells germany

const express = require("express");
const auth = require("../../middleware/auth");
const soap = require('strong-soap').soap;
const router = express.Router();



/**
 *
 * Package: npm i strong-soap
 *
 * Description:  A simple solution to save person.
 */


// Enter the field with the username and password that has the necessary permission to find the person.
const username = 'admin';
const password = 'admin';


// @route   Get api/users/createCustomer
// @desc    Create new customer in PayamGostar
// @access  Private
router.post (
    "/",
     auth,
    (req, res) => {
      User.findById(req.user._id).then((user) => {
        // Replace <url> keyword to your CRM host address.
        let url = "http://sajjadshiasi.demo.payamgostar.com/Services/API/IPerson.svc?wsdl";
        let requestArgs = {
          username: username,
          password: password,
          person: {
            FirstName: "Sajjad",
            LastName: "Shiasi",
            CrmObjectTypeCode: "person",
            IdentityType: "حقیقی",
            Categories: { CategoryInfo: { Key: "siteclients" } },
            PhoneContacts: {
              IdentityContactPhone: {
                PhoneNumber: "901",
                PhoneType: "موبایل",
                IsDefault: true,
              },
            },
            Emails: { string: ["4@test.com"] },
            Subject: "عضویت در سایت",
          },
        };
        let options = {};
        soap.createClient(url, options, async function (err, client) {
          let method = client["SavePerson"];
          console.log(client);
          await method(requestArgs, function (err, result, envelope, soapHeader) {
            if (result.SavePersonResult.Success) {
              user.CrmId = result.SavePersonResult.CrmId;
              User.findByIdAndUpdate(
                user._id,
                user,
                { upsert: true },
                function (err, doc) {
                  if (err) return res.send(500, { error: err });
                  return res.send("Succesfully saved.");
                },
              );
            }
          });
        });
      });
    },
  );
module.exports = router;

and this is my middleware auth :

    const config = require("config");
const jwt = require("jsonwebtoken");

function auth(req, res, next) {
  const token = req.header("x-auth-token");
  // Check for Token
  if (!token) {
    return res.status(401).json({ msg: "No Token" });
  }

  try {
    // Verify Token
    const decoded = jwt.verify(token, config.get("jwtSecret"));
    // Add User From Payload
    req.user = decoded;
    next();
  } catch (e) {
    res.status(400).json({ msg: "Token is not valid!" });
  }
}

module.exports = auth;

The important thing is that my database and CRM database is the same in other hand I use CRM database.

0 Upvotes
2 Replies 2
louischausse
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

how to set jwt in my api route in node js

@sali6 When you say "Connect CRM to my website" What do you mean exactly?


Can you define  a little bit more by writing down stories in this form:

When X happens on my website I want Y to happen in HubSpot CRM
When Z happens in HubSpot CRM I want W to happen in my website

Thanks

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
dennisedson
HubSpot Product Team
HubSpot Product Team

how to set jwt in my api route in node js

@louischausse , do you have any tips for @sali6 ?

0 Upvotes