- Authentication
- JSON Web Tokens (JWTs)
- Express Middleware
- Mongoose Middleware
- Hashing Passwords
In this project we'll implement a full authentication workflow (register/login/logout/restrict endpoint) using Node.js, Express, MongoDB and JSON Web Tokens on the server and a React Web Application for the client.
- Fok and Clone this repository.
- CD into the server folder.
- type
yarn installto download the server dependencies. - type
yarn startto run the server, and leave it running. - In a separate terminal window, CD into the client folder.
- type
yarn installto download the server dependencies. - type
yarn startto execute the client application. - Ensure that you have an instance of
MongoDBrunning.
- inspect the code to figure out which routes are already implemented and where you need to make changes to add support for JWTs. This will help you practice reading existing code.
- implement the authentication workflow (register and login) on the server using JSON Web Tokens.
- add the functionality to restrict access to
/api/usersto authenticated users only. If a non authenticated user tries to make a request the server should return the appropriateHTTP status code. - implement a React client:
- add client side routes and components for
signup,signinand showing thelist of usersstored in the database. - the
/signuproute should provide a form to gatherusername,passwordandracefor the user and make aPOSTrequest to the/api/auth/registerroute on the API. If the user is created successfully, take the returned token, save it to the browser's local storage and redirect the user to the/usersroute, where they should see the list of users. - the
/signinroute should provide a form to gatherusernameandpasswordfor the user and make aPOSTrequest to the/api/auth/loginroute on the API. Upon successful login, persist the returned token to the browser's local storage and redirect the user to the/usersroute. - the
/usersroute should read the token from local storage and make aGETrequest to the/api/usersroute on the API attaching the token as the value of theAuthorizationheader. - provide a button to
sign outthat will remove the token from local storage.
- add client side routes and components for
- add the code necessary so that when a client makes a
GETrequest to/api/usersthe server only returns documents with thesame raceas the logged in user. For example if the logged in user is a human, then only users of the human race should be returned; if the logged in user is a hobbit only the hobbits should be returned. - add any extra functionality to make the application more user friendly like showing a message and redirecting to
/signinif an unauthenticated user tries to access the list of users in the/usersroute.