How we secure our password in express and mongoDB

many developers think how we secure our password through malicious user they try to access data and destroy their server.
In express we discuss a library named is “bcrypt” they hashed our data and this hashed data does not decrypt any user this is best…


This content originally appeared on DEV Community and was authored by Deepak

many developers think how we secure our password through malicious user they try to access data and destroy their server.
In express we discuss a library named is "bcrypt" they hashed our data and this hashed data does not decrypt any user this is best feature of this library.
Install in your system

npm i express mongoose bcrypt

userSchema.js

const {Schema,model}=mongoose
const userSchema=new Schema({
usename:String,
password:String
)}
const User=model('user',userSchema)
module.exports=User

send data through this api end point

index.js

router.post('/api/register',acync (req,res)=>{
    const {username,password}=req.body
                    const oldUser=await User.findOne({username})
    (oldUser) return res.status(400).send("User already registered")
    const salt=await bcrypt.getSalt(10)
    const hashPassword=await bcrypt.hash(password,salt);
                    const user=new User({username,password:hashPassword})
                    const result=await user.save()
    res.status(200).send(result);
             });

above example is register it and saved their data

Image description


router.post('/api/login',acync (req,res)=>{
    const {username,password}=req.body
    const user=await User.findOne({username})
    (!user) return res.status(404).send("User Not Found")
    const hashPassword=await bcrypt.compare(password,user.password);
                    if(user && hashPassword)
    return res.send({username,password:hashPassword});
    else
    return res.status(400).send("password is wrong")
             });


above code is login user with athenticated.


This content originally appeared on DEV Community and was authored by Deepak


Print Share Comment Cite Upload Translate Updates
APA

Deepak | Sciencx (2021-12-20T17:12:40+00:00) How we secure our password in express and mongoDB. Retrieved from https://www.scien.cx/2021/12/20/how-we-secure-our-password-in-express-and-mongodb/

MLA
" » How we secure our password in express and mongoDB." Deepak | Sciencx - Monday December 20, 2021, https://www.scien.cx/2021/12/20/how-we-secure-our-password-in-express-and-mongodb/
HARVARD
Deepak | Sciencx Monday December 20, 2021 » How we secure our password in express and mongoDB., viewed ,<https://www.scien.cx/2021/12/20/how-we-secure-our-password-in-express-and-mongodb/>
VANCOUVER
Deepak | Sciencx - » How we secure our password in express and mongoDB. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/20/how-we-secure-our-password-in-express-and-mongodb/
CHICAGO
" » How we secure our password in express and mongoDB." Deepak | Sciencx - Accessed . https://www.scien.cx/2021/12/20/how-we-secure-our-password-in-express-and-mongodb/
IEEE
" » How we secure our password in express and mongoDB." Deepak | Sciencx [Online]. Available: https://www.scien.cx/2021/12/20/how-we-secure-our-password-in-express-and-mongodb/. [Accessed: ]
rf:citation
» How we secure our password in express and mongoDB | Deepak | Sciencx | https://www.scien.cx/2021/12/20/how-we-secure-our-password-in-express-and-mongodb/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.