简体   繁体   English

KeyStone JS帐户控制器

[英]KeyStone JS Account Controller

I understand MVC structure when coding in NodeJS. 我在NodeJS中编码时了解MVC结构。 I started using Keystone JS recently, and I really like it. 我最近开始使用Keystone JS,我非常喜欢它。 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. 在较早的项目中,我有一个Account.js模型和一个Account.js控制器。 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)? 因此:我将如何允许用户在Keystone项目中注册/登录/注销(不是进入管理界面,而是像常规站点的成员一样)? How would I make an Account controller (obviously with no view to render)? 我将如何制作“帐户”控制器(显然没有可呈现的视图)?

There are lots of ways you can implement your own methods of authentication and account management in keystone since it is based on express.js. 由于Keystone基于express.js,因此有很多方法可以在keystone中实现自己的身份验证和帐户管理方法。

You can then add an array of 'middleware' functions to routes which will run before passing the request to the controller. 然后,您可以向路由添加“中间件”功能数组,这些路由将在将请求传递给控制器​​之前运行。

eg Route before middleware added 例如, 在添加中间件之前进行路由

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

Middleware Function 中间件功能

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('/');
}

Route with middleware added 添加中间件的路由

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

It's a very broad questions so I'd recommend you go and decide on the best way you'd like to do it yourself as everyone has their own personal preference. 这是一个非常广泛的问题,因此我建议您自己去决定最好的方法,因为每个人都有自己的个人喜好。 The search terms you want are 'express middleware authentication'. 您想要的搜索词是“快速中间件认证”。 A lot of people use PassporJS http://passportjs.org/ 很多人使用PassporJS http://passportjs.org/

Hope that helps :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM