2017-10-08 6 views
0

Ich habe versucht, Authentifizierung mit E-Mail und Passwort einzurichten. Hier ist der Teil-Code in signup.ejs:Pass-lokale Strategie arbeitet im Postboten aber nicht im Browser

<% if (message.length > 0) { %> 
     <div class="alert alert-danger"><%= message %></div> 
    <% } %> 

    <!-- LOGIN FORM --> 
    <form action="/signup" method="post"> 
     <div class="form-group"> 
      <label>Email</label> 
      <input type="text" class="form-control" name="email"> 
     </div> 
     <div class="form-group"> 
      <label>Password</label> 
      <input type="password" class="form-control" name="password"> 
     </div> 

     <button type="submit" class="btn btn-warning btn-lg">Signup</button> 
    </form> 

Die Form Beiträge zu /signup und das ist meine ausdrückliche Route:

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

Und das ist die lokale Pass Strategie Ich verwende:

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 back the entire request to the callback 
      }, 
      function(req, email, password, done) { 
       // asynchronous 
       // User.findOne wont fire unless data is sent back 
       process.nextTick(function() { 
        // find a user whose email is the same as the forms email 
        // we are checking to see if the user trying to login already exists 
        User.findOne({ 'local.email': 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) { 
          return done(
           null, 
           false, 
           req.flash('signupMessage', 'That email is already taken.') 
          ) 
         } else { 
          // if there is no user with that email 
          // create the user 
          var newUser = new User() 

          // set the user's local credentials 
          newUser.local.email = email 
          newUser.local.password = newUser.generateHash(password) 

          // save the user 
          newUser.save(function(err) { 
           if (err) throw err 
           return done(null, newUser) 
          }) 
         } 
        }) 
       }) 
      } 
     ) 
    ) 

Hier ist ein Link zu meinem Github repo für den vollständigen Code.

Das Problem, das ich habe, ist, dass, wenn ich eine Post-Anfrage mit Postman mit E-Mail und Passwort in der Anfrage Körper mache, es gut geht und ich auf die Profilroute erfolgreich umgeleitet werden. Wenn ich jedoch versuche, mich anzumelden, indem ich das Formular auf meiner Seite ausfülle, werde ich zurück zur Route "/ Anmeldung" geleitet. Kann jemand bitte mit diesem Problem helfen?

+0

Generische Tipps: Überprüfen Sie, ob der Bestätigungshandler für die Strategie aufgerufen wird. Überprüfen Sie, ob 'email' und' password' die erwarteten Werte enthalten. Überprüfen Sie, ob Fehler auftreten. – robertklep

Antwort

0

Ich habe die Antwort gefunden. Der Grund dafür ist, dass das Formular die E-Mail- und Passwortwerte nicht an req.body weitergab. Ich änderte app.use(bodyParser.json()) zu app.use(bodyParser.urlencoded({ extended: true })) und es begann zu arbeiten.

Verwandte Themen