Comments (19)
可以用 egg-passport-github 和 egg-passport-local
from egg-cnode.
这里要用 egg-passport 来做么?
from egg-cnode.
要
from egg-cnode.
好的,那我开一个 feature-passport 分支
from egg-cnode.
登录注册这边就改为下面这样两个部分吧?
-
controller.sign
- 显示登录页面
- 注册
-
passport
- local
- github
- other
from egg-cnode.
cnode 本来就只支持 local 和 github
from egg-cnode.
疑问
- egg-passport 登录时如何传递成功/失败消息有点疑问
- 返回 null 可跳转登录页,但目前消息传递有问题,可选实现为直接写 session
- 若抛出异常(401),是否考虑使用通用的错误页面
- 登录成功的页面需要认证后的数据,是否考虑通过中间件将 session 中的信息添加到 ctx.locals,减少路由中重复书写?
参考
- egg-passport 成功/失败信息
done(null, user, message)
// 此处不抛出异常则缺少 message, egg-passport 实现为 done(null, user)
from egg-cnode.
handler 抛异常会通过 done(err) 返回 https://github.com/eggjs/egg-passport/blob/0afce5b0d5fbc107730e10dad6aff915c03cf079/lib/passport.js#L63
当然,你可以在 handler 里面实现 try catch 自行决定如何封装错误处理。
是否自定义 401 页面逻辑,完全由应用自身决定。
user 为 null 肯定就是登录失败了。
登录成功的页面需要认证后的数据,是否考虑通过中间件将 session 中的信息添加到 ctx.locals,减少路由中重复书写?
passport 不应该管数据保存到那里,这些都是后续应用去接管的操作。
from egg-cnode.
而且第三方登录失败,基本不会跳转回来,都在第三方网站卡住了。。。
from egg-cnode.
@fengmk2 passport-local
是本地
from egg-cnode.
上面说的是两部分:
-
passport 通过以后获取的信息,可以考虑通过一个 egg 中间节来添加,渲染的时候不用重复在
ctx.render(template, locals)
中添加,类似router.get(path, middleware, controller.method)
-
passport-local 是 passport 的 handler 部分,这里如果不通过 done 方法传递 message,
可以考虑直接写 message 到 session,是 passport 的 handler 处理了数据,和外部处理差不多,根据 @fengmk2 的建议,可以考虑通过封装 user 对象返回用户信息以及消息(验证成功/失败)并存储在 ctx.session.passport 中,此后可使用中间件可读取数据并消除 connect-flash 中使用ctx.session.message 带来的冲突
from egg-cnode.
cc @OnedayLiu
from egg-cnode.
@atian25 你们考虑自己实现一个 passport 吗,我看了下底下依赖的,写得比较戳。
from egg-cnode.
@_@ 要问问 @fengmk2 了,是他 wrap 的 passportjs ,后者好像是 express-base 的, wrap 起来就...
from egg-cnode.
@fengmk2 使用 egg-passport-local 插件,試過直接拋異常如下:
// app.js
app.passport.verify(async (ctx, user) => {
throw new Error('找不到 user');
}
在 npm run dev 執行的環境下,網頁就回應 500 狀態碼。
from egg-cnode.
@bestlong 嗯,是个问题,在跟苏千大大讨论方案
from egg-cnode.
@OnedayLiu 我正好在研究當 verify 失敗時要如何處理錯誤訊息讓前端可以顯示
doVerify(req, user, done) {
const hooks = this._verifyHooks;
if (hooks.length === 0) return done(null, user);
(async () => {
const ctx = req.ctx;
for (const handler of hooks) {
user = await handler(ctx, user);
if (!user) {
break;
}
}
done(null, user);
})().catch(done);
}
可以先考慮在上列的 doVerify 範圍內增加捕捉 exception 然後改跑 done(err, user)
這樣就可以用 throw new Error() 丟錯誤訊息了。
可惜目前 doVerify 的程式碼段落還不是我完全看得懂的語法,所以無法直接動手改程式送 PR 了。
from egg-cnode.
done(null, user);
有考虑过在这增加捕捉错误信息,但是如果这里出错的话是直接被 passport.js 直接捕获的
from egg-cnode.
我試過改成這樣但還是不行,還是 500 錯誤跑到 node_modules/koa-onerror/index.js 出現 non-error thrown 錯誤訊息,看來我的認知還不夠完整
doVerify(req, user, done) {
const hooks = this._verifyHooks;
if (hooks.length === 0) return done(null, user);
(async () => {
const ctx = req.ctx;
for (const handler of hooks) {
user = await handler(ctx, user);
if (!user) {
break;
}
}
done(null, user);
}
})().catch(err => {
done(err.message, user);
});
}
from egg-cnode.
Related Issues (20)
- docker-compose.yml中mongodb volumes问题
- 配置Alinode报错nodeinstall --install-alinode ^3 HOT 1
- egg-oauth2-server
- cnode 502, rescue ? HOT 2
- ctx.message 不能使用中文,但是你们源码中大量使用,怎么做到的
- [egg:core:ready_timeout] 10 seconds later redis.js was still unable to finish. HOT 2
- 根目录的app.js一启动是调用了吗?怎么用的? HOT 1
- 怎么调试? HOT 12
- 样例数据
- 没有阿里云怎么部署?
- Bootstrap 升级到3
- egg项目使用egg-sequlize,如何控制应用级别的数据库连接池大小
- 无法登录测试环境
- 求惑,config的数据是如何进入到layout.html模板里面的
- 论坛图床是不是挂了,头像好多看不见
- 论坛无法访问了么? HOT 13
- 运行nodeinstall --install-alinode ^3 报unknow version错误
- bcrypt 的 compare 方法,为什么在 TODO 中建议改成异步的调用方式,同步的调用方式有什么问题么?
- CNode社区的API页面未居中
- 论坛又无法访问了吗 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from egg-cnode.