-1
Cannot read property 'username' of null
Keeps giving this error I don’t know why, he can create a new user but can’t login, someone knows what is happening?
app.post('/users', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10)
const user = { username: req.body.username, password: hashedPassword }
await userModel.create(user)
res.status(201).send()
} catch {
res.status(500).send()
}
})
app.post('/users/login', async (req, res) => {
const user = await userModel.find(user => user.username === req.body.username)
if (user == null) {
return res.status(400).send('Cannot find user')
}
try {
if (await bcrypt.compare(req.body.password, user.password)) {
res.send('Success')
} else {
res.send('Not Allowed')
}
} catch {
res.status(500).send()
}
})