I’m new to coding and I’m trying to write one of my first apps. I encountered a problem with my log in function. The user id saves into my session object after user registration but it won’t save into it after a log in. I’m using MongoDB. Here is the code:
Controller
JavaScript
x
44
44
1
exports.register = function(req, res){
2
let user = new User(req.body)
3
user.register().then(()=>{
4
// what do we do if the registration is succesfull - redirecting to the homepage with updated session data
5
req.session.user = {username: user.data.username, avatar: user.avatar, _id: user.data._id}
6
req.session.save(function(){
7
res.redirect('/')})
8
console.log(' After the registration usercontroller line nr 14 ', user.data._id)
9
}).catch((regErrors)=>{
10
regErrors.forEach(function(error){
11
12
req.flash('regErrors', error)
13
})
14
req.session.save(function(){
15
res.redirect('/')
16
})
17
})
18
19
}
20
21
22
exports.login = function(req, res){
23
24
let user = new User(req.body)
25
user.login().then(function(result){
26
//saving data into a cookie
27
req.session.user = {avatar: user.avatar, username: user.data.username, _id: user.data._id}
28
req.session.save(function(){
29
res.redirect('/')
30
console.log('After the log in line 51 userController', req.session)
31
})
32
33
34
}).catch(function(e){
35
req.flash('errors', e)
36
req.session.save(function(){
37
38
res.redirect('/')
39
// After the failed log in
40
console.log('After the failed log in line 61', req.session)
41
})
42
})
43
44
}
Model
JavaScript
1
49
49
1
User.prototype.login = function() {
2
3
return new Promise((resolve, reject) => {
4
this.cleanUp()
5
usersCollection.findOne({
6
username: this.data.username
7
}).then((attemptedUser) => {
8
if (attemptedUser && bcrypt.compareSync(this.data.password, attemptedUser.password)) {
9
this.data.attemptedUser
10
this.getAvatar()
11
resolve("Congrats !")
12
13
} else {
14
reject("Invalid username/password")
15
}
16
}).catch(function() {
17
18
reject("Please try again later.")
19
20
})
21
22
23
})
24
25
26
}
27
28
User.prototype.register = function() {
29
return new Promise(async(resolve, reject) => {
30
//Step #1: Validate user data
31
await this.validate()
32
this.cleanUp()
33
34
//Step #2: Only if there are no validation errors save the user data into a database
35
if (!this.errors.length) {
36
// Hash user password
37
let salt = bcrypt.genSaltSync(10)
38
this.data.password = bcrypt.hashSync(this.data.password, salt)
39
await usersCollection.insertOne(this.data)
40
this.getAvatar()
41
resolve()
42
} else {
43
reject(this.errors)
44
45
}
46
47
48
})
49
}
Screenshot of the console – registration
Screenshot of mongo – registration
Screenshot of the console – log in
I’ve been following Brad Schiffs Java Script Full Stack from Scratch course and everything seems to be working fine for him.
Advertisement
Answer
Your model does nothing with found user: try
JavaScript
1
5
1
if(attemptedUser && bcrypt.compareSync(this.data.password, attemptedUser.password)) {
2
this.data.attemptedUser = attemptedUser
3
this.getAvatar()
4
resolve("Congrats !")
5