<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: FaresHamel</title>
    <description>The latest articles on DEV Community by FaresHamel (@fareshamel).</description>
    <link>https://dev.to/fareshamel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F571038%2F33d35dd9-a38f-495f-8bcd-385f091d615c.jpeg</url>
      <title>DEV Community: FaresHamel</title>
      <link>https://dev.to/fareshamel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fareshamel"/>
    <language>en</language>
    <item>
      <title>Authentication in NodeJS With Express and Mongo use Mongoose and #1</title>
      <dc:creator>FaresHamel</dc:creator>
      <pubDate>Mon, 01 Feb 2021 02:04:31 +0000</pubDate>
      <link>https://dev.to/fareshamel/authentication-in-nodejs-with-express-and-mongo-use-mongoose-and-1-3okm</link>
      <guid>https://dev.to/fareshamel/authentication-in-nodejs-with-express-and-mongo-use-mongoose-and-1-3okm</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Packages Required&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You will be needing these following 'npm' packages.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;express&lt;br&gt;
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;express-validator&lt;br&gt;
To Validate the body data on the server in the express framework, we will be using this library. It's a server-side data validation library. So, even if a malicious user bypasses the client-side verification, the server-side data validation will catch it and throw an error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;body-parser&lt;br&gt;
It is nodejs middleware for parsing the body data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;bcryptjs&lt;br&gt;
This library will be used to hash the password and then store it to database.This way even app administrators can't access the account of a user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;mongoose&lt;br&gt;
Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initiate Project&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will start by creating a node project. So, Create a new folder with the name 'node-auth' and follow the steps below. All the project files should be inside the 'node-auth' folder.&lt;/p&gt;

&lt;p&gt;router.js&lt;/p&gt;

&lt;p&gt;router.post('/singUp', [&lt;br&gt;
    check('username', 'Please Enter a Valid Username').not().isEmpty(),&lt;br&gt;
    check('firstname', 'Please Enter a Valid firstname').not().isEmpty(),&lt;br&gt;
    check('lastname', 'Please Enter a Valid lastname').not().isEmpty(),&lt;br&gt;
    check('city', 'Please Enter a Valid City').not().isEmpty(),&lt;br&gt;
    check('ville', 'Please Enter a Valid Ville').not().isEmpty(),&lt;br&gt;
    check('numberphone', 'Please Enter a Valid numberPhone').not().isEmpty().isNumeric(),&lt;br&gt;
    check('email', 'please Enter a valid Email Address').isEmail(),&lt;br&gt;
    check('password', 'please your password is short try again').isLength({ min: 8 }),&lt;br&gt;
], authClient.singUpClient);&lt;/p&gt;

&lt;p&gt;const Client = require('../models/clientCls');&lt;br&gt;
const mongose = require('mongoose');&lt;br&gt;
const { validationResult } = require('express-validator');&lt;br&gt;
const bcrypt = require("bcryptjs");&lt;br&gt;
const { error } = require('console');&lt;br&gt;
//Sing up controller with valitaor&lt;br&gt;
module.exports.singUpClient = async(req, res) =&amp;gt; {&lt;br&gt;
    const errors = validationResult(req);&lt;br&gt;
    if (!errors.isEmpty()) {&lt;br&gt;
        return res.status(400).json({&lt;br&gt;
            errors: errors.array()&lt;br&gt;
        });&lt;br&gt;
    }&lt;br&gt;
    const salt = await bcrypt.genSalt(10);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = new Client({

    _id: new mongose.Types.ObjectId(),

    username: req.body.username,

    firstname: req.body.firstname,

    lastname: req.body.lastname,

    email: req.body.email,

    password: req.body.password,

    numberPhon: req.body.numberphone,

    city: req.body.city,

    ville: req.body.ville,

    dateInscription: new Date(),

    passwrodEncrypt: await bcrypt.hash(req.body.password, salt)
});

try {
    const emailuser = user.email;

    const resultResearch = await Client.findOne({ email: emailuser });

    if (resultResearch) {

        return res.json({ insertion: false });;
    }


    await user.save().then(result =&amp;gt; {

        return res.json({ newname: result.id });

    }).catch(error =&amp;gt; {

        return res.send({ newname: error });
    });

} catch (erro) {

    res.json({ message: error });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

</description>
      <category>node</category>
      <category>mongodb</category>
      <category>express</category>
      <category>mongoose</category>
    </item>
  </channel>
</rss>
