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);