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.

129 lines (113 loc) 3.77 kB
/** * Simple test to demonstrate the new API response format * * This script shows how to use the updated RauthProvider library * with the new `/init` endpoint response format. */ const { RauthProvider } = require('./index'); console.log('RauthProvider loaded:', RauthProvider ? '✅' : '❌'); // Mock API response for demonstration purposes const mockApiResponse = { session_token: 'test-session-token-123', wa_link: 'https://wa.me/918888888888?text=fhad-dfsfd-eqwt-l4dt-lueb', qr_image_link: 'https://cdn.rauth.io/qr/15523456.png', }; // Initialize the provider first console.log('Initializing RauthProvider...'); try { RauthProvider.init({ rauth_api_key: 'test-api-key', app_id: 'test-app-id', webhook_secret: 'test-webhook-secret', }); console.log('RauthProvider initialized successfully'); } catch (error) { console.error('Failed to initialize RauthProvider:', error.message); process.exit(1); } // Then mock the API client's initSession method console.log('Setting up mock API client...'); const originalApiClient = RauthProvider.apiClient; RauthProvider.apiClient = { initSession: async () => { console.log('Mock initSession called'); return mockApiResponse; }, healthCheck: async () => true, verifySession: async () => null, getSessionDetails: async () => null, }; // Simulate a session initialization request async function testInitSession() { try { console.log('\nTesting initSession with new API response format...\n'); // Simulate HTTP headers const mockHeaders = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', 'x-forwarded-for': '192.168.1.1', }; console.log('Calling RauthProvider.initSession...'); // Call initSession const response = await RauthProvider.initSession('+1234567890', mockHeaders); // Log the response console.log('✅ Session initialization successful!'); console.log('\nAPI Response:'); console.log(JSON.stringify(response, null, 2)); // Example usage in an Express.js route console.log('\nExample usage in an Express.js route:'); console.log(` app.post('/api/login/init', async (req, res) => { try { const { phone } = req.body; const initResult = await RauthProvider.initSession(phone, req.headers); // Forward the response to the client res.json({ success: true, ...initResult }); } catch (error) { res.status(400).json({ success: false, error: error.message }); } });`); console.log('\nClient-side handling example:'); console.log(` // React component example function PhoneVerification({ initResult }) { return ( <div className="verification-options"> <h2>Verify your phone number</h2> <div className="qr-code"> <img src={initResult.qr_image_link} alt="WhatsApp QR Code" /> </div> <div className="or-divider">OR</div> <a href={initResult.wa_link} className="whatsapp-button" target="_blank" rel="noopener noreferrer" > Click to verify via WhatsApp </a> <div className="session-info"> <p>Your session token: {initResult.session_token}</p> </div> </div> ); } `); } catch (error) { console.error('❌ Error:', error.message); console.error(error); } finally { // Restore original API client RauthProvider.apiClient = originalApiClient; } } // Run the test console.log('Running test...'); testInitSession().then(() => { console.log('\n✅ Test completed successfully!'); });