Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加 egg-passport-local example #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion passport/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;
});
Expand Down
17 changes: 1 addition & 16 deletions passport/app/controller/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,7 @@ class HomeController extends Controller {
if (ctx.isAuthenticated()) {
ctx.body = ctx.user;
} else {
ctx.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>
`;
await ctx.render('login.html');
}
}
}
Expand Down
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>
9 changes: 4 additions & 5 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 @@ -21,9 +23,6 @@ exports.passportTwitter = {
secret: 'h',
};

// 为了演示方便这里把 csrf 暂时关闭
exports.security = {
csrf: {
enable: false,
},
exports.view = {
defaultViewEngine: 'nunjucks',
};
5 changes: 5 additions & 0 deletions passport/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ exports.passportBitbucket = {
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