syncCleverUser()
syncCleverUser, getCleverUser
Function Name
syncCleverUser
1. syncCleverUser
This callable Cloud Function acts as an entry point for client applications to initiate the synchronization process of user data from Clever.
Trigger
- Type: Callable function.
- Authentication: Requires users to be authenticated; accesses the user's UID through
request.auth.uid
.
Parameters
- request: Contains the necessary credentials and identifiers to fetch and synchronize user data:
assessmentUid
: UID used within the assessment Firebase project.accessToken
: Access token for authenticating requests to the Clever API.
Operation
- Extracts the authenticated user's UID and other necessary parameters from the request.
- Calls the
getCleverUser
function to perform the actual data synchronization process.
2. getCleverUser
A backend utility function that handles the actual interaction with the Clever API, retrieves user data, and updates Firebase Authentication and Firestore accordingly.
Parameters
- roarUid: ROAR-specific user identifier.
- adminUid: Administrator UID in the Firebase admin project.
- assessmentUid: UID used within the assessment Firebase project.
- accessToken: Token used to authenticate requests to the Clever API.
Operation
- Configures API requests and fetches user data from Clever.
- Processes the retrieved data to update Firebase Authentication custom claims and Firestore documents across both admin and assessment projects.
Detailed Workflow
Authentication and API Setup:
- Configures Axios for API requests to Clever using the provided
accessToken
. - Establishes Firebase and Firestore instances for necessary database interactions.
- Configures Axios for API requests to Clever using the provided
Data Retrieval from Clever:
- Fetches user data from Clever using the
/me
endpoint and follows links to specific user resources. - Extracts and logs user data for further processing.
- Fetches user data from Clever using the
Firebase Data Processing:
- Runs transactions to ensure atomic updates across Firebase services.
- Updates custom claims and Firestore documents to reflect the latest user data from Clever.
- Conditionally handles data based on whether the operation pertains to the admin or assessment database.
Role-Specific Data Handling:
- Processes user roles from Clever to update Firebase data accordingly, utilizing role mappings and custom logic to handle administrative and educational data specifically.
- Uses transactions to update Firestore documents, ensuring data consistency and reliability.
Error Handling and Security Considerations
Error Handling:
- Implements comprehensive error handling throughout the API interaction and data processing stages.
- Catches and logs errors related to API failures, Firebase operations, and data integrity issues.
Security:
- Ensures all operations are performed under strict authentication checks to prevent unauthorized access.
- Validates and sanitizes incoming data from Clever to prevent injection attacks and ensure data integrity.
Deployment and Maintenance
This function is deployed automatically using GitHub actions, whenever changes are merged into the main
branch.