const userAuth = asyncHandler( async (req,res)=>{
    const  { email , password } = req.body;

    const user = User.findOne({email});

    if(user && (await user.matchPassword(password))){
        generateToken(res,user._id)
        res.status(201).json({
            _id:user._id,
            name:user.name,
            email:user.email
        })
       }else{
        res.status(401);
        throw new Error('Incorrect Password or Email');
        }
});
{
    "message": "user.matchPassword is not a function",
    "stack": "TypeError: user.matchPassword is not a function\n    at file:///C:/Users/siroh/OneDrive/Desktop/MERN-Auth/server/Controllers/userControllers.js:45:28\n    at asyncUtilWrap (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express-async-handler\\index.js:3:20)\n    at Layer.handle [as handle_request] (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at next (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\route.js:144:13)\n    at Route.dispatch (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\route.js:114:3)\n    at Layer.handle [as handle_request] (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\index.js:284:15\n    at Function.process_params (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\index.js:346:12)\n    at next (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\index.js:280:10)\n    at Function.handle (C:\\Users\\siroh\\OneDrive\\Desktop\\MERN-Auth\\node_modules\\express\\lib\\router\\index.js:175:3)"
}
2

There are 2 best solutions below

0
On

await is missing

const user = await User.findOne({email});
1
On

i assume this code find data in database

const user = User.findOne({email});

and the response should be JSON not function

user.matchPassword(password)

so this code will be error because you find a function in JSON data