Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thecodermehedi/84bddee8f7c65eadf340e32e0c0a269a to your computer and use it in GitHub Desktop.
Save thecodermehedi/84bddee8f7c65eadf340e32e0c0a269a to your computer and use it in GitHub Desktop.
Problem: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'

Problem: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'

When working with Express and jsonwebtoken, it's better to use res.locals to store user-related data rather than modifying the req object directly. This approach adheres to best practices and avoids potential issues with modifying the req object.

Here’s why:

  1. Separation of Concerns: res.locals is designed for passing data to subsequent middleware or templates. It keeps the request object clean and unmodified.
  2. Middleware-Safe: Using res.locals ensures that the data is available to other middleware without interfering with the original request object.

Instead of:

req.user = decoded; // typeof decoded is JwtPayload

Use:

res.locals.user = decoded;

This way, the decoded payload from the JWT can be accessed safely in subsequent middleware or routes via res.locals.user.

Example:

const jwt = require('jsonwebtoken');

function authenticateToken(req, res, next) {
  const token = req.header('Authorization')?.split(' ')[1];
  if (!token) return res.status(401).json({ error: 'Access token required' });

  jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
    if (err) return res.status(403).json({ error: 'Invalid token' });

    res.locals.user = decoded; // Store the decoded token in res.locals
    next();
  });
}

Now, in a subsequent middleware or route handler, you can access the user information like this:

app.get('/protected', (req, res) => {
  const user = res.locals.user;
  res.json({ message: `Welcome, ${user.name}` });
});

Key Takeaways:

  • Avoid modifying req directly for custom properties.
  • Use res.locals to pass data between middleware safely and cleanly.
  • Always follow best practices to keep your code readable and maintainable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment