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
header
and abody content
. - Checks if the body has a
uid
field [400]. - Gets the
Authorization Bearer
token from theheaders
[401]. - Verify if the
JWT
is valid using the same secret key that was used to generate it with the same original algorithm. - Checks if the
payload
of theJWT
contains aniss
part [401]. - Finds the record for the
user
whoseuid
corresponds to that in thebody
from theusers
collection [404]. - Checks if that
user
record has the same value for itsprev_email
field as that obtained from theiss
part of thepayload
[401]. - If there was a field named
old_password
passed in the body of that request, checks if the value in theold_password
field is valid by hashing it and comparing it to the one in the fetched record from theusers
collection [401]. - Checks if the
user
record fetched has a value oftrue
for itsactive
field [401].