To implement API authentication in KeystoneJS, you need the following:
For key based authentication
- Middleware that validates the key in the request body or a header
For session based authentication
- An endpoint that handles signin
- An endpoint that handles signout
- Middleware that validates the session
Examples of both methods are below.
Note that the code in each file below would normally be spread across several files, depending on your project organisation, e.g. route handlers in a /routes/api/...
folder, and route bindings in /routes/index.js
.
With a correct username and wrong password you get a 200 code. I changed the
keystone.session.signin
error callback to:return res.status(401).json({ 'error': 'wrong credentials' });