Documentation for EmAuth.js SDK
Overview
The EmAuth.js SDK is a lightweight JavaScript library designed to facilitate user verification and data retrieval in a secure and streamlined manner. It is specifically built for applications requiring user authentication and access to specific user information using the EmAuth service.
Importing the SDK
To use the EmAuth.js SDK, include it in your HTML file:
<script src="EmAuth.js"></script>
To directly fetch it from our CDN, include it in your HTML file:
<script src="https://sdk.mukham.in/EmAuth.js"></script>
Core Features
The SDK provides two primary functions:
- User Verification: Verify if a user successfully authenticates for a given application.
- Data Retrieval: Fetch specific user data based on the application's requirements.
Functions
1. verifyUser(email_of_user, requester)
Description
This function verifies whether the user can authenticate themselves for the specified requesting application. It employs an asynchronous mechanism to provide seamless authentication.
Parameters
| Parameter | Type | Description |
|---|---|---|
email_of_user |
String | The email address of the user to be authenticated. |
requester |
String | The name of the application requesting the authentication. |
Return Value
- Type: Boolean
- Returns
trueif the user successfully authenticates for the application. - Returns
falseotherwise.
Usage Example
async function authenticateUser() {
const email = "user@example.com";
const appName = "MyApplication";
const isVerified = await verifyUser(email, appName);
if (isVerified) {
console.log("User authenticated successfully.");
} else {
console.log("Authentication failed.");
}
}
authenticateUser();
2. getUserData(email_of_user, requested_data, requester)
Description
This function retrieves specific data for the user, as requested by the application. The data is returned as a JSON string, where any requested image is encoded in Base64 format.
Parameters
| Parameter | Type | Description |
|---|---|---|
email_of_user |
String | The email address of the user whose data is being requested. |
requested_data |
Array of String | A list of requested data fields. Can include one or more of: 'Name', 'Date-Of-Birth', 'Image', 'IP'. |
requester |
String | The name of the application requesting the data. |
Return Value
- Type: JSON String
- A JSON string containing the requested user data. If the requested data includes an image, it is returned as a Base64 string.
Usage Example
async function fetchUserData() {
const email = "user@example.com";
const dataFields = ["Name", "Image"];
const appName = "MyApplication";
const userData = await getUserData(email, dataFields, appName);
console.log("User Data:", JSON.parse(userData));
}
fetchUserData();
Sample JSON Response
For a request for 'Name' and 'Image', the function might return:
{
"Name": "John Doe",
"Image": "data:image/png;base64,iVBORw0KGgoAAAANS..."
}
Error Handling
Both functions are asynchronous and should be handled using try-catch blocks to manage potential errors gracefully. Common errors might include:
- Invalid email format.
- Requesting data that the user has not consented to share.
- Network or server-side issues.
Example of Error Handling
async function secureFetch() {
try {
const email = "invalid_email";
const appName = "MyApplication";
const isVerified = await verifyUser(email, appName);
console.log("Verification Status:", isVerified);
} catch (error) {
console.error("Error during verification:", error.message);
}
}
secureFetch();
Security Considerations
- Email Validation: Ensure the email passed is in a valid format.
- Data Scope: Only request data that is absolutely necessary for the application's functionality.
- Base64 Image Handling: When processing Base64-encoded images, validate the integrity and size of the data to prevent memory issues.
- Encryption: Data exchanged with the SDK should occur over secure HTTPS connections. The SDK may fail to work if used in HTTP sites.