Friday, 24 July 2020

Passportjs signup works in localhost but doesn't work after deploying in Heroku.

I'm using nodemailer to send a random code to the user and then redirecting them to a verify page where they paste the code to verify their account. I'm using mongo Altas.When I'm running it on localhost it works perfectly but when I deploy to my free version of Heroku the signup page redirects me to the same page as instructed to do for any errors.​Express-session middlewareapp.use(session({ secret: '9131848995', resave: false, saveUninitialized: false, store: new MongoStore({mongooseConnection: mongoose.connection}), cookie: { maxAge: 8 * 180 * 60 * 1000, } })); ​config/passport.jspassport.use('local.signup', new localStrategy({ usernameField: 'email', passwordField: 'password', passReqToCallback: true }, (req, email, password, done) => { req.checkBody('email', 'Invalid email').notEmpty().isEmail(); req.checkBody('password', 'Invalid password').notEmpty().isLength({min:6}); let errors = req.validationErrors(); if(errors){ let messages = []; errors.forEach((error)=>{ messages.push(error.msg); }); return done(null, false, req.flash('error', messages)) } User.findOne({'email': email}, (err, user)=> { if (err) { return done(err); } if (user) { return done(null, false, {message: 'Email is already in use.'}); } transporter.sendMail(Mail(email), (error, info)=>{ if(!error){ let newUser = new User(); newUser.email = email; newUser.password = newUser.encryptPassword(password); newUser.v_code = v_code; if (!err) { newUser.save((err, result)=>{ if (err) { return done(err); } return done(null, newUser); }); } } else { return done(null, false, {message: error}); } }); }) })); Signup routerouter.get('/signup', (req, res)=> { let messages = req.flash('error'); res.render('signup', {csrfToken: req.csrfToken(), messages: messages, hasError: messages.length > 0}); }); router.post('/signup', passport.authenticate('local.signup',{ successRedirect: '/user/verify', failureRedirect: '/user/signup', failureFlash: true })); ​Thank you <3.

Submitted July 24, 2020 at 10:25AM by sublimeDawn

No comments:

Post a Comment