โœจWorks out of the box guarantee. If you face any issue at all, hit us up on Telegram and we will write the integration for you.
logoReclaim Protocol Docs

Frontend Example with React

This guide will walk you through the fundamental steps to integrate Reclaim's proof verification system into your React application.

Prerequisites

Before implementing Reclaim verification in your React application, ensure you have:

  • Successfully installed the Reclaim SDK (refer to the Installation Guide)
  • Obtained the following credentials for initializing your Application from Reclaim Developer Portal:
    • Application ID
    • Application Secret
    • Provider ID

โš ๏ธ Security Warning: Using your Application Secret in client-side code should be limited to prototyping only. For production environments, always store your secret securely on a backend server and never expose it in frontend code.

Implementation Guide

The following example demonstrates how to create a React component that implements Reclaim verification with QR code functionality:

import { useState } from 'react';
import QRCode from 'react-qr-code';
import { ReclaimProofRequest } from '@reclaimprotocol/js-sdk';
 
function ReclaimDemo() {
  // State to store the verification request URL
  const [requestUrl, setRequestUrl] = useState('');
  const [proofs, setProofs] = useState([]);
 
  const getVerificationReq = async () => {
    // Your credentials from the Reclaim Developer Portal
    // Replace these with your actual credentials
    const APP_ID = 'YOUR_APPLICATION_ID';
    const APP_SECRET = 'YOUR_APPLICATION_SECRET';
    const PROVIDER_ID = 'YOUR_PROVIDER_ID';
 
    // Initialize the Reclaim SDK with your credentials
    const reclaimProofRequest = await ReclaimProofRequest.init(APP_ID, APP_SECRET, PROVIDER_ID);
 
    // Generate the verification request URL
    const requestUrl = await reclaimProofRequest.getRequestUrl();
    console.log('Request URL:', requestUrl);
    setRequestUrl(requestUrl);
 
    // Start listening for proof submissions
    await reclaimProofRequest.startSession({
      // Called when the user successfully completes the verification
      onSuccess: (proofs) => {
        if (proofs) {
          if (typeof proofs === 'string') {
            // When using a custom callback url, the proof is returned to the callback url and we get a message instead of a proof
            console.log('SDK Message:', proofs);
            setProofs([proofs]);
          } else if (typeof proofs !== 'string') {
            // When using the default callback url, we get a proof object in the response
            if (Array.isArray(proofs)) {
              // when using provider with multiple proofs, we get an array of proofs
              console.log('Verification success', JSON.stringify(proofs.map(p => p.claimData.context)));
              setProofs(proofs);
            } else {
              // when using provider with a single proof, we get a single proof object
              console.log('Verification success', proofs?.claimData.context);
              setProofs(proofs);
            }
          }
        }
        // Add your success logic here, such as:
        // - Updating UI to show verification success
        // - Storing verification status
        // - Redirecting to another page
      },
      // Called if there's an error during verification
      onError: (error) => {
        console.error('Verification failed', error);
 
        // Add your error handling logic here, such as:
        // - Showing error message to user
        // - Resetting verification state
        // - Offering retry options
      },
    });
  };
 
  return (
    <>
      <button onClick={getVerificationReq}>Get Verification Request</button>
      {/* Display QR code when URL is available */}
      {requestUrl && (
        <div style={{ margin: '20px 0' }}>
          <QRCode value={requestUrl} />
        </div>
      )}
      {proofs && (
        <div>
          <h2>Verification Successful!</h2>
          <pre>{JSON.stringify(proofs, null, 2)}</pre>
        </div>
      )}
    </>
  );
}
 
export default ReclaimDemo;

Implementation Breakdown

1. Component Structure

  • Manages state for verification URL and proofs
  • Handles verification requests
  • Renders user interface elements

2. Verification Flow

  • Initializes SDK with credentials on button click
  • Generates and stores request URL
  • Monitors for proof submissions
  • Processes success/error states

3. Response Types

  • String: When using custom callback URLs
  • Object: When using default callback handling

Integration Steps

  1. Component Integration
import ReclaimDemo from './ReclaimDemo';
 
function App() {
  return (
    <div>
      <h1>Reclaim Verification</h1>
      <ReclaimDemo />
    </div>
  );
}
  1. Environment Setup
  • Store your credentials securely
  • Configure error handling
  • Set up appropriate loading states

๐Ÿ’ก Device Detection Tip: Consider implementing device detection to optimize the user experience:

  • Desktop: Display QR code for mobile scanning
  • Mobile: Show direct verification link or better yet, use the appClip/Instant App flow from Advance Options

๐Ÿ”’ Production Security Requirements:

  1. Never expose APP_SECRET in frontend code
  2. Generate request configurations server-side as shown in Fullstack Example
  3. Use fromJsonString() method in frontend code to initialize the SDK
  4. Implement proper error handling and validation

Refer to the Fullstack Example for secure implementation details.

Next Steps

After implementing the basic verification:

  1. Advance Options

    • Explore Advance Options for more fine-tuned customization of your implementation
  2. Testing

    • Test across different devices and browsers
    • Verify error scenarios and validate security measures

Support Resources

Remember to regularly update your implementation as new SDK versions are released to ensure optimal security and performance.

On this page