admin管理员组文章数量:1333430
I'm having following error while authenticating LinkedIn in Passport JS.
{
"message": "LinkedIn authentication failed",
"error": "failed to fetch user profile"
}
Here is my code:
passport.use(
new LinkedInStrategy(
{
clientID: process.env.LINKEDIN_CLIENT_ID,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET,
callbackURL: '',
scope: ['email', 'profile'],
},
async (accessToken, refreshToken, profile, done) => {
// console.log('Access Token:', accessToken);
console.log('Profile:', profile);
console.log('hi');
try {
let user = await User.findOne({ linkedinId: profile.id });
if (user) {
return done(null, user);
}
user = await User.findOne({ email: profile.emails[0].value });
if (user) {
if (!user.linkedinId) {
user.linkedinId = profile.id;
user.verify = true;
await user.save();
}
console.log('user in strategy:', user);
return done(null, user);
}
const newUser = new User({
linkedinId: profile.id,
email: profile.emails[0].value,
verify: true,
});
await newUser.save();
done(null, newUser);
} catch (error) {
done(error, null);
}
}
)
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
const user = await User.findById(id);
done(null, user);
} catch (error) {
done(error, null);
}
});
exports.linkedinAuth = passport.authenticate('linkedin', { state: true, scope: ['email', 'profile']});
exports.linkedinAuthCallback = (req, res, next) => {
passport.authenticate('linkedin', { session: false }, async (error, user, info) => {
console.log('info:', info);
console.log('user in callback:', user);
if (error) {
console.error('LinkedIn authentication error:', error);
return res.status(400).json({ message: 'LinkedIn authentication failed error', error: error.message });
}
if (!user) {
const message = info ? info.message : 'User already registered with LinkedIn';
return res.redirect(`=${encodeURIComponent(message)}`);
}
try {
const token = jwt.sign({ userId: user._id }, secretKey, { expiresIn: '1h' });
const redirectUrl = `/recommendation?token=${token}&userId=${user._id}`;
res.redirect(redirectUrl);
} catch (jwtError) {
console.error('JWT generation error:', jwtError);
res.status(500).json({ message: 'Internal server error during JWT generation' });
}
})(req, res, next);
};
Also, I'm curious to know why the console 'hi' isn't printing? I'm actually confused about the flow of the process. Would be appreciated if someone guide about that. Note: Please read the question thoroughly instead of downvoting or suggesting a dupicate as there are the details that differ from other questions posted earlier.
Here is the link to a short video demonstrating this issue:
本文标签:
版权声明:本文标题:node.js - "failed to fetch user profile" Error While Authenticating LinkedIn in Passport JS OAuth2 - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742307899a2450284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论