-
Notifications
You must be signed in to change notification settings - Fork 738
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 || 'local'; | ||
user.displayName = user.displayName || user.name; | ||
return user; | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,38 @@ 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 { | ||
ctx.body = ` | ||
<h1>egg-passport-local login page</h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改为 egg-view-nunjucks ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK,我改下 |
||
<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> | ||
`; | ||
} | ||
} | ||
} | ||
|
||
module.exports = HomeController; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 想起来了,之前我 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你的想法是不是这样: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 嗯 |
||
app.router.post('/passport/local', localStrategy); | ||
|
||
app.router.get('/logout', 'user.logout'); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,10 @@ exports.passportTwitter = { | |
key: 'g', | ||
secret: 'h', | ||
}; | ||
|
||
// 为了演示方便这里把 csrf 暂时关闭 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 英文或干掉,其实用 egg-view-nunjucks 后就不用关了。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
exports.security = { | ||
csrf: { | ||
enable: false, | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user.id 这个取值不对吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我的想法是为了跟其他 strategy 的展示结果对应
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user.id
的语义应该是用户唯一 ID,不应该是你这个 local 的取值吧There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg-passport-local
只有 username,password,provider,user.id
得由应用方产生There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,所以你这里是没必要写这个 ID 的。
即使要显示 strategy,那应该是 ctx.user.provider 才对