This extension requires completion of the cinema-booking-api repo of the API module.
- Fork this repository and clone the fork to your machine.
- Choose Option 1 or Option 2 below to copy your existing work over:
- Copy all of the files and folders except
README.md
and./node_modules/
into this new repo - Run
npm ci
to install dependencies
- Add a new remote to your local repo that points to your
cinema-booking-api
repository, giving it the name upstream:git remote add upstream THE_URL_TO_THE_REPO
- Fetch data from the upstream repo:
git fetch upstream
- Merge in the code from upstream:
git rebase upstream/main
- Copy over your
.env
file from your local cinema-booking-api repo - Run
npm ci
to install dependencies
-
Change the
Customer
model to have a password and a username field.- Remember to create a new migration, update the seed file and apply the migration with the new seed data
-
Create a route to allow customers to create their account.
- Passwords must be hashed
-
Create a route to allow customers to login.
- The route should create a JSON web token which is sent back to the client in the response.
-
Create an express middleware function that checks and validates a JSON web token from the request's
authorization
header and attach this to every route that creates, updates or deletes data.- If the token is invalid or not present, a response with a 401 status code should be sent back to the client
- If the token is valid, you should add it to the
req
object (e.g.req.token = token
) beforenext()
is called. This will make sure the token is attached to the request object in every function that gets called after your middleware, which will be useful later.
-
Now that you have authentication hooked up, it's time to consider permissions. Should a customer be able to create new screenings for movies, or is that the role of a manager? This will require some refactoring, a very common part of development!
- Rename the
Customer
model toUser
and update every reference appropriately. e.g. relations that rely on acustomerId
should becomeuserId
, prisma queries should be updated to useuser
instead ofcustomer
. Remember to create a new migration and re-seed your database! - Use this documentation on Prisma Enums to add a
role
field to theUser
model. Instead ofUSER
andADMIN
, the enum values should beCUSTOMER
andMANAGER
. Remember to migrate! - Update your
login
route so it adds the users role to the payload of the JWT. - Create a new express middleware function that decodes the token attached to the request object and checks whether the users role is
MANAGER
.- If the user is a
MANAGER
, call the next function to move the request forward. - If not, send a 401 status response.
- If the user is a
- Attach this new middleware to any routes that a customer should not have access to (anything that deletes, creates or updates movie / screening / screen data). This middleware should be placed after the JWT checking middleware and before the route's controller. Example:
router.post('/movie', checkToken, checkIsManager, createMovieController);
- Rename the