From c24bc03c2ebfc8eb75f4a490ec8f7996b7ca2dcd Mon Sep 17 00:00:00 2001 From: OnedayLiu Date: Wed, 17 Jan 2018 20:39:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20egg-passport-l?= =?UTF-8?q?ocal=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- passport/app.js | 9 +++++++++ passport/app/controller/home.js | 27 ++++++++++++++++++++++++++- passport/app/router.js | 3 +++ passport/config/config.default.js | 7 +++++++ passport/config/plugin.js | 5 +++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 passport/app.js diff --git a/passport/app.js b/passport/app.js new file mode 100644 index 0000000..39340b4 --- /dev/null +++ b/passport/app.js @@ -0,0 +1,9 @@ +'use strict'; +module.exports = app => { + app.passport.verify(async (ctx, user) => { + user.photo = user.photo || 'https://zos.alipayobjects.com/rmsportal/JFKAMfmPehWfhBPdCjrw.svg'; + user.id = user.provider || 'local'; + user.displayName = user.displayName || user.name; + return user; + }); +}; diff --git a/passport/app/controller/home.js b/passport/app/controller/home.js index cfca6af..c10b43e 100644 --- a/passport/app/controller/home.js +++ b/passport/app/controller/home.js @@ -23,13 +23,38 @@ class HomeController extends Controller {
Login with Weibo | Github | - Bitbucket | Twitter + Bitbucket | Twitter | + Local
Home | User `; } } + + async local() { + const { ctx } = this; + if (ctx.isAuthenticated()) { + ctx.body = ctx.user; + } else { + ctx.body = ` +

egg-passport-local login page

+
+
+ + +
+
+ + +
+
+ +
+
+ `; + } + } } module.exports = HomeController; diff --git a/passport/app/router.js b/passport/app/router.js index e7f242f..6b4c3ec 100644 --- a/passport/app/router.js +++ b/passport/app/router.js @@ -3,11 +3,14 @@ module.exports = app => { app.router.get('/', 'home.render'); app.router.get('/user', 'home.render'); + app.router.get('/login', 'home.local'); app.passport.mount('weibo'); app.passport.mount('github'); app.passport.mount('bitbucket'); app.passport.mount('twitter'); + const localStrategy = app.passport.authenticate('local'); + app.router.post('/passport/local', localStrategy); app.router.get('/logout', 'user.logout'); }; diff --git a/passport/config/config.default.js b/passport/config/config.default.js index 7dc8168..82accdf 100644 --- a/passport/config/config.default.js +++ b/passport/config/config.default.js @@ -20,3 +20,10 @@ exports.passportTwitter = { key: 'g', secret: 'h', }; + +// 为了演示方便这里把 csrf 暂时关闭 +exports.security = { + csrf: { + enable: false, + }, +}; diff --git a/passport/config/plugin.js b/passport/config/plugin.js index 1ed71bb..48c2611 100644 --- a/passport/config/plugin.js +++ b/passport/config/plugin.js @@ -24,3 +24,8 @@ exports.passportBitbucket = { enable: true, package: 'egg-passport-bitbucket', }; + +exports.passportLocal = { + enable: true, + package: 'egg-passport-local', +}; \ No newline at end of file From fa60035a1df2671e6d37b45e1f5054314cb2f46a Mon Sep 17 00:00:00 2001 From: OnedayLiu Date: Thu, 18 Jan 2018 23:29:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=20egg-passport-l?= =?UTF-8?q?ocal=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- passport/app.js | 2 +- passport/app/controller/home.js | 17 +---------------- passport/app/view/login.html | 27 +++++++++++++++++++++++++++ passport/config/config.default.js | 9 ++++----- passport/config/plugin.js | 5 +++++ passport/package.json | 4 +++- 6 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 passport/app/view/login.html diff --git a/passport/app.js b/passport/app.js index 39340b4..5d9abc2 100644 --- a/passport/app.js +++ b/passport/app.js @@ -2,7 +2,7 @@ module.exports = app => { app.passport.verify(async (ctx, user) => { user.photo = user.photo || 'https://zos.alipayobjects.com/rmsportal/JFKAMfmPehWfhBPdCjrw.svg'; - user.id = user.provider || 'local'; + user.id = user.provider; user.displayName = user.displayName || user.name; return user; }); diff --git a/passport/app/controller/home.js b/passport/app/controller/home.js index c10b43e..94e8f60 100644 --- a/passport/app/controller/home.js +++ b/passport/app/controller/home.js @@ -37,22 +37,7 @@ class HomeController extends Controller { if (ctx.isAuthenticated()) { ctx.body = ctx.user; } else { - ctx.body = ` -

egg-passport-local login page

-
-
- - -
-
- - -
-
- -
-
- `; + await ctx.render('login.html'); } } } diff --git a/passport/app/view/login.html b/passport/app/view/login.html new file mode 100644 index 0000000..d40bbf4 --- /dev/null +++ b/passport/app/view/login.html @@ -0,0 +1,27 @@ + + + + + + + egg-passport-local login page + + + +

egg-passport-local login page

+
+
+ + +
+
+ + +
+
+ +
+
+ + + \ No newline at end of file diff --git a/passport/config/config.default.js b/passport/config/config.default.js index 82accdf..8d72b33 100644 --- a/passport/config/config.default.js +++ b/passport/config/config.default.js @@ -1,5 +1,7 @@ 'use strict'; +exports.keys = 'egg-examples'; + // secure keys on https://github.com/eggjs/egg/wiki#secure-env-for-travis-ci exports.passportWeibo = { key: 'a', @@ -21,9 +23,6 @@ exports.passportTwitter = { secret: 'h', }; -// 为了演示方便这里把 csrf 暂时关闭 -exports.security = { - csrf: { - enable: false, - }, +exports.view = { + defaultViewEngine: 'nunjucks', }; diff --git a/passport/config/plugin.js b/passport/config/plugin.js index 48c2611..ad55aba 100644 --- a/passport/config/plugin.js +++ b/passport/config/plugin.js @@ -28,4 +28,9 @@ exports.passportBitbucket = { exports.passportLocal = { enable: true, package: 'egg-passport-local', +}; + +exports.nunjucks = { + enable: true, + package: 'egg-view-nunjucks', }; \ No newline at end of file diff --git a/passport/package.json b/passport/package.json index 7baaa87..f0d1e85 100644 --- a/passport/package.json +++ b/passport/package.json @@ -5,8 +5,10 @@ "egg-passport": "^1.0.0", "egg-passport-bitbucket": "^1.0.0", "egg-passport-github": "^1.0.0", + "egg-passport-local": "^1.1.0", "egg-passport-twitter": "^1.0.0", - "egg-passport-weibo": "^1.0.0" + "egg-passport-weibo": "^1.0.0", + "egg-view-nunjucks": "^2.1.4" }, "devDependencies": { "egg-bin": "^4.3.5" From 69e994c6a1016bd909362f663ed17e49ef8f6ee8 Mon Sep 17 00:00:00 2001 From: OnedayLiu Date: Fri, 19 Jan 2018 11:27:23 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=20user.id=20?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- passport/app.js | 1 - 1 file changed, 1 deletion(-) diff --git a/passport/app.js b/passport/app.js index 5d9abc2..9d52f83 100644 --- a/passport/app.js +++ b/passport/app.js @@ -2,7 +2,6 @@ module.exports = app => { app.passport.verify(async (ctx, user) => { user.photo = user.photo || 'https://zos.alipayobjects.com/rmsportal/JFKAMfmPehWfhBPdCjrw.svg'; - user.id = user.provider; user.displayName = user.displayName || user.name; return user; });