@cyphlens/2fa-sse-sdk
v2.2.0
Published
Cyphlens SDK for 2FA SSE
Readme
Cyphlens SSE Client SDK 🚀
A lightweight JavaScript library designed to simplify 2FA authentication with Cyphlens' Server-Sent Events (SSE) integration.
This SDK is bundled using Rollup and supports ES Modules (ESM), CommonJS (CJS), and UMD (Universal Module Definition) formats for maximum compatibility across environments.
Features ✨
- Easy integration with Cyphlens 2FA authentication
- Real-time event listening via Server-Sent Events (SSE)
- Lightweight and dependency-free
- Cross-environment support (Node.js, browsers, and modern frameworks)
Installation 📦
Using NPM
Install the SDK via npm:
npm install @cyphlens/2fa-sse-sdkUsing Yarn
Install the SDK via Yarn:
yarn add @cyphlens/2fa-sse-sdkUsing CDN (Browser)
Include the SDK directly in your HTML:
<script src="https://cdn.jsdelivr.net/npm/@cyphlens/2fa-sse-sdk@2/dist/bundle.min.js"></script>Usage 🔧
💡 Tip: Start with the Sandbox base URL for development and testing.
Importing the SDK
1. ES Modules (ESM) - Modern JavaScript
import { Cyphlens, EventType } from "@cyphlens/2fa-sse-sdk";2. CommonJS (CJS) - Node.js
const { Cyphlens, EventType } = require("@cyphlens/2fa-sse-sdk");3. Browser (IIFE)
After including the CDN script, the SDK is available as a global Cyphlens object:
const cyphlensClient = new Cyphlens("https://api.sandbox.cyphlens.com/b2b/v1");Example: Listening to 2FA Events
1. Initialize the Client
const baseUrl = "https://api.sandbox.cyphlens.com/b2b/v1"; // Use the correct Cyphlens base URL
const sessionId = "your-session-id"; // Replace with the current session ID
const cyphlensClient = new Cyphlens(baseUrl); // Initialize the Cyphlens client2. Listen for Events
// Define the callback for handling events
const onData = (eventType, data) => {
if (eventType === EventType.MFASwipe) {
const twoFactorEvent = data;
console.log("2FA Status:", twoFactorEvent.status);
// Handle status change (e.g., update UI or trigger logic)
}
};
const onError = (error) => {
console.error("SSE Error:", error);
// Handle the error (e.g., retry connection, notify user)
};
// Start listening to events
cyphlensClient.listen(sessionId, onData, onError);3. Stop Listening
// Stop the event listener when no longer needed
cyphlensClient.stop();API Reference 📚
new Cyphlens(baseUrl)
Creates a new instance of the Cyphlens client.
- baseUrl:
string— Cyphlens API base URL- Production:
https://api.prod.cyphlens.com/b2b/v1 - Sandbox:
https://api.sandbox.cyphlens.com/b2b/v1
- Production:
listen(sessionId, callback, errorCallback)
Starts listening for Server-Sent Events tied to the specified session.
- sessionId:
string- The session ID to listen for events - callback:
(eventType: EventType, data: any) => void- Callback function to handle incoming events - errorCallback:
(error: Event) => void- Callback function to handle errors (optional)
stop()
Stops the event listener and closes the connection.
Event Types
EventType.MFASwipe: Triggered when a 2FA swipe event occurs.- data:
{ status: string, ... }- Contains the event details, including the 2FA status.
- data:
Types
CyphlensErrorCallback:(error: Event) => void- Type definition for the error callback function.
Requirements ⚙️
- Node.js: v14.x or higher (for CommonJS/ESM usage)
- Browsers: Modern browsers with support for Server-Sent Events (SSE)
Contributing 🤝
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/YourFeature) - Commit your changes (
git commit -m "Add your feature") - Push to the branch (
git push origin feature/YourFeature) - Open a Pull Request
Maintainers
If you're looking for docs on maintaining this package, head over to docs/
Note:
• Ensure your local branch is up to date with origin/main (or your target branch) before tagging.
• You can verify the tag using git tag.License 📄
This project is licensed under the Cyphlens License. See the LICENSE file for details.
