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
.
You can use a hack-ish method to determine if a user is logged in from the same origin, that doesn't require additional code.
This doesn't help with signing in or registering, but it is can determine if someone is logged in already.
Just send a GET request to the built-in keystone api:
domain.com/keystone/api/listname
you can use any list for "listname". If it comes back with actual json data, then you're logged in. If it comes back with the html of the sign in page, then you aren't logged in!