summaryrefslogtreecommitdiff
path: root/examples/generate-uri.ts
blob: 27d54019db067116c8adc4f83692788e0db8b202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// examples/generate-uri.ts

/**
 * Example: Generating a Digi-ID URI
 * 
 * This example demonstrates how to generate a Digi-ID URI that can be displayed as a QR code
 * for users to scan with their Digi-ID compatible wallet.
 */

// Import directly from src for running locally before publishing
// In a real project, you'd import from 'digiid-ts' after installing
// Revert extension, ts-node should handle this when configured
import { DigiIDError, generateDigiIDUri } from '../src/index';

console.log('--- DigiID URI Generation Example ---');

// --- Example 1: Secure Callback (HTTPS) ---
const secureOptions = {
  callbackUrl: 'https://myapp.example.com/api/auth/digiid',
};

try {
  const secureUri = generateDigiIDUri(secureOptions);
  console.log('\nSecure URI (HTTPS):');
  console.log(`  Callback: ${secureOptions.callbackUrl}`);
  console.log(`  Generated: ${secureUri}`);
  // Typically, you would now generate a QR code from secureUri
} catch (error) {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  console.error('Error generating secure URI:', (error as Error).message);
}

// --- Example 2: Unsecure Callback (HTTP) for Testing ---
const unsecureOptions = {
  callbackUrl: 'http://localhost:8080/dev/callback',
  unsecure: true, // Must set this flag for http
};

try {
  const unsecureUri = generateDigiIDUri(unsecureOptions);
  console.log('\nUnsecure URI (HTTP) for testing:');
  console.log(`  Callback: ${unsecureOptions.callbackUrl}`);
  console.log(`  Generated: ${unsecureUri}`);
} catch (error) {
  console.error('Error generating unsecure URI:', (error as Error).message);
}

// --- Example 3: Providing a Custom Nonce ---
const customNonceOptions = {
  callbackUrl: 'https://anotherapp.com/verify',
  nonce: 'my-unique-secret-nonce-per-request-12345'
};

try {
  const customNonceUri = generateDigiIDUri(customNonceOptions);
  console.log('\nURI with Custom Nonce:');
  console.log(`  Callback: ${customNonceOptions.callbackUrl}`);
  console.log(`  Nonce:    ${customNonceOptions.nonce}`);
  console.log(`  Generated: ${customNonceUri}`);
} catch (error) {
  console.error('Error generating URI with custom nonce:', (error as Error).message);
}

// --- Example 4: Invalid URL (Missing Scheme) ---
const invalidUrlOptions = {
  callbackUrl: 'myapi.com/auth' // Missing https://
};

console.log('\nAttempting URI with Invalid URL (expecting error):');
try {
  generateDigiIDUri(invalidUrlOptions);
} catch (error) {
  if (error instanceof DigiIDError) {
    console.log(`  Caught expected DigiIDError: ${error.message}`);
  } else {
    console.error('  Caught unexpected error:', error);
  }
}

console.log('\n--- End of Generation Example ---');