The Mdk SDK provides functionalities to support user authentication using JWT tokens. This SDK is designed to integrate with React applications, enabling you to manage login sessions, validate tokens, and handle user logout.
-
Install the
@interlinklabs/mdklibrary:npm install @interlinklabs/mdk
-
Import the Mdk SDK into your project from the corresponding file.
Integrate the SDK by using the Mdk component in your application.
import React from "react";
import Mdk from "@interlinklabs/mdk";
const App = () => {
const handleSuccess = () => {
console.log("Login successful!");
};
const handleFailure = () => {
console.log("Login failed.");
};
return (
<Mdk
appid="your-app-id"
onSuccess={handleSuccess}
onFailure={handleFailure}
>
{({ open }) => <button onClick={open}>Login with the app</button>}
</Mdk>
);
};
export default App;appid(string): Your application ID for identification.onSuccess(function): A callback function triggered upon successful login.onFailure(function): A callback function triggered upon login failure.children(function): Renders UI components with the{ open }property to initiate the login process.
- Description: Retrieves the loginId from the JWT stored in cookies.
- Usage:
const loginId = Mdk.getLoginId("your-app-id"); console.log("Login ID:", loginId);
- Description: Fetches the username associated with a given loginId.
- Usage:
const username = await Mdk.getUserName("your-app-id"); console.log("Username:", username);
- Description: Deletes the JWT token and logs out the user.
- Usage:
Mdk.logOut("your-app-id"); console.log("Logged out!");
- Login Request: The user clicks the login button (triggered by the open method).
- Message Sent: A message containing the appid is sent to the native app.
- Native App Response: The native app responds with the status (pass or fail).
- Token Validation: The SDK validates the JWT token:
- If valid: The token is stored in cookies, and the onSuccess function is called.
- If invalid: The token is deleted, and the onFailure function is called.
A sample application demonstrating the integration of the Mdk SDK is available on GitHub: AimShoota.