Answer the question
In order to leave comments, you need to log in
How to select information from a session user in mongodb?
When sending the form for authorization, I check the user data, if successful, I write it to the session.
Next, I redirect to the user's page with his info.
As I understand it, it is necessary to write a session user to the store, and in the route, when getting to the user's page, you need to somehow select this particular user and there already in the database look for him and extract
Passport information, I do not use tokens.
The code is given, where I send data from the form during authorization
router.post('/login', (req, res) => {
let { firstName, password } = req.body
User.findOne({ firstName: firstName }, 'firstName userEmail password', (err, userData) => {
if(!err) {
let passwordCheck = bcrypt.compareSync(password, userData.password)
if ( passwordCheck ) {
req.session.user = {
firstName: userData.firstName,
userEmail: userData.userEmail,
_id: userData._id
}
req.session.user.expires = new Date(
Date.now() + 3 * 24 * 3600 * 1000
)
console.log('You are logged in', req.session.user)
// res.status(200).send({ user: req.session.user })
res.redirect('/login')
} else {
console.log('invaild login')
res.send(401).send('incorrect password')
}
} else {
console.log('invaild login')
res.status(401).send('invaild login')
}
})
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question