Skip to content

Dev Manual: backend_requests

Samyak Mishra edited this page Jun 12, 2024 · 4 revisions

This directory contains code files for interacting with the app backend, which is a python server available publicly (https://ims-app.iiit.ac.in), which in turn queries the database of the Institute Management System (IMS), only available on our institute network.

1. AccessToken.tsx

This file contains the default exported function getAccessToken(), that retrieves the access token field from the cookie fetched from the app backend. This returned access token is used by all the app components to send to the backend as part of each request in order to authenticate the user, thus ensuring security.

2. AuthUser.tsx

This file contains the default exported function authenticate_user(), which uses the email and password entered during login (it is used in the login screen) and attempts to authenticate the user.

It does so by the following.

  • packages the username and password into a FormData object
  • sets a timer for timeout
  • makes a POST request to the authentication API with the FormData object
  • processes the response based on the status received, doing error handling as well
  • if authentication is successful, assigns the cookie and locally stores the current date for cookie expiry checking (done in the welcome screen)

This function is used in the login screen after the user presses the button after entering their email and password.

3. CookieManage.tsx

This file exports the function getCookie(), which takes in a request object (which would've been received from a request to the backend, either for authentication or for extending cookie validity), and returns a boolean as a promise, denoting the successful receival, verification and assignment of the cookie to the user. It parses the response object's cookie field to get the value of the authentication token, and creates a cookie from that. It then matches it against a cookie received from the backend for verifying the user, and if so returns true, denoting a successful verification.

This function is used in AuthUser.tsx and RefreshToken.tsx.

4. PostLeave.tsx

This file has code to send leave request applications to the backend. The main exported function postLeaveToServer() takes in the JSON object we wish to send (see the API format), and first checks if the access token is available. If so, it calls the auxiliary function sendData(), which makes a POST request to the leave application API endpoint, sending it the JSON object along with the access token. It then parses the response, and if it was successfully received, it sends a push notification indicating successful application of the leave request.

postLeaveToServer() is used in the Leave Application screen to make the POST request once the user presses the submit button.

5. RefreshToken.tsx

This file exports the function extend_cookie(), whose purpose is to extend the user's cookie. It first gets the current access token, which it then uses to make a POST request to the cookie extension API. If the request doesn't time out, the response is checked for the extension status. If successful, a new cookie is created using the response and the current date is stored as the latest login date, to be checked for the cookie's expiry (done in the welcome screen).

This function is used in the welcome screen, where the cookie is extended if it has been more than 20 days since it was last renewed. Then the user is directed to the dashboard without any need for them to log in again.

6. UserDetails.tsx

This file exports the function get_user_details(), whose job is to set four variables that are used by the app to get basic information about the user, namely userMail, userName, rollno, and userType (student, employee or staff). It first obtains the access token, uses it to make a GET request to the corresponding API. If the request doesn't time out, it sets these variable according to the fields in the response, and the variables are then exported.

get_user_details() is used in the welcome and login screens for use in their logic, and the four variables are used in various components of the app as well.