This type of header passes to the Organization API protected routes, header looks like this:
"Authorization": "Bearer access_token_value"
- It is a middleware function used to protect API endpoints from unauthorized access
- Function checks access_token cookie existence in the request
- It is a middleware function used to protect API endpoints from unauthorized access
- Function checks Authorization header existence
- Function decodes access_token
- Function checks user existence from access_token payload
- Function verifies refresh_token from DB
- Function passes access_token payload in "req.user" object
- Function accepts two parameters: user_type (consumer or driver), id (consumer_id or driver_id)
- Function signs access_token and refresh_token, both with payload:
{
"user_type": "driver",
"id": "<driver_id>"
}
- Function returns access_token and refresh_token
Request body:
{
"username": "John Doe",
"password": "98787r623",
"password_confirm": "98787r623",
"user_email": "hello@email.com",
"location": "helsinki"
}
Response object:
{
"status": "success"
}
- Function compares password and password_confirm
- Function calls Organization API endpoint /consumer/signup
- Function checks if Organization API response successful
- Function sets access_token cookie to a user
Request body:
{
"user_email": "example@email.com",
"password": "hello1234"
}
Response object:
{
"status": "success"
}
- Function calls Organization API endpoint /consumer/login
- Function checks if Organization API response successful
- Function sets access_token cookie to a user
Response object:
{
"status": "success",
"username": "<username>",
"notifications": [
{
"parcel_id": "<parcel_id>",
"title": "<notification_title>",
"status": "<parcel_status>"
},...
]
}
- This route is protected
- Function passes the access_token from a cookie to a request Authorization header for calling /me route in Organization API
- Function checks if Organization API response successful
- Function responds with the response of Organization API response
Request body:
{
"driver_email": "example@email.com",
"password": "hello1234"
}
Response object:
{
"status": "success"
}
- Function checks for driver existence
- Function compares hashed passwords from a DB and from driver input
- Function signs access_token and refresh_token
- Function updates user refresh_token in DB
- Function sets access_token cookie to a driver
Response object:
{
"status": "success",
"username": "<username>"
}
- This route is protected
- Function passes the access_token from a cookie to a request Authorization header for calling /me route in Organization API
- Function checks if Organization API response successful
- Function responds with the response of Organization API response
Request body:
{
"type": "<either 'delivery' or 'pickup'>",
"pin": 12345,
"cabinet_location": "helsinki"
}
Response object:
{
"status": "success",
"message": "<Organization API message>"
}
- Function checks if all the required fields (type, pin, location) are present in request body
- Dependingly on request type (delivery, pickup) function calls Organization API endpoint /cabinet/delivery (for type delivery) or /cabinet/pickup (for type pickup)
- Function checks if Organization API response successful
- Function sends Organization API message
Request body:
{
"username": "John Doe",
"password": "98787r623",
"email": "hello@email.com",
"location": "helsinki"
}
Response object:
{
"status": "success",
"access_token": "token_value"
}
- Function checks if user with the same email already exists
- Function encrypts user password
- Function signs access_token and refresh_token
- Function creates a new user in "users" table including refresh_token
- Function returns access_token in response object
Request body:
{
"user_email": "example@email.com",
"password": "hello1234"
}
Response object:
{
"status": "success",
"access_token": "token_value"
}
- Function checks for user existence
- Function compares hashed passwords from a DB and from user input
- Function signs access_token and refresh_token
- Function updates user refresh_token in DB
- Function returns access_token in response object
Request body:
{
"pin": 12345,
"location": "helsinki"
}
Response object:
{
"status": "success",
"message": "Parcel delivered."
}
- Function looks through parcels if delivery_pin of any parcel matches pin passed into request
- Function checks if location of the cabinet is the same as location from where request passed
- Function creates timestamp object:
{
"timestamp": "time",
"status": "<one of three statuses described in next three steps>"
}
- Function checks if location of the cabinet is "warehouse", then parcel status changes to "at warehouse"
- Function checks if location of the cabinet is the same as location of the sender, then parcel status changes to "prepared for delivery"
- Function checks if location of the cabinet is the same as location of the reciever, then parcel status changes to "ready for pick up", and function generates pickup_pin for the parcel
- Function populates parcel status_timestamps array with new timestamp object
- Function sets cabinet status to "occupied" and sets parcel_id in cabinets' parcel_id field
- Function responds with message of the operation
Request body:
{
"pin": 12345,
"location": "helsinki"
}
Response object:
{
"status": "success",
"message": "Parcel picked up."
}
- Function looks through parcels if pickup_pin of any parcel matches pin passed into a request
- Function checks if location of the cabinet is the same as location from where request passed
- Function creates timestamp object:
{
"timestamp": "time",
"status": "<one of three statuses described in next three steps>"
}
- Function checks if location of the cabinet is "warehouse" or same as location of the sender, then parcel status changes to "en route", and function generates delivery_pin for the parcel
- Function checks if location of the cabinet is the same as location of the reciever, then parcel status changes to "delivered"
- Function populates parcel status_timestamps array with new timestamp object
- Function sets cabinet status to "free" and sets cabinet parcel_id field to null
- Function responds with message of the operation
Request headers:
{
"Authorization": "Bearer <access_token_value>"
}
Response object (user_type = consumer):
{
"status": "success",
"username": "<username>",
"notifications": [
{
"parcel_id": "<parcel_id>",
"title": "<notification_title>",
"status": "<parcel_status>"
},...
]
}
Response object (user_type = driver):
{
"status": "success",
"username": "<username>"
}
- This route is protected
- Function gets "user_type" and "id" of a user requesting this route in req.user object
- Function checks if "user_type" is consumer, then it gets the actual username as well as user parcels where "notify" field is set to "true" from a DB
- Function checks if "use_type" is driver, then it gets the actual username from a DB
- Function returns username in both scenarios, but it also returns notifications if "user_type" is consumer