Initialize a Backend server and database for an API Restful
Create a new user in the database
Two possiblities to create a new user inside the database
constUser=require('../models/users.model');constbcrypt=require("bcrypt");constjwt=require('jsonwebtoken');exports.signUp=async(req,res)=>{if(!req.body.email|!req.body.password){returnres.status(400).send({message: "Must have email and password"});}try{bcrypt.hash(req.body.password,10).then(hash=>{constuser=newUser({email: req.body.email,password: hash});});user.save().then(()=>res.status(201).json({message: 'User has been created'}))}catch(err){returnerr=res.status(500).send({message: err.message});}};
bcrypt.has(req.body.password,10).then(hash=>{constuser=newUser({email: req.body.email,password: hash});user.save().then(()=>res.status(201).json({message: "User saved in the database"})).catch(error=>res.status(400).json({ error }));}).catch(error=>res.status(500).json({ error }));
Two possibilities to log in
async(req,res)=>{User.findOne({email: req.body.email}).then(user=>{if(!user){returnres.status(401).json({message: "email/password are incorrect"});}bcrypt.compare(req.body.password,user.password).then(valid=>{if(!valid){returnres.status(401).json({message: "email/password are incorrect"});}res.status(200).json({userID: user._id,token: "TOKEN"});}).catch(error=>res.status(500).json({ error }));}).catch(error=>res.status(500).json({ error }));};