KeyStone JS Account Controller

时间:2015-10-29 15:58:44

标签: node.js model-view-controller express account keystonejs

I understand MVC structure when coding in NodeJS. I started using Keystone JS recently, and I really like it. But, the way they set their controllers up, it seems that the controllers ONLY serve the purpose of rendering a view.

In an earlier project, I had an Account.js model and an Account.js controller. I'm trying to see how it would copy over to keystone.

So: How would I allow users to signup/signin/logout in a Keystone project (not into the Admin UI, but like a member of a regular site)? How would I make an Account controller (obviously with no view to render)?

1 个答案:

答案 0 :(得分:1)

有很多方法可以在keystone中实现自己的身份验证和帐户管理方法,因为它基于express.js。

然后,您可以向将在将请求传递给控制器​​之前运行的路由添加一组“中间件”函数。

e.g 添加中间件之前的路由

app.get('/admin', routes.views.userAdmin);

中间件功能

function isAuthenticated(req, res, next) {

// do any checks you want to in here

// CHECK THE USER STORED IN SESSION FOR A CUSTOM VARIABLE
// you can do this however you want with whatever variables you set up
if (req.user.authenticated)
    return next();

// IF A USER ISN'T LOGGED IN, THEN REDIRECT THEM SOMEWHERE
res.redirect('/');
}

添加中间件的路线

app.get('/admin', isAuthenticated, routes.views.userAdmin);

这是一个非常广泛的问题所以我建议你去决定你想要自己做的最佳方式,因为每个人都有自己的个人偏好。您想要的搜索词是“快速中间件身份验证”。很多人使用PassporJS http://passportjs.org/

希望有所帮助:)