Skip to content
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.
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 || 'local';
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 这个取值不对吧

Copy link
Author

Choose a reason for hiding this comment

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

我的想法是为了跟其他 strategy 的展示结果对应

// home.js
Logined user: <img src="${ctx.user.photo}"> ${ctx.user.displayName} / ${ctx.user.id} | <a href="/logout">Logout</a>

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 的语义应该是用户唯一 ID,不应该是你这个 local 的取值吧

Copy link
Author

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 得由应用方产生

// app.js
module.exports = app => {
  app.passport.verify(async (ctx, user) => {
    const existsUser = await ctx.model.User.findOne({  id: user.id });
    if (existsUser) {
      return existsUser;
    }
    const newUser = await ctx.service.user.register(user); // 生成 id
    return newUser;
  });
};

Copy link
Member

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 才对

user.displayName = user.displayName || user.name;
return user;
});
};
27 changes: 26 additions & 1 deletion passport/app/controller/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Copy link
Member

Choose a reason for hiding this comment

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

改为 egg-view-nunjucks ?

Copy link
Author

Choose a reason for hiding this comment

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

// 为了演示方便这里把 csrf 暂时关闭
Copy link
Member

Choose a reason for hiding this comment

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

英文或干掉,其实用 egg-view-nunjucks 后就不用关了。

Copy link
Author

Choose a reason for hiding this comment

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

OK

exports.security = {
csrf: {
enable: false,
},
};
5 changes: 5 additions & 0 deletions passport/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ exports.passportBitbucket = {
enable: true,
package: 'egg-passport-bitbucket',
};

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