Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | 1x 1x 1x 1x 25x 25x 20x 1x 1x 1x 20x 1x 1x 1x 20x 23x 23x 6x 6x 6x 5x 1x 6x 6x 5x 1x 6x 6x 9x 9x 9x 9x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 9x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 5x 1x 4x 1x 3x 1x 1x 1x 1x 5x 5x 2x 2x 2x 2x 2x 2x 2x 9x 1x 1x 1x 9x 6x 6x 6x 6x 6x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 6x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 5x 5x 5x 5x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 5x 1x 1x 5x 5x 5x 1x 1x 1x 5x | /**
* Twilio Verify Service
*
* Direct integration with Twilio Verify API for phone OTP.
* This bypasses Supabase's built-in phone auth for better control and debugging.
*/
const TWILIO_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID?.trim();
const TWILIO_AUTH_TOKEN = process.env.TWILIO_AUTH_TOKEN?.trim();
const TWILIO_VERIFY_SERVICE_SID = process.env.TWILIO_VERIFY_SERVICE_SID?.trim();
// Check if Twilio is configured
export function isTwilioConfigured(): boolean {
const configured = !!(TWILIO_ACCOUNT_SID && TWILIO_AUTH_TOKEN && TWILIO_VERIFY_SERVICE_SID);
// Validate format (Account SID starts with AC, Verify Service starts with VA)
if (configured) {
if (!TWILIO_ACCOUNT_SID?.startsWith('AC')) {
console.error('TWILIO_ACCOUNT_SID should start with "AC"');
return false;
}
if (!TWILIO_VERIFY_SERVICE_SID?.startsWith('VA')) {
console.error('TWILIO_VERIFY_SERVICE_SID should start with "VA"');
return false;
}
}
return configured;
}
// Get masked credentials for debugging
function getMaskedCredentials() {
return {
accountSid: TWILIO_ACCOUNT_SID
? `${TWILIO_ACCOUNT_SID.slice(0, 6)}...${TWILIO_ACCOUNT_SID.slice(-4)}`
: 'NOT SET',
authToken: TWILIO_AUTH_TOKEN ? '***SET***' : 'NOT SET',
serviceSid: TWILIO_VERIFY_SERVICE_SID
? `${TWILIO_VERIFY_SERVICE_SID.slice(0, 6)}...${TWILIO_VERIFY_SERVICE_SID.slice(-4)}`
: 'NOT SET',
};
}
/**
* Send OTP via Twilio Verify
* https://www.twilio.com/docs/verify/api/verification
*/
export async function sendVerificationCode(
phone: string
): Promise<{ success: boolean; error?: string; status?: string }> {
if (!isTwilioConfigured()) {
console.error('Twilio Verify not configured. Credentials:', getMaskedCredentials());
return { success: false, error: 'SMS service not configured' };
}
try {
const url = `https://verify.twilio.com/v2/Services/${TWILIO_VERIFY_SERVICE_SID}/Verifications`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic ' + Buffer.from(`${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}`).toString('base64'),
},
body: new URLSearchParams({
To: phone,
Channel: 'sms',
}),
});
const data = await response.json();
if (!response.ok) {
console.error('Twilio Verify send error:', {
status: response.status,
code: data.code,
message: data.message,
moreInfo: data.more_info,
credentials: getMaskedCredentials(),
});
// Map Twilio error codes to user-friendly messages
let errorMessage = 'Failed to send verification code';
// Authentication errors (401)
if (response.status === 401) {
console.error(
'Twilio authentication failed. Check TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN'
);
errorMessage = 'Authentication Error - invalid username';
// Check for common issues
if (data.message?.includes('invalid username')) {
errorMessage = 'SMS service configuration error. Please contact support.';
}
} else if (data.code === 60200) {
errorMessage = 'Invalid phone number format';
} else if (data.code === 60203) {
errorMessage = 'Max send attempts reached. Please wait and try again.';
} else if (data.code === 60212) {
errorMessage = 'Too many requests. Please wait a few minutes.';
} else if (data.code === 60605) {
errorMessage = 'Phone number is not valid for this region';
} else if (data.code === 20003) {
// Authentication error
errorMessage = 'SMS service configuration error. Please contact support.';
} else if (data.message) {
errorMessage = data.message;
}
return { success: false, error: errorMessage };
}
console.log('Twilio Verify send success:', {
sid: data.sid,
to: data.to,
status: data.status,
channel: data.channel,
});
return { success: true, status: data.status };
} catch (error) {
console.error('Twilio Verify send exception:', error);
return { success: false, error: 'Failed to send verification code' };
}
}
/**
* Verify OTP code via Twilio Verify
* https://www.twilio.com/docs/verify/api/verification-check
*/
export async function checkVerificationCode(
phone: string,
code: string
): Promise<{ success: boolean; error?: string; status?: string; valid?: boolean }> {
if (!isTwilioConfigured()) {
console.error('Twilio Verify not configured');
return { success: false, error: 'SMS service not configured' };
}
try {
const url = `https://verify.twilio.com/v2/Services/${TWILIO_VERIFY_SERVICE_SID}/VerificationCheck`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic ' + Buffer.from(`${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}`).toString('base64'),
},
body: new URLSearchParams({
To: phone,
Code: code,
}),
});
const data = await response.json();
if (!response.ok) {
console.error('Twilio Verify check error:', {
status: response.status,
code: data.code,
message: data.message,
});
// Map Twilio error codes
let errorMessage = 'Verification failed';
if (data.code === 60200) {
errorMessage = 'Invalid phone number';
} else if (data.code === 20404) {
errorMessage = 'No pending verification found. Please request a new code.';
} else if (data.code === 60202) {
errorMessage = 'Max verification attempts reached. Please request a new code.';
} else if (data.message) {
errorMessage = data.message;
}
return { success: false, error: errorMessage };
}
console.log('Twilio Verify check result:', {
sid: data.sid,
to: data.to,
status: data.status,
valid: data.valid,
});
if (data.status === 'approved' && data.valid === true) {
return { success: true, status: data.status, valid: true };
}
// Code was incorrect
return {
success: false,
error: 'Invalid verification code',
status: data.status,
valid: data.valid,
};
} catch (error) {
console.error('Twilio Verify check exception:', error);
return { success: false, error: 'Verification failed' };
}
}
/**
* Check verification status
*/
export async function getVerificationStatus(
phone: string
): Promise<{ pending: boolean; error?: string }> {
if (!isTwilioConfigured()) {
return { pending: false, error: 'SMS service not configured' };
}
try {
// Note: Twilio doesn't have a direct "get status" endpoint
// We can list verifications for the phone number
const url = `https://verify.twilio.com/v2/Services/${TWILIO_VERIFY_SERVICE_SID}/Verifications?To=${encodeURIComponent(phone)}`;
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization:
'Basic ' + Buffer.from(`${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}`).toString('base64'),
},
});
const data = await response.json();
if (!response.ok) {
return { pending: false, error: data.message };
}
// Check if there's a pending verification
const pending = data.verifications?.some((v: { status: string }) => v.status === 'pending');
return { pending: !!pending };
} catch (error) {
console.error('Twilio Verify status check error:', error);
return { pending: false, error: 'Failed to check status' };
}
}
|