I am getting this error: Cannot read properties of undefined (reading ‘fromDate’), when I try to get the expiry time for the otp I getting from Twilio. So I am using firebase function for my backend and I am successfully getting an OTP but I am getting that error for reasons unknown to me.
This is my code below in “functions/src/index.ts” file.
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import twilio from "twilio";
import * as dotenv from "dotenv";
// import {getFirestore, Timestamp} from "firebase-admin/firestore";
dotenv.config();
admin.initializeApp();
// const firestore = getFirestore();
const twilioClient = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
const generateOtp = (): string => {
return Math.floor(100000 + Math.random() * 900000).toString();
};
// Send OTP via SMS
export const sendOtpSms = functions.https.onRequest(async (req, res) => {
const {phoneNumber} = req.body;
if (!phoneNumber) {
res.status(400).json({error: "Phone number is required"});
return;
}
const otp = generateOtp();
try {
await twilioClient.messages.create({
body: `Your OTP is ${otp}`,
from: process.env.TWILIO_PHONE,
to: phoneNumber,
});
if (typeof admin.firestore.Timestamp.fromDate === "function") {
const expiresAt = admin.firestore.Timestamp.fromDate(
new Date(Date.now() + 5 * 60000)
);
await admin.firestore().collection("otps").doc(phoneNumber).set({
otp,
expiresAt,
});
console.log(`Generated OTP: ${otp}, Expires At: ${expiresAt.toDate()}`);
} else {
console.log(`Generated OTP: ${otp}`);
res.status(500).json({error: "fromDate is not available"});
return;
}
res.status(200).json({message: "OTP sent successfully"});
} catch (error) {
console.error("Error sending OTP:", error);
res.status(500).json({error: "Failed to send OTP"});
}
});
// Verify Otp
export const verifyOtp = functions.https.onRequest(async (req, res) => {
const {phoneNumber, otp} = req.body;
if (!phoneNumber || !otp) {
res.status(400).json({error: "Phone number and OTP are required"});
return;
}
try {
const otpDoc = await admin
.firestore()
.collection("otps")
.doc(phoneNumber)
.get();
if (!otpDoc.exists) {
res.status(404).json({error: "OTP not found"});
return;
}
const otpData = otpDoc.data();
if (!otpData || !otpData.expiresAt) {
res.status(400).json({error: "OTP data is incomplete"});
return;
}
const isExpired = otpData?.expiresAt.toDate() < new Date();
if (isExpired) {
res.status(400).json({error: "OTP has expired"});
return;
}
if (otp !== otpData?.otp) {
res.status(400).json({error: "Invalid OTP"});
return;
}
res.status(200).json({message: "OTP verified successfully"});
} catch (error) {
console.error("Error verifying OTP:", error);
res.status(500).json({error: "Failed to verify OTP"});
}
});
I have checked if it is undefined, I have done all I can but I am not getting any solution.
I will be glad to get a reply on how to sort this out because it is getting frustrating at this point. I have tried all I can, search online and also used chatGPT and Google Gemini but I could not get a solution.