電子メールとパスワードで認証を設定しようとしています。ここで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'ルートにリダイレクトされます。誰かがこの問題を助けてくれますか?
一般的なヒント:戦略の検証ハンドラが呼び出されているかどうかを確認してください。 'email'と' password'に期待値が含まれているかどうか確認してください。エラーが発生していないか確認してください。 – robertklep