UNPKG

rauth-provider

Version:

A lightweight, plug-and-play Node.js library for phone number authentication using the Rauth.io reverse verification flow via WhatsApp or SMS.

157 lines (141 loc) 4.75 kB
// Fetch polyfill for Node.js < 18 const fetch = globalThis.fetch || require('node-fetch'); /** * API Client for rauth.io integration */ class RauthApiClient { constructor(config) { this.config = config; this.baseUrl = 'https://api.rauth.io/session'; } /** * Initialize a session with rauth.io API * @param {Object} payload - Session initialization payload * @returns {Promise<Object>} Session initialization response with wa_link and qr_image_link */ async initSession(payload) { try { const response = await fetch(`${this.baseUrl}/init`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.config.rauth_api_key}`, 'X-App-ID': this.config.app_id, }, body: JSON.stringify(payload), }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(`Rauth API Error (${response.status}): ${errorData.message || response.statusText}`); } const data = await response.json(); // New API response format: // { // session_token: "api-generated-token", // wa_link: "https://wa.me/918888888888?text=fhad-dfsfd-eqwt-l4dt-lueb", // qr_image_link: "https://cdn.rauth.io/qr/15523456.png", // } return data; } catch (error) { if (error.name === 'TypeError' && error.message.includes('fetch')) { throw new Error('Failed to connect to Rauth API. Please check your internet connection.'); } throw error; } } /** * Get session details from rauth.io API * @param {string} sessionToken - Session token to query * @returns {Promise<Object|null>} Session details or null if not found */ async getSessionDetails(sessionToken) { try { const response = await fetch(`${this.baseUrl}/status`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.config.rauth_api_key}`, 'X-App-ID': this.config.app_id, }, body: JSON.stringify({ session_token: sessionToken, }), }); if (response.status === 404) { return null; // Session not found } if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(`Rauth API Error (${response.status}): ${errorData.message || response.statusText}`); } const data = await response.json(); return data; } catch (error) { if (error.name === 'TypeError' && error.message.includes('fetch')) { throw new Error('Failed to connect to Rauth API. Please check your internet connection.'); } throw error; } } /** * Verify session status with rauth.io API * @param {string} sessionToken - Session token to verify * @returns {Promise<Object|null>} Verification status or null if not found */ async verifySession(sessionToken) { try { const sessionDetails = await this.getSessionDetails(sessionToken); if (!sessionDetails) { return null; } // Check if session is verified if (sessionDetails.status === 'verified') { return { verified: true, phone: sessionDetails.phone, timestamp: sessionDetails.timestamp, ttl: sessionDetails.ttl || 900, // Default 15 minutes }; } // Check if session is revoked if (sessionDetails.status === 'revoked') { return { verified: false, revoked: true, reason: sessionDetails.reason || 'revoked', timestamp: sessionDetails.timestamp, ttl: sessionDetails.ttl || 3600, // Default 1 hour }; } // Session exists but not verified yet return { verified: false, revoked: false, status: sessionDetails.status, phone: sessionDetails.phone, timestamp: sessionDetails.timestamp, }; } catch (error) { throw error; } } /** * Check if API is reachable * @returns {Promise<boolean>} True if API is reachable */ async healthCheck() { try { const response = await fetch(`${this.baseUrl}/health`, { method: 'GET', headers: { 'Authorization': `Bearer ${this.config.rauth_api_key}`, 'X-App-ID': this.config.app_id, }, }); return response.ok; } catch (error) { return false; } } } module.exports = RauthApiClient;