2017-10-08 9 views
0

電子メールとパスワードで認証を設定しようとしています。ここでsignup.ejsで部分コードです:/signup郵便配達員ではなくブラウザではなく、パスポートのローカル戦略

<% 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> 

フォームのポストは、これは私の急行路線です:

// 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 
    }) 
) 

そして、これは私が使用しているローカルパスポート戦略です:

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) 
          }) 
         } 
        }) 
       }) 
      } 
     ) 
    ) 

完全なコードについてはGithub repoへのリンクがあります。

私が抱えている問題は、リクエスト本体でPostmanにメールとパスワードを使って投稿要求を行っているときにうまくいくとわかりました。プロフィールルートに正常にリダイレクトされました。しかし、私のページでフォームに記入してログインしようとすると、私は '/ signup'ルートにリダイレクトされます。誰かがこの問題を助けてくれますか?

+0

一般的なヒント:戦略の検証ハンドラが呼び出されているかどうかを確認してください。 'email'と' password'に期待値が含まれているかどうか確認してください。エラーが発生していないか確認してください。 – robertklep

答えて

0

私は答えを見つけました。理由は、フォームが電子メールとパスワードの値をreq.bodyに渡していないためです。私はapp.use(bodyParser.json())app.use(bodyParser.urlencoded({ extended: true }))に変更し、動作を開始しました。

関連する問題