我对节点js完全不熟悉。这是为了学习目的。我使用护照js验证用户名并登录它工作正常。
passport.use(new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
}, function (username, password, done) {
console.log(username,password);
User1.collection('mydb1', function(err, collection) {
if (!err) {
collection.findOne({
username:username
}, function(err,user) {
console.log(user);
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (user.password != password) {
return done(null, false, {
message: 'Invalid password'
});
} else {
return done(null, user);
}
})
}});
构造
app.use(bodyParser.urlencoded({extended: true}));
app.use(passport.initialize());
app.use(passport.session());
验证
app.post(
'/',
passport.authenticate(
'local',
{
successRedirect: '/success',
failureRedirect: '/loginerror'
}
)
);
在/success
我想在另一个页面中显示用户名,我尝试访问用户名,例如
request.user.usernmame
app.get('/success', function(request, response){
response.send('ok');
console.log(request.user.username);
});
但是我在控制台中收到以下错误消息
TypeError: Cannot read property 'usernmame' of undefined
at Object.app.post.passport.authenticate.successRedirect [as handle] (D:\nod
ejsmodule\mongodb\server.js:114:25)
at next_layer (D:\nodejsmodule\node_modules\express\lib\router\route.js:103:13)
at Route.dispatch (D:\nodejsmodule\node_modules\express\lib\router\route.js:107:5)
at D:\nodejsmodule\node_modules\express\lib\router\index.js:213:24
at Function.proto.process_params (D:\nodejsmodule\node_modules\express\lib\r
outer\index.js:286:12)
at next (D:\nodejsmodule\node_modules\express\lib\router\index.js:207:19)
at SessionStrategy.strategy.pass (D:\nodejsmodule\node_modules\passport\lib\
middleware\authenticate.js:314:9)
at SessionStrategy.authenticate (D:\nodejsmodule\node_modules\passport\lib\s
trategies\session.js:67:10)
at attempt (D:\nodejsmodule\node_modules\passport\lib\middleware\authenticat
e.js:337:16)
at Layer.authenticate [as handle] (D:\nodejsmodule\node_modules\passport\lib
\middleware\authenticate.js:338:7)
如果我在配置部分添加app.use(session({ secret: 'anything',saveUninitialized: true,resave: true }));
,我在控制台
ReferenceError: err is not defined
at passport.use.LocalStrategy.usernameField (D:\nodejsmodule\mongodb\server.
js:67:10)
at pass (D:\nodejsmodule\node_modules\passport\lib\authenticator.js:353:9)
at Authenticator.deserializeUser (D:\nodejsmodule\node_modules\passport\lib\
authenticator.js:358:5)
at SessionStrategy.authenticate (D:\nodejsmodule\node_modules\passport\lib\s
trategies\session.js:49:28)
at attempt (D:\nodejsmodule\node_modules\passport\lib\middleware\authenticat
e.js:337:16)
at Layer.authenticate [as handle] (D:\nodejsmodule\node_modules\passport\lib
\middleware\authenticate.js:338:7)
at trim_prefix (D:\nodejsmodule\node_modules\express\lib\router\index.js:254
:17)
at D:\nodejsmodule\node_modules\express\lib\router\index.js:216:9
at Function.proto.process_params (D:\nodejsmodule\node_modules\express\lib\r
outer\index.js:286:12)
at next (D:\nodejsmodule\node_modules\express\lib\router\index.js:207:19)
答案 0 :(得分:1)
我之前遇到过同样的问题。我想,你也一样。
原因是,Passport无法从POST正文中获取数据。要解决此问题,您必须提供Express to Passport的req
和res
个变量。
尝试使用auhtentication包装代码(req,res):
app.post(
'/',
passport.authenticate('local', {
successRedirect: '/success',
failureRedirect: '/loginerror'
})(req, res)
);
为了更好的视力,我会这样写:
app.post('/', passport.authenticate('local')(req, res));
答案 1 :(得分:1)
尝试执行console.log('myreq',request);并尝试从app.get更改为app.post。对不起,我想用手机版。
类似的东西:
app.post('/ success',function(request,response){response.send('ok'); 的console.log( 'myreq',请求);的console.log(request.user.username); });
答案 2 :(得分:0)
尝试执行console.log('myreq',request);并尝试从app.get更改为app.post。对不起,我想用手机版。