Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
bcryptjs no funciona dentro de un método de esquema de mangosta

Versión de bcryptjs: "bcryptjs": "^2.4.3"

versión mangosta: "mongoose": "^6.0.12"

Estoy tratando de cifrar la contraseña del usuario en la creación del usuario o la actualización de la contraseña. Los datos se insertan correctamente pero la contraseña no está cifrada. Este es mi esquema (lo copié de mi proyecto anterior que funcionaba correctamente):

 import mongoose from 'mongoose' import bcrypt from 'bcryptjs' const userSchema = mongoose.Schema( { username: { type: String, required: true, unique: true }, password: { type: String, required: true, unique: true, }, votedFor: [ { type: mongoose.Schema.ObjectId, ref: 'Election' } ], finishedVoting: { type: Boolean, required: true, default: false }, isAdmin: { type: Boolean, required: true, default: false, }, }, { timestamps: true, } ) userSchema.pre('save', async function(next) { // Only run this function if password was actually modified if (!this.isModified('password')) return next(); // Hash the password with salt 10 this.password = await bcrypt.hash(this.password, 10); next(); });

En la base de datos, veo la contraseña como texto sin formato (sin cifrar). Así que intenté usar la función de devolución de llamada en lugar de async/await como se sugiere en otras respuestas:

 userSchema.pre('save', function(next) { var user = this; console.log("encryption") // only hash the password if it has been modified (or is new) if (!user.isModified('password')) return next(); console.log("started encrypting") // generate a salt bcrypt.genSalt(10, function(err, salt) { if (err) return next(err); // hash the password using our new salt bcrypt.hash(user.password, salt, function(err, hash) { if (err) return next(err); // override the cleartext password with the hashed one user.password = hash; next(); }); }); });

No ayudó. Eliminé la colección y volví a insertar los datos. Una vez más, la contraseña permanece sin cifrar. No vi console.log("encryption") ni console.log("started encrypting") durante la inserción. Estos son algunos datos de usuarios que estoy insertando:

 const voters = [ { username: "194199", password: "FA654644", //will be encrypted isAdmin: false, finishedVoting: false // votedFor: [Object], // }, { username: "184183", password: "MB454644", //will be encrypted isAdmin: false, finishedVoting: false // votedFor: [Object], // }, { username: "194324", password: "FA651684", //will be encrypted isAdmin: false, finishedVoting: false // votedFor: [Object], // } ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

tengo el mismo problema pero con bcrypt-nodejs.

😐

about 4 years ago · Kikiriya Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!