Property 'id' does not exist on type 'string | JwtPayload' [duplicate]
Emmanuel Kelechi Igwesi
Posted on January 19, 2023
I just want to get user by id but I got an error that states id does not exist on type string , my code is shown below:
import asyncHandler from "express-async-handler";
import { NextFunction, Request, Response } from "express";
import User from "../models/User";
import jwt from "jsonwebtoken";
import configs from "../config/config";
const protect = asyncHandler (async (req: Request, res: Response, next: NextFunction) => {
try {
const token = req.cookies.token
if (!token) {
res.status(401)
throw new Error("Not authorized, please login");
}
// Verify Token
const verified = jwt.verify(token, configs.JWT_SECRET)
// Get user id from token
const user = await User.findById(verified.id).select("-password")
if (!user) {
res.status(401)
throw new Error("User was not found")
}
req.user = user
next()
} catch (error) {
}
});
export default protect;
💖 💪 🙅 🚩
Emmanuel Kelechi Igwesi
Posted on January 19, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.