Auth Middleware
/api/v2/
users
jwt
middleware
Purpose
Carries out essential validation of most routes to secure the whole API.
What the Middleware does
- Checks if the request has a
headerand abody content. - Checks if the body has a
uidfield [400]. - Gets the
Authorization Bearertoken from theheaders[401]. - Verify if the
JWTis valid using the same secret key that was used to generate it with the same original algorithm. - Checks if the
payloadof theJWTcontains anisspart [401]. - Finds the record for the
userwhoseuidcorresponds to that in thebodyfrom theuserscollection [404]. - Checks if that
userrecord has the same value for itsprev_emailfield as that obtained from theisspart of thepayload[401]. - If there was a field named
old_passwordpassed in the body of that request, checks if the value in theold_passwordfield is valid by hashing it and comparing it to the one in the fetched record from theuserscollection [401]. - Checks if the
userrecord fetched has a value oftruefor itsactivefield [401].