feat: use User for login

This commit is contained in:
karishmas6
2024-09-24 17:31:56 +05:30
parent 21a1ea3a5c
commit f26f8300d3

View File

@@ -37,18 +37,14 @@ router.post('/login', async (req, res) => {
if (!email || !password) return res.status(400).send('Email and password are required') if (!email || !password) return res.status(400).send('Email and password are required')
if (password.length < 6) return res.status(400).send('Password must be at least 6 characters') if (password.length < 6) return res.status(400).send('Password must be at least 6 characters')
let user = await User.findOne({ email }).exec() let user = await User.findOne({ where: { email } });
const match = await comparePassword(password, user.password) const match = await user?.isValidPassword(password);
if (!match) return res.status(400).send('Invalid email or password') if (!match) return res.status(400).send('Invalid email or password')
// create signed jwt const token = jwt.sign({ id: user?.id }, process.env.JWT_SECRET as string, { expiresIn: '1h' });
const token = jwt.sign({
_id: user._id
}, process.env.JWT_SECRET as string, {
expiresIn: '3d'
})
// return user and token to client, exclude hashed password // return user and token to client, exclude hashed password
user.password = undefined // user.password = undefined
res.cookie('token', token, { res.cookie('token', token, {
httpOnly: true httpOnly: true
}) })