Reputation: 158
So, my current code works this is it
this is the routes
router.get('/user/login', renderLoginForm);
router.post('/user/login', login);
this is the controller
userCtrl.login = passport.authenticate('local',
failureRedirect: '/user/login',
successRedirect: '/user/edit-perfil',
failureFlash: true
);
and heres all the code from the passport.js
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
async (email, password, done) => {
// Match email user
const user = await User.findOne({email})
console.log('ya busque el usuario')
if (!user) {
console.log('estoy en el primer if')
console.log('usuario no encontrado')
return done(null, false, {message: 'Usuario no encontrado'});
} else {
console.log('estoy en el primer else')
// Match password user
const match = await user.matchPassword(password);
if (match){
console.log('estoy en el segundo if')
return done(null, user)
} else {
console.log('estoy el el segundo else')
console.log('contrasena incorrecta')
return done(null, false, {message: 'Contraseña Incorrecta'});
}
}
}))
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done (err, user);
})
})
I cant find a way to try to make a custom redirecction without usign sucessRedirect and other options, couse i want to make that when a new user is registered i want to them to go to a edit-profile page, and if is not a new user just go to the index
i looked at the http://www.passportjs.org/docs/authenticate/ docs but i cant figure it out how to do what i want
Upvotes: 1
Views: 457
Reputation: 5051
you can use with res.redirec
in express like this:
if(true){
res.redirect('/edit-profile')
}
else{
//do somthing
}
check condition manually, do what you want, check this article about redirect in express
Upvotes: 1