Data Encryption in Express Mongo DB App

For the encryption and authentication for mongoose documents, we can use a package called mongoose-encryption.

Refer: https://www.npmjs.com/package/mongoose-encryption

Install mongoose-encryption package

npm i mongoose-encryption

Using mongoose-encrytption

const encrypt = require("mongoose-encryption");

const userSchema =  new mongoose.Schema{
  email: String,
  password: String
};

const secret = "Thisisourlittlesecret";
userSchema.plugin(encrypt, {secret: secret, encryptedFields: ['password']});

const User = new mongoose.model("User", userSchema);

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *