我正在努力让Passport在本地策略上使用自定义回调。
我在前端和nodejs中使用AngularJS,在后端表示
到目前为止,我已经能够完成整个工作流程。我的问题是,当使用自定义回调时,显然策略不会调用验证回调中的反序列化/序列化函数。奇怪的是,当我让护照处理其余部分而不给出自定义回调时,函数被调用,我得到了foo' foo'或者' bar'打印在控制台上。
或者也许我只是错过了一些重要的东西,但我找不到文档中的任何内容或其他来源可以解决我的问题。
var sessionOpts = {
saveUninitialized: true, // saved new sessions
resave: false, // do not automatically write to the session store
store: sessionStore,
secret: sessionSecret,
cookie : { httpOnly: true, maxAge: config.session.maxAge } // configure when sessions expires
};
var server = express();
// log all requests to the console
server.use(morgan('dev'));
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({
extended: true
}));
server.use(session(sessionOpts));
server.use(passport.initialize());
server.use(passport.session());
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
console.log('foo')
var sessionUser = {
id: user._id,
email: user.email
};
done(null, sessionUser)
});
// used to deserialize the user
passport.deserializeUser(function(sessionUser, done) {
console.log('bar')
done(null, sessionUser);
});
passport.use('local-signin', 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) { // callback with email and password from our form
// attempt to authenticate user
User.getAuthenticated(email, password, function (err, user, reason) {
if (err) return done(err);
// login was successful if we have a user
if (user) {
// handle login success
return done(null, user);
}
// otherwise send back the reason why it failed
return done(null, false, reason)
});
}));
这是我在express中定义的路由(错误处理现在只是虚拟错误代码):
server.get('/signin', function(req, res, next) {
passport.authenticate('local-signin', function(err, user, reason) {
if (err) {
return res.sendStatus(500);
}
// login was successful if we have a user
if (user) {
// handle login success
return res.send(user);
}
// otherwise we can determine why we failed
var reasons = User.failedLogin;
switch (reason) {
case reasons.NOT_FOUND:
return res.sendStatus(401);
break;
case reasons.PASSWORD_INCORRECT:
// note: these cases are usually treated the same - don't tell
// the user *why* the login failed, only that it did
return res.sendStatus(402);
break;
case reasons.MAX_ATTEMPTS:
// send email or otherwise notify user that account is
// temporarily locked
return res.sendStatus(403);
break;
default:
return res.sendStatus(500)
}
})(req, res, next);
});
提前谢谢!
答案 0 :(得分:1)
在搜索互联网大约2天后,绝对没有结果或任何可以帮助我解决问题的例子我在这里发布了这个问题。现在发布后,我在文档中找到了解决方案。多么尴尬......
解决方案在这句话中:"请注意,在使用自定义回调时,应用程序负责建立会话(通过调用req.login())并发送响应。 #34; 强>
无论如何......我希望这可以帮助别人节省一些时间
上面编辑的代码:
SELECT lo_export(oid-to-export, '/path/to/a/file');