JSON Server Auth is a middleware that adds JWT-based authentication and authorization capabilities to JSON Server, enabling developers to quickly create mock REST APIs with realistic security flows for prototyping and testing. Currently at version 2.1.0, this package doesn't specify a fixed release cadence but generally follows the evolution of JSON Server. Its key differentiators include a simplified authentication flow with user registration and login endpoints (`/register`, `/login`), automatic password hashing with `bcryptjs`, and a Unix-like numeric permission system (e.g., `640`) for granular resource authorization based on owner, logged-in users, and public access. It integrates directly with JSON Server, either via its dedicated CLI or as a programmatic middleware, making it an efficient tool for front-end development requiring mock authentication without backend implementation.
npm install json-server-authVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install `json-server` and `json-server-auth`, create a basic `db.json` with a 'users' collection, start the server using the `json-server-auth` CLI, and then perform user registration, login, and access a protected resource using the obtained JWT access token. The TypeScript language is chosen as the library ships types.
Consult the specific release notes for `json-server-auth` and `json-server` when upgrading. Ensure your `db.json` structure and any programmatic configurations align with the new versions. Test existing authentication and authorization flows thoroughly.
For any scenario beyond local development, configure a strong, unique JWT secret. This can typically be done via environment variables (e.g., `process.env.JWT_SECRET`) if using a custom `json-server` setup, or through specific configuration options if provided by the middleware.
Ensure your `db.json` file has an empty `users` array at minimum: `{"users": []}`. Additional user properties can be added upon successful registration or updates.Use `json-server-auth db.json` for straightforward setup. If you need fine-grained control over `json-server`'s internal configuration (e.g., custom routers, multiple middlewares), you might prefer a programmatic setup with `json-server` directly, explicitly importing and `app.use()`-ing `json-server-auth`.
Carefully review the documentation for the numeric permission system: the first digit for owner, second for logged-in users, third for public. '4' grants read, '2' grants write. Test your endpoints thoroughly with different user states (public, logged-in, resource owner) to ensure desired access control.
Run `npm install json-server json-server-auth` or `yarn add json-server json-server-auth` to ensure both packages are installed locally. If using `json-server -m`, verify the exact path to `node_modules/json-server-auth`.
Ensure your request includes an `Authorization: Bearer <accessToken>` header. Verify the access token is not expired and the user's role/ownership matches the resource's configured numeric authorization rules (e.g., `640`).
Ensure your POST request body for `/register` or `/login` contains valid `email` and `password` properties, formatted as `application/json`.
Use a unique email address for new user registration, or if the user is already created, attempt to log in instead of registering again.