如何同时发送对象和使用护照进行身份验证
How to send an object and authenticate with passport at the same time
这个问题看起来很模糊,我需要一种方法来发送 json 对象并同时使用护照进行身份验证。该对象是 req.isAuthenticated ,稍后将在前端作为检查点使用 axios 进行拾取。这就是我的意图。到目前为止,使用下面的代码,对象将不会被发送。
app.get('/login',
passport.authenticate('saml', {
successRedirect: '/assert',
failureRedirect: '/',
}),
(req, res) => {
res.json({isAuthenticated: req.isAuthenticated()})
}
);
这是我项目中的示例:
authorizeLocal: (req, res, next) => {
passport.authenticate('local-auth', (err, user, info) => {
if (info) console.log(info);
if (err) return next(err);
if (!user) return res.status(200).send({failReason: 'wrong login/password'});
req.logIn(user, err => {
if (err) return next(err);
delete user.password;
req.session.cookie.maxAge = 24 * 60 * 60 * 1000; // 24 hours
if (user.role === 'operator') {
user.status = 'Online';
operatorsService.setStatus('Online', user.id)
.then(result => {
dialogsService.getWaitingDialogs();
user.work_time = result;
res.status(200).send(user);
})
.catch(() => res.status(200).send({failReason: 'Service error'}));
} else res.status(200).send(user);
});
})(req, res, next);
},
在那里你可以看到护照 req.logIn
,它(在你的情况下需要 local-auth
策略或其他)执行身份验证,如果成功则触发回调逻辑。更深层次你可以有任何 user/object get/generation 逻辑。例如,我离开了我的案子。 OperatorsService.setStatus
returns 一些时间数据,存储给用户(用户在策略逻辑 运行 之后作为回调参数获得)结束作为响应发送。您可以在此处添加 user.isAuthenticated = req.isAuthenticated();
。
所以你会有这样的东西:
auth.route.js
app.get('/login', authCtrl.authorizeLocal);
authCtrl.js
authorizeLocal: (req, res, next) => {
passport.authenticate('saml', (err, user, info) => {
if (info) console.log(info);
if (err) return next(err);
// if (!user) return res.status(200).send({failReason: 'wrong login/password'});
req.logIn(user, err => {
if (err) return next(err);
res.status(200).send({isAuthenticated: req.isAuthenticated()}));
});
})(req, res, next);
},
这个问题看起来很模糊,我需要一种方法来发送 json 对象并同时使用护照进行身份验证。该对象是 req.isAuthenticated ,稍后将在前端作为检查点使用 axios 进行拾取。这就是我的意图。到目前为止,使用下面的代码,对象将不会被发送。
app.get('/login',
passport.authenticate('saml', {
successRedirect: '/assert',
failureRedirect: '/',
}),
(req, res) => {
res.json({isAuthenticated: req.isAuthenticated()})
}
);
这是我项目中的示例:
authorizeLocal: (req, res, next) => {
passport.authenticate('local-auth', (err, user, info) => {
if (info) console.log(info);
if (err) return next(err);
if (!user) return res.status(200).send({failReason: 'wrong login/password'});
req.logIn(user, err => {
if (err) return next(err);
delete user.password;
req.session.cookie.maxAge = 24 * 60 * 60 * 1000; // 24 hours
if (user.role === 'operator') {
user.status = 'Online';
operatorsService.setStatus('Online', user.id)
.then(result => {
dialogsService.getWaitingDialogs();
user.work_time = result;
res.status(200).send(user);
})
.catch(() => res.status(200).send({failReason: 'Service error'}));
} else res.status(200).send(user);
});
})(req, res, next);
},
在那里你可以看到护照 req.logIn
,它(在你的情况下需要 local-auth
策略或其他)执行身份验证,如果成功则触发回调逻辑。更深层次你可以有任何 user/object get/generation 逻辑。例如,我离开了我的案子。 OperatorsService.setStatus
returns 一些时间数据,存储给用户(用户在策略逻辑 运行 之后作为回调参数获得)结束作为响应发送。您可以在此处添加 user.isAuthenticated = req.isAuthenticated();
。
所以你会有这样的东西:
auth.route.js
app.get('/login', authCtrl.authorizeLocal);
authCtrl.js
authorizeLocal: (req, res, next) => {
passport.authenticate('saml', (err, user, info) => {
if (info) console.log(info);
if (err) return next(err);
// if (!user) return res.status(200).send({failReason: 'wrong login/password'});
req.logIn(user, err => {
if (err) return next(err);
res.status(200).send({isAuthenticated: req.isAuthenticated()}));
});
})(req, res, next);
},