This exercise will assess your ability to build a fully working API with MongoDB that includes authentication and authorization.
-
Fork & clone
-
cp nodemon.sample.json nodemon.json -
Create a new Cluster on MongoDB Atlas titled something like
general-purposeor reuse one you already have. -
Update the
MONGO_DB_CONNECTIONin yournodemon.jsonfile with the full connection string. Make sure you include the password you set up for the database and change the name of the database fromtestto something lkelibrary_access_dev. -
npm install -
npm run reset-db -
npm run dev
Currently, this repository includes an API with the following routes:
Retrieve all books.
Retrieve a specific book by id.
Create a new book.
Reserve a book.
To complete this exercise, you will need to do the following:
-
Create a User Model: Users have a
username, apassword, and anadminproperty which is set tofalseby default. -
Create a
POST /api/signuproute: Create a new route that allows someone to create an account. Securely store the password using thebcryptpackage. On successful creation, return a JWT token. You should return an error in the following cases:- Username is not provided
- Username is already taken
- Password is not provided
- Password is less than 8 characters
-
Create a
POST /api/loginroute: Create a new route that allows someone to login. On successful creation, return a JWT token. You should return an error in the following cases:- Username is not found
- Username and password do not match
-
Add an admin User and a regular user to the
./db/seeds.jsfile: In theseeds.jsfile, when thereset()function is run, create a new User who has admin permissions and another User without admin permissions. Make sure that both users will be deleted and then recreated whenever the function is run. -
Create a
PATCH /api/users/:id/permissionsroute: Create a new route that allows for an admin to change permissions of another user. The route should only be looking for theadmin: <boolean>key in the request body and setting the value appropriately. On success, return a status 204. You should return an error in the following cases:- A valid JWT token is not provided (status 401)
- The JWT token is for a user who is not an admin (status 401)
- User cannot be found (status 404)
- The request body does not include an
adminkey with a boolean value (status 400)
-
Update the
POST /api/booksroute: This route should only be available to users who are admins. If the user is an admin, proceed as normal. If they are not an admin, return an error message with a status code of 401. -
Update the
POST /api/books/:id/reserveroute: This route allows for someone to reserve a book. If the user is logged in, proceed as normal. You should return an error in the following cases:- A valid JWT token is not provided (status 401)
- The book is already reserved (status 400)
- Book cannot be found (status 404)
-
Create a
PATCH /api/books/:id/returnroute: This route should return a book if the user has already reserved it. If the appropriate user is returning the book, set thereserved.statustofalseand update thereserved.memberIdto benull. You should return an error in the following cases:- A valid JWT token is not provided (status 401)
- The book is reserved by a different user (status 401)
- The book is not reserved (status 400)
- Book cannot be found (status 404)