2016-11-16 8 views
0

passport-localを使用して、認証とパスワードのハッシングを小さなWebアプリケーションに焼き付けようとしています。私はそれが正しく設定されていると信じていますが、資格情報が110%であっても、失敗して再度ログインするように指示されています。Passport-localが正しい資格情報で決して認証されない

パスポートのドキュメントは、デバッグエラーが非常に軽く、この障害のリダイレクトの原因を誰かが共有できるか、デバッグを続行するソリューションを提供できますか?

パスポート構成

passport.use(new LocalStrategy({ 
    usernameField: 'email', 
    passwordField: 'password' 
}, 
    function(username, password, cb) { 
    UserSchema.findByUsername(username, function(err, user) { 
     if (err) { return cb(err); } 
     if (!user) { return cb(null, false); } 
     if (user.password != password) { return cb(null, false); } 
     return cb(null, user); 
    }); 
    })); 

ログインルート

router.post('/login', 
    passport.authenticate('local', { 
    failureRedirect: '/login' }), 
    function(req, res) { 
    res.redirect('/'); 
    }); 

スキーマおよびfindByUsername機能

const UserSchema = mongoose.Schema ({ 
    firstName: { 
    type: String, 
    index: true 
    }, 
    lastName: { 
    type: String, 
    index: true 
    }, 
    email: { 
    type: String, 
    index: true 
    }, 
    password: { 
    type: String, 
    index: true 
    }, 
    homeAirport: { 
    type: String, 
    index: true 
    } 
}) 

UserSchema.plugin(passportLocalMongoose) 

// Adding this function to find by username 
exports.findByUsername = function(username, cb) { 
    process.nextTick(function() { 
    for (var i = 0, len = records.length; i < len; i++) { 
     var record = records[i]; 
     if (record.username === username) { 
     return cb(null, record); 
     } 
    } 
    return cb(null, null); 
    }); 
} 

// Export it to the app 
module.exports = mongoose.model('user', UserSchema) 
+0

デバッグステップとして、リターンユーザオブジェクトを返す関数を返す関数をユーザー戻り関数に置き換えます。 – danh

+0

また、私はちょっとしたことですが、私が試みた少しのプロジェクトでは、passportLocalMongooseを使ってユーザー名、パスワード、塩分などをhttps:// githubにあるメソッドと同様に追加しています。 com/saintedlama/passport-local-mongoose#api-documentation – danh

答えて

0

パスポートから取られた文書:

リダイレクトは、要求を認証した後に発行されるのが一般的です。

私の推測では、あなただけ{failureRedirect: '/login' }
を持っているので、あなたがいつもそこにリダイレクトされていることです。これを追加してみてください:

app.post('/login', 
    passport.authenticate('local', { successRedirect: '/', 
            failureRedirect: '/login' })); 

したがって、この場合には、あなたがsuccessRedirectfailureRedirect

それとも、また、単に認証後にリダイレクトを削除し、代わりにこのようなユーザーをリダイレクトする可能性があります

app.post('/login', 
    passport.authenticate('local'), 
    function(req, res) { 
    console.log('Successfully logged in'); 
    res.redirect('/users/' + req.user.username); 
    }); 

これがうまくいけば教えてください

関連する問題