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 | 1x 1x 43x 43x 12x 12x 31x 43x 2x 2x 29x 29x 1x 12x 12x 12x 4x 4x 4x 4x 4x 1x 1x 3x 3x 3x 4x 1x 1x 1x 2x 2x 7x 7x 7x 7x 7x 7x 7x 2x 2x 5x 5x 5x 5x 5x 7x 1x 1x 1x 7x 1x 1x 3x 3x 3x 3x 7x 7x 7x 7x 4x 4x 4x 1x 7x 7x 5x 5x | /**
* OTP Authentication Service
*
* Handles phone OTP authentication using Supabase Auth
*/
import { createClient } from '@/lib/supabase/client';
/**
* Format phone number to E.164 format
*/
export function formatPhoneNumber(phone: string, countryCode: string = '+91'): string {
// Remove all non-digit characters
const digits = phone.replace(/\D/g, '');
// If phone already starts with +, return as is (already E.164)
if (phone.startsWith('+')) {
return phone.replace(/[^\d+]/g, '');
}
// Extract country code digits (remove +)
const countryDigits = countryCode.replace('+', '');
// If the phone starts with the country code digits, add + prefix
if (digits.startsWith(countryDigits)) {
return '+' + digits;
}
// Otherwise, prepend the country code
return countryCode + digits;
}
/**
* Validate phone number format
*/
export function validatePhoneNumber(phone: string): boolean {
const formatted = formatPhoneNumber(phone);
// E.164 format: + followed by 10-15 digits
return /^\+[1-9]\d{9,14}$/.test(formatted);
}
/**
* Send OTP to phone number
*/
export async function sendPhoneOTP(phone: string): Promise<{
success: boolean;
error?: string;
}> {
const supabase = createClient();
const formattedPhone = formatPhoneNumber(phone);
if (!validatePhoneNumber(formattedPhone)) {
return { success: false, error: 'Invalid phone number format' };
}
const { error } = await supabase.auth.signInWithOtp({
phone: formattedPhone,
});
if (error) {
console.error('Send OTP error:', error);
return { success: false, error: error.message };
}
return { success: true };
}
/**
* Verify OTP code
*/
export async function verifyPhoneOTP(
phone: string,
token: string
): Promise<{
success: boolean;
error?: string;
user?: { id: string; phone: string };
}> {
const supabase = createClient();
const formattedPhone = formatPhoneNumber(phone);
if (!token || token.length !== 6) {
return { success: false, error: 'Invalid OTP format' };
}
const { data, error } = await supabase.auth.verifyOtp({
phone: formattedPhone,
token,
type: 'sms',
});
if (error) {
console.error('Verify OTP error:', error);
return { success: false, error: error.message };
}
if (!data.user) {
return { success: false, error: 'Verification failed' };
}
return {
success: true,
user: {
id: data.user.id,
phone: data.user.phone || formattedPhone,
},
};
}
/**
* Check if phone number is registered
*/
export async function isPhoneRegistered(_phone: string): Promise<boolean> {
// Note: This would require admin access to check
// For now, we allow any phone to attempt login/signup via OTP
return true;
}
/**
* Mask phone number for display
*/
export function maskPhoneNumber(phone: string): string {
const formatted = formatPhoneNumber(phone);
if (formatted.length < 6) return '****';
return formatted.slice(0, 4) + '****' + formatted.slice(-2);
}
|