Sunday, 3 December 2017

Implement my own schema with passport.js?

I am using passport.js to use an authentication system, specifically I am using the local strategy, I would like to be able to change the schema that it uses for one that I need and has different fields. The schema that I currently have is this:var userSchema = mongoose.Schema({ local : { email : String, password : String } }); I want to change it for this:var userSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true, lowercase: true, }, name: { type: String, required: true, max: [50, "Error, muchos caracteres"] }, lastName: { type: String, required: true, max: [50, "Error, muchos caracteres"] }, password: { type: String, select: false, required: true, max: [50, "Error, muchos caracteres"] }, active: { type: Boolean, select: false, default: true }, registerDate: { type: Date, default: Date.now() }, registerForm: Number, birthDate: { type: Date, required: true } }); I tried changing the schemas properly from the model folder, and editing my user schema, but it does not work. Apparently I must edit the parameters that are passed to the signup function, which is the following: passport.use('local-signup', new LocalStrategy({ // by default, local strategy uses username and password, we will override with email usernameField : 'email', passwordField : 'password', passReqToCallback : true // allows us to pass back the entire request to the callback }, function(req, email, password, done) { // asynchronous // User.findOne wont fire unless data is sent back process.nextTick(function() { // find a user whose email is the same as the forms email // we are checking to see if the user trying to login already exists User.findOne({ 'local.email' : email }, function(err, user) { // if there are any errors, return the error if (err) return done(err); // check to see if theres already a user with that email if (user) { return done(null, false, req.flash('signupMessage', 'That email is already taken.')); } else { // if there is no user with that email // create the user var newUser = new User(); // set the user's local credentials newUser.local.email = email; newUser.local.password = newUser.generateHash(password); // save the user newUser.save(function(err) { if (err) throw err; return done(null, newUser); }); } }); }); })); }; Could someone guide me about it?

Submitted December 03, 2017 at 08:01PM by MagoMerlin

No comments:

Post a Comment