【发布时间】:2021-07-18 06:16:06
【问题描述】:
所以我一直在尝试在我的应用程序中实现 使用linkedin 登录,但我在网上找不到任何可以告诉我从 A 到 Z 的步骤的东西 我实现了 backend 和 frontend separateley,但是,我不知道如何将它们链接在一起
在后台,我用的是passportjs
这就是我到目前为止所做的:
前端
app.component.html
<button click="loginWithLinkedin()">Linkedin</button>
app.component.ts
window.location.href = `https://www.linkedin.com/uas/oauth2/authorization?response_type=code&state=true&client_id=${environment.LINKEDIN_API_KEY}&redirect_uri=${environment.LINKEDIN_REDIRECT_URL}&scope=${environment.LINKEDIN_SCOPE}`;
redirect.component.ts
const linkedInToken = this.route.snapshot.queryParams["code"];
this.http.get('http://localhost:3000/user/auth/linkedin',
{ params: { token: linkedinToken }}).subscribe(res => {
console.log(res);
});
后台
passport.use(new LinkedInStrategy({
clientID: LINKEDIN_CLIENT_ID,
clientSecret: LINKEDIN_CLIENT_SECRET,
callbackURL: "http://127.0.0.1:8000/user/auth/linkedin/callback",
scope: ['r_emailaddress', 'r_basicprofile'],
passReqToCallback: true
},
function (req, accessToken, refreshToken, profile, done) {
req.session.accessToken = accessToken;
process.nextTick(function () {
return done(null, profile);
});
}));
linkedinRouter.route('/auth/linkedin')
.get(passport.authenticate('linkedin', { state: 'SOME STATE' }),
function(req, res){
// The request will be redirected to LinkedIn for authentication, so this
// function will not be called.
});
linkedinRouter.route('/auth/linkedin/callback')
.get( passport.authenticate('linkedin', { failureRedirect: '/' }),
function (req, res) {
return res.send('hello');
});
我不明白护照如何工作,也不明白如何链接后端和前端。 我不知道这是否是实现linkedin认证的正确方法
如果你有任何可以指导的文章,或者如果你能纠正这真的有帮助,我已经被困了几天了。
非常感谢
【问题讨论】:
标签: node.js angular authentication passport.js linkedin-api