我已经使用Passport.js进行linkedin验证,并且效果很好。现在,如果数据库中已经存在该用户,我想存储一个cookie。
在我的网站上,我使用过googlee,linkedin,github身份验证。
在google身份验证中,我使用了相同的逻辑如果用户已经存在,我想存储一个cookie。为此,我的代码
router.get(
'/auth/google/callback',
passportGoogle.authenticate('google', {
failureRedirect: '/login',
}),
(req, res) => {
const nameFirst = req.user.profile._json.displayName;
const email = req.user.profile.emails[0].value;
const id = req.user.profile.id;
const user = new User({
user_token: id,
name: nameFirst,
email: email,
provider: 'Google',
dateSent: Date.now(),
});
User.find({ email: email }, (err, docs) => {
if (docs.length) {
req.session.token = req.user.token;
res.redirect('/dashboard');
} else {
user
.save()
.then(user => {
res.redirect('/');
})
.catch(err => {
console.log(err);
});
}
});
}
);
在google autho中它可以正常工作,但是对于linkedin和github登录,我无法存储cookie,并且也不会显示错误或异常。 我很累,但是我无法修复这些错误。请帮助我解决此问题 预先谢谢你
对于Linkedin
router.get(
'/auth/linkedin/callback',
passportLinkedIn.authenticate('linkedin', { failureRedirect: '/' }),
(req, res) => {
const nameFirst = req.user.profile._json.firstName;
const email = req.user.profile._json.emailAddress;
const id = req.user.profile.id;
const user = new User({
user_token: id,
name: nameFirst,
email: email,
provider: 'linkedIn',
dateSent: Date.now(),
});
User.find({ email: email }, (err, docs) => {
if (docs.length) {
console.log('coo');
console.log(req.user.accessToken);
req.session.token = req.accessToken;
res.redirect('/dashboard');
} else {
user
.save()
.then(user => {
res.redirect('/');
})
.catch(err => {
console.log(err);
});
}
});
}
);
我在此行req.session.token = req.accessToken;
中存储了一个cookie