40 lines
943 B
TypeScript
40 lines
943 B
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
const OTP_FILE = path.join(process.cwd(), 'auth-otp.json');
|
|
|
|
interface OTPData {
|
|
code: string;
|
|
expiresAt: number;
|
|
}
|
|
|
|
export function generateOTP(): string {
|
|
return Math.floor(100000 + Math.random() * 900000).toString();
|
|
}
|
|
|
|
export function saveOTP(code: string) {
|
|
const data: OTPData = {
|
|
code,
|
|
expiresAt: Date.now() + 5 * 60 * 1000 // 5 minutes
|
|
};
|
|
try {
|
|
fs.writeFileSync(OTP_FILE, JSON.stringify(data));
|
|
} catch (err) {
|
|
console.error("Error saving OTP:", err);
|
|
}
|
|
}
|
|
|
|
export function verifyOTP(code: string): boolean {
|
|
if (!fs.existsSync(OTP_FILE)) return false;
|
|
|
|
try {
|
|
const data: OTPData = JSON.parse(fs.readFileSync(OTP_FILE, 'utf8'));
|
|
if (Date.now() > data.expiresAt) return false;
|
|
// Simple check
|
|
return String(data.code).trim() === String(code).trim();
|
|
} catch (e) {
|
|
console.error("Error verifying OTP:", e);
|
|
return false;
|
|
}
|
|
}
|