ayan-biswas0412 / gssoc2021-HotelOnTouch

👨‍🔧👨‍🔧Manage your all hotel services at one place - This is the project repository for HotelOnTouch Project and this project is actively looking for new contributors👨‍🔧👩‍🏫
https://hotelontouch.netlify.app
GNU General Public License v3.0
70 stars 82 forks source link

Middleware for user authentication added #133

Closed singhayushh closed 3 years ago

singhayushh commented 3 years ago

Fixes #131

Changes done

Screenshots of the changes (If any) -

Note: Please check Allow edits from maintainers. if you would like us to assist in the PR.

✅️ By submitting this PR, I have verified the following

Note: Work was done on a new branch from PR #121 . Merging this PR will merge #121 automatically.

singhayushh commented 3 years ago

If you check the login route, whenever a user logs in, a jwt token with the user's email and _id encrypted is passed to the client side. Now, whenever there is any request from the client side, this token is passed along with the request in the header.

What this middleware does is it tries to access the token from the request header. If found, the code then checks for its validity. If the token is valid, the user's email and _id can be extracted, thereby verifying that the user was signed in. And we have all the information of who sent the request just by fetching details from the User collection by passing the above mail or the _id.

If the token isn't found or isn't valid, immediately a response is returned with corresponding message and the flow never reaches the route handler function.