Skip to content

Instantly share code, notes, and snippets.

@tjkhara
Last active November 12, 2020 10:13
Show Gist options
  • Select an option

  • Save tjkhara/b6b9c2258236c40f3f5b09efae40f817 to your computer and use it in GitHub Desktop.

Select an option

Save tjkhara/b6b9c2258236c40f3f5b09efae40f817 to your computer and use it in GitHub Desktop.
Letting users login

Login

Part 1

Find out which URL the html form submits to

views -> home-guest.ejs line 17 -> /login With the form submission the browser will send a post request to /login

Then create new route

open router.js

below the router.post

router.post('/login', userController.login)

Create function login in userController

Already has login function

exports.login = function(req, res) {
	let user = new User(req.body)
	user.login()
}

Go to model and create login

After validate add login

User.prototype.login = function() {
	this.cleanUp()
	usersCollection.findOne({username: this.data.username}, (err, attemptedUser) => {
		if(attemptedUser && attemptedUser.password == this.data.password) {
			console.log("congrats")
		} else {
			console.log("Invalid username/password")
		}
	})
}

Test this

Part 2

go to the controller

Focus on this method

exports.login = function(req, res){
   let user = new User(req.body)
   user.login()
}

We don't know how long the login will take

Traditional approach:

exports.login = function(req, res){
   let user = new User(req.body)
   user.login(function(){
   
   }
}

In model:

See the callback argument added in the function:

User.prototype.login = function(callback){
  this.cleanUp()
  usersCollection.findOne({username: this.data.username}, (err, attemptedUser) => {
    if(attemptedUser && attemptedUser.password == this.data.password){
      callback("Congrats!")
    } else {
      callback("Invalid username or password")
    }
  })
}

In controller:

exports.login = function(req, res){
  let user = new User(req.body)
  user.login(function(result){
  	res.send(result)
  })
}

Summary

callback function

This is about connecting the controller and the model. The controller should send back a response to the browser not the model. But, the controller does not know how long the model is going to take.

This is the function in the controller

exports.login = function(req, res){
    let user = new User(req.body)
    user.login(function(result){
    res.send(result)
  })
}

We are calling the user.login function which lives in the model. We give it a function as an argument.

Here is the login function in the model:

User.prototype.login = function(callback){
  this.cleanUp()
  usersCollection.findOne({username: this.data.username}, (err, attemptedUser) => {
    if(attemptedUser && attemptedUser.password == this.data.password){
      callback("congrats")
    } else {
      callback("Invalid username/password")
    }
  })
}

This function does its thing and takes that function as an argument and sends a response via this function.

In the end this function will continue its work once it gets the response:

exports.login = function(req, res){
    let user = new User(req.body)
    user.login(function(result){
    res.send(result)
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment