|
1 | 1 | /** |
2 | | - * Phone Number Pairing utilities |
3 | | - * Alternative authentication method using pairing codes |
| 2 | + * Phone Number Pairing and QR Code utilities |
| 3 | + * Alternative authentication methods for WhatsApp sessions |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import { getClient } from "~/client" |
7 | 7 | import { debugLog } from "~/utils/debug" |
8 | 8 |
|
| 9 | +/** |
| 10 | + * Get QR code as raw data from WAHA API |
| 11 | + */ |
| 12 | +export async function getQRCode(sessionName: string): Promise<string | null> { |
| 13 | + try { |
| 14 | + debugLog("QR", `Fetching QR code for session: ${sessionName}`) |
| 15 | + const client = getClient() |
| 16 | + |
| 17 | + const { data } = await client.pairing.authControllerGetQr(sessionName, { |
| 18 | + format: "raw", |
| 19 | + }) |
| 20 | + |
| 21 | + if (typeof data === "object" && data && "value" in data) { |
| 22 | + debugLog("QR", `QR code fetched successfully for ${sessionName}`) |
| 23 | + return (data as { value: string }).value |
| 24 | + } |
| 25 | + |
| 26 | + debugLog("QR", `No QR value in response for ${sessionName}`) |
| 27 | + return null |
| 28 | + } catch (error) { |
| 29 | + debugLog("QR", `Failed to fetch QR code: ${error}`) |
| 30 | + return null |
| 31 | + } |
| 32 | +} |
| 33 | + |
9 | 34 | export interface PairingResult { |
10 | 35 | success: boolean |
11 | 36 | code?: string |
@@ -38,7 +63,7 @@ export async function requestPairingCode( |
38 | 63 | debugLog("Pairing", `Requesting code for: ${cleanNumber}`) |
39 | 64 | const client = getClient() |
40 | 65 |
|
41 | | - const { data } = await client.auth.authControllerRequestCode(sessionName, { |
| 66 | + const { data } = await client.pairing.authControllerRequestCode(sessionName, { |
42 | 67 | phoneNumber: cleanNumber, |
43 | 68 | method: undefined, // undefined = web pairing (not SMS/voice) |
44 | 69 | }) |
|
0 commit comments