官网教程:https://eggjs.org/zh-cn/tutorials/passport.html
1.后端安装插件
$ npm i --save egg-passport
$ npm i --save egg-passport-github
//如果是weixin,qq等三方登陆,那么就是安装egg-passport-weixin等
2.后端开启插件
plugin.ts
passport: {
enable: true,
package: 'egg-passport',
},
passportGithub: {
enable: true,
package: 'egg-passport-github',
},
3.创建github oauth apps
https://github.com/settings/applications/new
Homepage URL写前端服务器根路径
Authorization callback URL写后端服务器根路径+/passport/github/callback
获取clientID 和 clientSecret 信息
4.后端配置key和secret
config.default.ts
config.passportGithub = {
key: 'fba73323af3xxxc0a54362',
secret: '7675822zeb42fx2e19038165eb5dcce11bfa12e4f',
};
5.后端挂载路由
router.ts
const github = (app as any).passport.authenticate('github', {
//用户登陆或者注册登陆成功后显示的前端页面地址
successRedirect: 'http://127.0.0.1:8080/admin'
});
router.get('/passport/github', github