- Fork project
- Clone your fork:
git clone <link-to-project>
cd <project-directory>/
npm installThe app needs the following environment variables
- MONGO_URI=MongoDB connection string
Dev commands runs app with Node and the experimental flag --watch, you will need Node 18.11+
npm run devStart commands runs app with Node
npm startCheck the WBS CS Blog API.postman_collection file. You can import it in Postman to have an interface to the API. You will need to create an enviroment variable called WBS_CS_BLOG_API with value equal to the path where your backend API is running, e.g. http://localhost:8000
You are taske with implementing authentication in order to:
- Only allow blog posts creation to authenticated users
- Only allow blog post edition to authenticated users when the user is the owner of the post
- Only allow blog post deletion to authenticated users when the user is the owner of the post
- Reading endpoints on the post resource (all and single) are public
- You will need three endpoints:
--
POST/auth/signup => takes a body withfirstName,lastName,emailandpasswordand returns a JWT with the user ID as the payload --POST/auth/signin => takes a body withemailandpasswordand returns a JWT with the user ID as the payload --GET/auth/me => takes no body but anauthorizationheader is present, the value of said header is a valid JWT - Implement a
routerin theroutesdirector forauth - Implement a
controllerin thecontrollersdirectory for users - Implement an
Usermodel in themodelsdirectory - Implement a
verifyTokenmiddleware that will inspect theauthorizationheader of a request and validate a token. We will use this middleware to protect private routes! - You have some utilities at your disposal:
--
asyncHandler=> it takes an async function and follows the resolution of the promise, catches errors and passes them tonextif necessary --ErrorResponse=> a custom class that extends the nativeErrorclass, you can create errors with HTTP status codes and throw them for a cleaner error handling experience --validateJOI=> a custom middleware that takes a valid JOI schema for body validatation. You can check the available schemas atjoi/schemas.jsand a sample use case inroutes/postsRouter.js
- Hash the password using a library like
bcryptbefore inserting in the database! - Do NOT return the password in the
auth/meendopoint - You can return the token as a cookie or in the body and then store it in browser storage, both have their pros and cons