Monday, 15 May 2017

Node.js authentication using passport and typescript: cannot find req.user

I am currently working on a authentication service for a node.js microservices application using typescript, WebStorm, passport and jwt. While trying to add the route to "/api/login", I am noticing that the intellisense does not seem to pick up the user object of req.user or the authorization object of req.header.authorization. For example, the following method is not working because it can not find the user object:

private generateToken(req: Request, res: Response, next: NextFunction){
req.token = jwt.sign({
  id: req.user.id,
  firstname: req.user.firstname,
  lastname: req.user.lastname,
  roles: req.user.roles
}, process.env.AUTH_KEY, {
  expiresIn: "7d"
});
return next();
}

I am using the Request object from express:

import { NextFunction, Request, Response, Router } from "express";

Would I need to use a different Request object?

Also, if I need to force authentication to certain api routes but lock other routes down, how should this be done using passport-jwt? I know there is an express-unless package that I can use for express-jwt. Any help would be greatly appreciated, as I am stuck on this issue at the moment.



via user1790300

No comments:

Post a Comment