Вопрос

UPDATE

I have moved the code from the inside of the passport local-signup to a separate handler and it operates fine. The issue lies with Passport and the usage of the local-signup however I do not know why.


I have a setup with Node.js (Express) + Passport for authentication and registration. This is a standard procedure have used it before however it does not seem to work. The passport registration goes immediately to the failureRedirect. The main issue is that the code doesnt hit any of the console.log() statements and thus I assume that it doesnt even process the first request and automatically fails. Any idea why or how to debugg?

cmd log:

POST /register 302 4ms - 58b
GET / 304 2ms
GET /css/bootstrap.css 304 1ms
GET /css/main.css 304 1ms
GET /javascript/chart.js 304 2ms
GET /javascript/bootstrap.js 304 2ms
GET /javascript/index.js 304 2ms

index.html form:

 <form class="form-register" name="register-form" method="post" action="/register" data-name="register Form" enctype="application/x-www-form-urlencoded">
     <div class="ARegistration" style="display:none;">
         <input type="email" id="txtOnceEmail" class="form-control" style="width:300px; display:block;" placeholder="One use Email Address" name="AEmail">
           <br />

         <input type="text" id="txtCodeName" class="form-control" style="width: 200px; display:block;" placeholder="Code Name" name="CodeName">
           <br />

         <input type="number" id="txtSecretCodeLength" value="10" class="form-control" style="width: 200px; display:block;" placeholder="Secret Code Length" name="SecretCodeLength">
           <br />

         <textarea id="txtaXInfo" class="form-control" placeholder="Any info" name="aXInfo"></textarea>

         <button class="btn btn-info btn-xs">Register</button>
      </div>
  </form>

Route:

    app.post('/register', passport.authenticate('local-signup', {
        successRedirect : '/', // redirect to the secure profile section
        failureRedirect : '/', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

passport local sign up:

// =========================================================================
// LOCAL SIGNUP ============================================================
// =========================================================================
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 in the req from our route (lets us check if a user is logged in or not)
},
function (req, email, password, done) {
    // asynchronous
    process.nextTick(function () {
        // check if the user is already logged in
        if (!req.user) {
            console.log("step1");
            User.findOne({ $or: [{ 'valid.email': email }, { 'emailList': 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) {
                    console.log("step2");
                    return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
                } else {

                        console.log("step5");
                        newUser.anonym.CodeName = req.body.CodeName;
                        newUser.anonym.extraInfo = req.body.aXInfo;
                        newUser.emailList.push(req.body.AEmail);

                        //Generate random string
                        var rString = null;
                        var goOn = false;

                        while (!goOn)
                        {
                            console.log("step6");
                            rString = randomString(req.body.SecretCodeLength, charSet);
                            User.findOne({ 'emailList': rString }, function (err, user) {
                                // if there are any errors, return the error
                                if (err){
                                    console.log(err);
                                    return done(err);}

                                // check to see if theres already a user with that email
                                if (!user) {
                                    goOn = true;
                                }
                            });
                        }
                        console.log("step7");
                        newUser.anonym.secretCode = rString;
                        rString = null;

                        newUser.save(function (err) {
                            if (err)
                                throw err;
                            return done(null, newUser);
                        });
                    }
                // }
            });
        } else {
            console.log('test5');
        }
    });

}));

Tested it also using angular.js to execute the post but the problem still remains. It is definitely something with passport.

Это было полезно?

Решение

The issue was here:

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 in the req from our route (lets us check if a user is logged in or not)

},

If email or password are not provided passport just automatically fails. I guess some documentation of that would have been useful!.

Другие советы

If you don't identify the usernameField in the new LocalStrategy, it will default to saving the username in username, as explained here.

This strategy takes an optional options hash before the function, e.g. new LocalStrategy({/* options */, callback}).

The available options are:

  • usernameField - Optional, defaults to username
  • passwordField - Optional, defaults to password

Both fields define the name of the properties in the POST body that are sent to the server.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top