How to transfer data to a model.js-File


#1

Hi, although I’m an Ionic / Angular / Node beginner, I’m almost done with my first major app, including the REST API. The JWT authentication also works very well, at least in the postman. But unfortunately I do not manage to hand over the token in Ionic from a provider to the middleware.

I have the following situation:
server/src/routes/product.js

...
  app.get('/products', checkAuth, (req, res) => {
   productsModel.getProducts((err, data) => {
      res.status(200).json(data);
    });
  });
...

and the middleware
checkAuth.js

const jwt = require('jsonwebtoken');

module.exports = (req, res, next) => {
        try {
            const token = req.headers.authorization.split(" ")[1];  

            const decoded = jwt.verify(token, 'mysecret');
            req.userData = decoded;
        
            next();
        } catch (error) {
            return res.status(401).json({
                message: "token error"
            });

        }
    }

So, everything works fine in Postman. But I do not succeed in getting the token from one fole to the other.

Now a probably ridiculous question for experienced programmers: how do I get a variable, namely the token, from my login.ts file into the checkAuth.js file.
I also implemented storage. That too works. But as I said, how do I get a value from the login.ts in checkAuth.js?

many many thanks!