Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - Using a condition in passport.authenticate successRedirect

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

question from:https://stackoverflow.com/questions/65850762/using-a-condition-in-passport-authenticate-successredirect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...