为什么我没有机会允许或禁止使用linkedin进行身份验证?我只是自动获得一个经过身份验证的用户



我正在使用node.js和react构建一个应用程序。我需要使用带有Passport.js和MongoDB的Oauth v2对用户进行身份验证。我想允许用户使用谷歌、脸书或领英登录。我的谷歌授权/身份验证工作得很好。用户启动应用程序,获得一个带有登录选项的登录页面。当选择谷歌选项时,会呈现一个中间页面,要求用户选择他们的谷歌帐户(我有4个)。我选择我想要的谷歌帐户。应用程序将移动到应用程序中的下一页。我可以注销并返回我的登录页。所有这些都很好。现在,如果我选择使用LinkedIn登录(我只有一个帐户),应用程序不会给我中介页面,要求我允许(或不允许)登录。它简单且自动地为我提供一个经过身份验证的用户,并转到应用程序中的下一页。然后我可以注销并返回我的登录页面。因此,该应用程序正在正常工作,但并非完全正常。

我已经确认,当我开始使用领英登录时,用户收藏中没有任何项目。我已经确认,登录后,我确实在用户收藏中有一个Linkedin登录的项目。我已经(重复地)物理删除了该项目并重试。我没有机会允许/禁止登录Linkedin。

这是我的代码:


服务/passport.js


'use strict';
//  node modules
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const FacebookStrategy = require('passport-facebook').Strategy
const LinkedinStrategy = require('passport-linkedin-oauth2').Strategy;
const mongoose = require('mongoose');
//  local modules
const keys = require('../config/keys');
const User = mongoose.model('users');
passport.serializeUser((user, done) => {
done(null, user.id); //  NOTE: user.id = mongoDB _id
});
passport.deserializeUser((id, done) => {
User.findById(id)
.then(user => {
done(null, user);
});
});
passport.use(
new GoogleStrategy({
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback',
proxy: true
},
async (accessToken, refreshToken, profile, done) => {
const existingUser = await User.findOne({
provider: profile.provider,
providerID: profile.id
})
if (existingUser) {
return done(null, existingUser);
}
const user = await new User({
provider: profile.provider,
providerID: profile.id,
displayName: profile.displayName
}).save()
done(null, user);
})
)
passport.use(
new LinkedinStrategy({
clientID: keys.linkedinAppID,
clientSecret: keys.linkedinAppSecret,
callbackURL: '/auth/linkedin/callback',
proxy: true
},
async (accessToken, refreshToken, profile, done) => {
const existingUser = await User.findOne({
provider: profile.provider,
providerID: profile.id
})
if (existingUser) {
return done(null, existingUser);
}
const user = await new User({
provider: profile.provider,
providerID: profile.id,
displayName: profile.displayName
}).save()
done(null, user);
})
)

路由/auth.js


use strict';
//  node modules
const passport = require('passport');
//  local modules
const keys = require('../config/keys');
module.exports = (app) => {
//  google routes
app.get('/auth/google', 
passport.authenticate('google', {
scope: ['profile', 'email']
})
);
app.get('/auth/google/callback', 
passport.authenticate('google'),
(req, res) => {
res.redirect('/selector');
}
);
//  linkedin routes
app.get('/auth/linkedin', 
passport.authenticate('linkedin', {
request_type: 'code',
state: keys.linkedinAppState,
scope: ['r_basicprofile', 'r_emailaddress']
})
);
app.get('/auth/linkedin/callback', 
passport.authenticate('linkedin'),
(req, res) => {
res.redirect('/selector');
}
);
//  common routes
app.get('/api/logout', (req, res) => {
req.logout();
res.redirect('/');
});
app.get('/api/current_user', (req, res) => {
res.send(req.user);
});
}

我不知道你还有什么需要看的。我已经确认Header组件中的href指向正确的端点,并且它们与auth.js 中的路由匹配

经过进一步调查,我发现在使用passport.js而不是手动编写API接口时,必须进行大量的插值。我遇到的问题是因为API的各种参数设置不完整。通过暴力的尝试和错误,我解决了问题。

最新更新