SlideShare a Scribd company logo
Node JS crash course
Login,Registration and HashPassword | 8:15 pm
Abdul Rahman
Masri Attal
@abed_attal
● Mongoose
● Model & Validations
● Controllers
● Routes
Recap of last week
var mongoose = require('mongoose');
//Define a schema
var Schema = mongoose.Schema;
var SomeModelSchema = new Schema({
a_string: String,
a_date: Date
});
var SomeModel = mongoose.model('SomeModel', SomeModelSchema );
const express = require('express'); //import express
const router = express.Router()
const teaController = require('../controllers/tea');
router.post('/tea', teaController.newTea);
module.exports = router
Express and Routes
// newTea function for post tea route
const newTea = (req, res, next) => {
res.json({message: "POST new tea"}); //dummyfuncti
};
module.exports = {newTea};
route.js
/controllers/tea.js
const express = require ('express');
const routes = require('./routes/tea');
const app = express();
app.use(express.json());
app.use('/', routes); //to use the routes
const listener = app.listen(process.env.PORT || 3000,
() => { console.log('Your app is listening on port ' +
listener.address().port) })
server.js
module.exports = (app) => {
const brands = require('../controllers/brand.controller.js');
// Create a new brand
app.post('/brands', brands.create);
// Retrieve all brands
app.get('/brands', brands.findAll);
Creating CRUD REST
Basic GET & POST
// Retrieve a single brand with brandId
app.get('/brands/:brandId', brands.findOne);
// Update a brand with brandId
app.put('/brands/:brandId', brands.update);
// Delete a brand with brandId
app.delete('/brands/:brandId', brands.delete);
}
Creating CRUD REST
API by ID : GET & UPDATE & DELETE
// Retrieve a single brand with brandId
app.get('/brands/:brandId', brands.findOne);
// Update a brand with brandId
app.put('/brands/:brandId', brands.update);
// Delete a brand with brandId
app.delete('/brands/:brandId', brands.delete);
}
Creating CRUD REST
API by ID : GET & UPDATE & DELETE
The first step of login/register is creating a model defining the schema for
user.
Then, we'll need to register the model with Mongoose so that we can use it
throughout our application.
We should add more validations
Creating the User Schema in Mongoose
var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
}, {timestamps: true});
mongoose.model('User', UserSchema);
● We use POST to send the user registration info
● We should obviously not to save the password as it is in the body.
That’s why we encrypt it
● We might check if the user email already registered.
● We can use “bcrypt” package to do so with function bcrypt.hash()
● We can also use genSalt(10) to add more complexity to the hash
Registration and setting User passwords
app.post('/register, auth.register);
● We also use POST to send the data of login information so it checks in
the database without passing them in the URL
● First we check if the user exist so if not we throw error
● Then we check the hashed version of our password using
bcrypt.compare() so if matched we login.
Login
app.post('/login, auth.login);

More Related Content

PPTX
Node js crash course session 5
PPTX
Node js crash course session 3
PPTX
Mongoose and MongoDB 101
PDF
Mongoose: MongoDB object modelling for Node.js
KEY
Practical Use of MongoDB for Node.js
DOCX
Mongoose getting started-Mongo Db with Node js
PPT
Intro to mongo db
PDF
Nodejs mongoose
Node js crash course session 5
Node js crash course session 3
Mongoose and MongoDB 101
Mongoose: MongoDB object modelling for Node.js
Practical Use of MongoDB for Node.js
Mongoose getting started-Mongo Db with Node js
Intro to mongo db
Nodejs mongoose

What's hot (20)

PDF
Getting started with MongoDB and Scala - Open Source Bridge 2012
PDF
mobile in the cloud with diamonds. improved.
PPTX
Automated Slow Query Analysis: Dex the Index Robot
PPTX
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
PPT
Connecting to a REST API in iOS
PPT
Web Services with Objective-C
PPTX
Log MongoDB slow query
PDF
Scala with mongodb
PPTX
Rapid prototyping using azure functions - A walk on the wild side
PPT
Ajax Introduction
PPTX
Using Webservice in iOS
PPTX
Mule: JSON to Object
PPTX
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
ODP
CONFidence 2014: Kiss, Zagon, Sseller: Scaling security
PPTX
Sekilas PHP + mongoDB
PDF
iOS: Web Services and XML parsing
PPT
PPT
Ajax with DWR
PPTX
JSON Processing and mule
PPTX
Mule json transformers
Getting started with MongoDB and Scala - Open Source Bridge 2012
mobile in the cloud with diamonds. improved.
Automated Slow Query Analysis: Dex the Index Robot
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Connecting to a REST API in iOS
Web Services with Objective-C
Log MongoDB slow query
Scala with mongodb
Rapid prototyping using azure functions - A walk on the wild side
Ajax Introduction
Using Webservice in iOS
Mule: JSON to Object
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
CONFidence 2014: Kiss, Zagon, Sseller: Scaling security
Sekilas PHP + mongoDB
iOS: Web Services and XML parsing
Ajax with DWR
JSON Processing and mule
Mule json transformers
Ad

Similar to Node js crash course session 6 (20)

PPTX
Spring MVC 3 Restful
PDF
Bonnes pratiques de développement avec Node js
PDF
SproutCore and the Future of Web Apps
PPTX
Angular 1 + es6
PDF
[W3C HTML5 2016] Angular + ES6
PDF
2016 W3C Conference #4 : ANGULAR + ES6
PDF
Testing in JavaScript
PPT
RESTful API In Node Js using Express
PDF
Leveraging Playwright for API Testing.pdf
PDF
express of full stack web development notes
PPT
ASP.NET MVC introduction
PDF
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
PPTX
Backend Technologies Notes asnfasdngndasdf
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
PPTX
Designing CakePHP plugins for consuming APIs
PPTX
Sql storeprocedure
PPTX
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
PPT
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
PPT
Create a web-app with Cgi Appplication
PPTX
AngularJS for Java Developers
Spring MVC 3 Restful
Bonnes pratiques de développement avec Node js
SproutCore and the Future of Web Apps
Angular 1 + es6
[W3C HTML5 2016] Angular + ES6
2016 W3C Conference #4 : ANGULAR + ES6
Testing in JavaScript
RESTful API In Node Js using Express
Leveraging Playwright for API Testing.pdf
express of full stack web development notes
ASP.NET MVC introduction
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Backend Technologies Notes asnfasdngndasdf
Intro To JavaScript Unit Testing - Ran Mizrahi
Designing CakePHP plugins for consuming APIs
Sql storeprocedure
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
Create a web-app with Cgi Appplication
AngularJS for Java Developers
Ad

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
System and Network Administraation Chapter 3
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Introduction to Artificial Intelligence
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
medical staffing services at VALiNTRY
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
CHAPTER 2 - PM Management and IT Context
How to Choose the Right IT Partner for Your Business in Malaysia
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Designing Intelligence for the Shop Floor.pdf
Why Generative AI is the Future of Content, Code & Creativity?
System and Network Administraation Chapter 3
Digital Systems & Binary Numbers (comprehensive )
Softaken Excel to vCard Converter Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Introduction to Artificial Intelligence
Understanding Forklifts - TECH EHS Solution
Design an Analysis of Algorithms I-SECS-1021-03
Wondershare Filmora 15 Crack With Activation Key [2025
wealthsignaloriginal-com-DS-text-... (1).pdf
iTop VPN Free 5.6.0.5262 Crack latest version 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
medical staffing services at VALiNTRY
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
CHAPTER 2 - PM Management and IT Context

Node js crash course session 6

  • 1. Node JS crash course Login,Registration and HashPassword | 8:15 pm Abdul Rahman Masri Attal @abed_attal
  • 2. ● Mongoose ● Model & Validations ● Controllers ● Routes Recap of last week var mongoose = require('mongoose'); //Define a schema var Schema = mongoose.Schema; var SomeModelSchema = new Schema({ a_string: String, a_date: Date }); var SomeModel = mongoose.model('SomeModel', SomeModelSchema );
  • 3. const express = require('express'); //import express const router = express.Router() const teaController = require('../controllers/tea'); router.post('/tea', teaController.newTea); module.exports = router Express and Routes // newTea function for post tea route const newTea = (req, res, next) => { res.json({message: "POST new tea"}); //dummyfuncti }; module.exports = {newTea}; route.js /controllers/tea.js const express = require ('express'); const routes = require('./routes/tea'); const app = express(); app.use(express.json()); app.use('/', routes); //to use the routes const listener = app.listen(process.env.PORT || 3000, () => { console.log('Your app is listening on port ' + listener.address().port) }) server.js
  • 4. module.exports = (app) => { const brands = require('../controllers/brand.controller.js'); // Create a new brand app.post('/brands', brands.create); // Retrieve all brands app.get('/brands', brands.findAll); Creating CRUD REST Basic GET & POST
  • 5. // Retrieve a single brand with brandId app.get('/brands/:brandId', brands.findOne); // Update a brand with brandId app.put('/brands/:brandId', brands.update); // Delete a brand with brandId app.delete('/brands/:brandId', brands.delete); } Creating CRUD REST API by ID : GET & UPDATE & DELETE
  • 6. // Retrieve a single brand with brandId app.get('/brands/:brandId', brands.findOne); // Update a brand with brandId app.put('/brands/:brandId', brands.update); // Delete a brand with brandId app.delete('/brands/:brandId', brands.delete); } Creating CRUD REST API by ID : GET & UPDATE & DELETE
  • 7. The first step of login/register is creating a model defining the schema for user. Then, we'll need to register the model with Mongoose so that we can use it throughout our application. We should add more validations Creating the User Schema in Mongoose var mongoose = require('mongoose'); var UserSchema = new mongoose.Schema({ name: String, email: String, password: String, }, {timestamps: true}); mongoose.model('User', UserSchema);
  • 8. ● We use POST to send the user registration info ● We should obviously not to save the password as it is in the body. That’s why we encrypt it ● We might check if the user email already registered. ● We can use “bcrypt” package to do so with function bcrypt.hash() ● We can also use genSalt(10) to add more complexity to the hash Registration and setting User passwords app.post('/register, auth.register);
  • 9. ● We also use POST to send the data of login information so it checks in the database without passing them in the URL ● First we check if the user exist so if not we throw error ● Then we check the hashed version of our password using bcrypt.compare() so if matched we login. Login app.post('/login, auth.login);