Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions passport/app.js
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是,我的意思是,为什么你要把 user.id 赋值为 user.provider ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

搞错

user.displayName = user.displayName || user.name;
return user;
});
};
12 changes: 11 additions & 1 deletion passport/app/controller/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ class HomeController extends Controller {
<hr>
Login with
<a href="/passport/weibo">Weibo</a> | <a href="/passport/github">Github</a> |
<a href="/passport/bitbucket">Bitbucket</a> | <a href="/passport/twitter">Twitter</a>
<a href="/passport/bitbucket">Bitbucket</a> | <a href="/passport/twitter">Twitter</a> |
<a href="/login">Local</a>
<hr>
<a href="/">Home</a> | <a href="/user">User</a>
</div>
`;
}
}

async local() {
const { ctx } = this;
if (ctx.isAuthenticated()) {
ctx.body = ctx.user;
} else {
await ctx.render('login.html');
}
}
}

module.exports = HomeController;
3 changes: 3 additions & 0 deletions passport/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想起来了,之前我 app.passport.mount('twitter'); 的想法是会去判断对应的 strategy 如果提供了 mount 方法就用它的,否则用默认的。不过这样先也行吧

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你的想法是不是这样:
可以由各个 strategy 实现自己的 mount 方法,用户调用 mount 的时候优先调用 strategy 的 mount,没有的的话才调用 egg-passport 默认的 mount

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.router.post('/passport/local', localStrategy);

app.router.get('/logout', 'user.logout');
};
27 changes: 27 additions & 0 deletions passport/app/view/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>egg-passport-local login page</title>
</head>

<body>
<h1>egg-passport-local login page</h1>
<form method="post" action="/passport/local">
<div>
<label>Username:</label>
<input type="text" name="username" />
</div>
<div>
<label>Password:</label>
<input type="password" name="password" />
</div>
<div>
<input type="submit" value="Log In" />
</div>
</form>
</body>

</html>
6 changes: 6 additions & 0 deletions passport/config/config.default.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -20,3 +22,7 @@ exports.passportTwitter = {
key: 'g',
secret: 'h',
};

exports.view = {
defaultViewEngine: 'nunjucks',
};
10 changes: 10 additions & 0 deletions passport/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ exports.passportBitbucket = {
enable: true,
package: 'egg-passport-bitbucket',
};

exports.passportLocal = {
enable: true,
package: 'egg-passport-local',
};

exports.nunjucks = {
enable: true,
package: 'egg-view-nunjucks',
};
4 changes: 3 additions & 1 deletion passport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down