Passport-local / I don’t understand the structure of the function

Asked

Viewed 70 times

0

I am trying to implement the authentication system on my platform using Passport, Passport-local and Passport-local-Mongoose.

I was able to apply login authentication:

app.post("/login", passport.authenticate("local", {
    successRedirect: "/secret",
    failureRedirect: "/login"
}) ,function(req, res){
});

But I cannot understand the authentication structure when registering a new user:

app.post("/register", function(req, res) {
    User.register(new User({username: req.body.username}), req.body.password, function(erro, user) {
        if(erro) {
            console.log(erro);
            return res.render("register");
        } 
        passport.authenticate('local')(req, res, function () {
            res.redirect("/secret");
        });        

    });
});

Because the structure that will redirect after user authentication has this structure: passport.authenticate("local")(req, res, function(){}

I cannot understand the role of these next parentheses. What role do they play?

2 answers

0

This second parenthesis functions as a callback of the Passport authentication function. Passport will do the validations and then use what is being passed in the second parentheses to continue

-1

app.post("/register", function(req, res) {
User.register(new User({username: req.body.username}), req.body.password, //aqui é requerido o username e o password 
 function(erro, user) {
    if(erro) {
        console.log(erro);
        return res.render("register"); //se tiver erro, retorna para a página register
    } 
    passport.authenticate('local')(req, res, function () { 
  //vai fazer a autenticação com os username e o password digitado pelo usuário
        res.redirect("/secret"); 
  //em caso de sucesso, é redirecionado para a página secrets
    });        

});
});

The documentation is here: http://www.passportjs.org/docs/authenticate/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.