简体   繁体   中英

KeyStone JS Account Controller

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)?

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.

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/

Hope that helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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