Typescript 如何诊断passport.authenticate?
由于函数没有返回值,我如何识别导致函数失败的原因。我有以下代码:Typescript 如何诊断passport.authenticate?,typescript,oauth-2.0,passport.js,Typescript,Oauth 2.0,Passport.js,由于函数没有返回值,我如何识别导致函数失败的原因。我有以下代码: function test(req : IncomingMessage, res :ServerResponse, next:(err:any)=>void) { passport.authenticate('google', {scope:['https://www.googleapis.com/auth/userinfo.email']})(req, res,next); //how do I get det
function test(req : IncomingMessage, res :ServerResponse, next:(err:any)=>void) {
passport.authenticate('google', {scope:['https://www.googleapis.com/auth/userinfo.email']})(req, res,next);
//how do I get details here
}
我的意思是,我知道它失败了,因为行中的下一个处理程序没有被调用,但我如何获得更详细的信息
此函数的实例如下所示:
export const usersRouter = express
.Router()
.post('/googleLogin', test, async (req, res) => {
const user: User = req.user;
if (!user) {
res.send(401);
}
// const token = getJwtToken(user._id.toHexString());
// res.send({ token });
})
可能需要一个自定义回调。() 在本例中,请注意,authenticate()是从路由处理程序中调用的,而不是用作路由中间件。这使回调可以通过闭包访问req和res对象
app.get('/googleLogin', function(req, res, next) {
passport.authenticate('google', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});
像这样的身份验证工作流(oauth/openid)涉及几个步骤
app.get('/auth/google',
passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/userinfo.email']
});
app.get('/auth/google',
passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/userinfo.email']
});
您将从用户界面使用此端点,例如:
<a href="/auth/google">Connect with google</a>
此时passport将执行身份验证流程,以从google请求访问/刷新令牌。
当服务器从google请求访问/刷新令牌时,将调用策略定义的回调:
passport.use(new GoogleStrategy({
consumerKey: GOOGLE_CONSUMER_KEY,
consumerSecret: GOOGLE_CONSUMER_SECRET,
callbackURL: "http://www.example.com/auth/google/callback"
},
function(token, tokenSecret, profile, done) {
// you can debug google's response here (this is from your server's request)
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
希望这能有所帮助。已尝试添加回调,但仍未实例化。