From 3f785267175c860c4924292e146d96b2076ede98 Mon Sep 17 00:00:00 2001 From: Osman Abdelnasir Date: Tue, 6 Aug 2024 00:35:46 +0400 Subject: [PATCH] fix: set record after emailing and create acc on specific error --- packages/functions/src/index.ts | 57 +++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/functions/src/index.ts b/packages/functions/src/index.ts index 66781b4a..fe8babf1 100644 --- a/packages/functions/src/index.ts +++ b/packages/functions/src/index.ts @@ -28,29 +28,32 @@ export const sendOTP = functions.https.onCall( const { email } = data; const otp = crypto.randomInt(100000, 999999).toString(); - - const otpDoc = admin.firestore().collection('otps').doc(email); - await otpDoc.set({ - otp, - createdAt: admin.firestore.FieldValue.serverTimestamp(), - expiresAt: admin.firestore.Timestamp.fromDate( - new Date(Date.now() + 10 * 60 * 1000) - ), // 10 minutes expiration - }); - const now = new Date(); const formattedDate = now.toISOString().replace(/T/, ' ').replace(/\..+/, ' Z'); - const emailContent = `Hello,

We received a request to sign in to NEAR Onboarding using this email address, at ${formattedDate}. If you want to sign in with your ${email} account, use this code:

${otp}

If you did not request this link, you can safely ignore this email.

Thanks,

Your NEAR Onboarding team`; - await transporter.sendMail({ - from: SENDER_EMAIL, - to: email, - subject: `Sign in to NEAR Onboarding requested at ${formattedDate}`, - html: emailContent - }); - - return { success: true, message: 'OTP sent successfully' }; + try { + await transporter.sendMail({ + from: SENDER_EMAIL, + to: email, + subject: `Sign in to NEAR Onboarding requested at ${formattedDate}`, + html: emailContent + }); + + const otpDoc = admin.firestore().collection('otps').doc(email); + await otpDoc.set({ + otp, + createdAt: admin.firestore.FieldValue.serverTimestamp(), + expiresAt: admin.firestore.Timestamp.fromDate( + new Date(Date.now() + 10 * 60 * 1000) + ), // 10 minutes expiration + }); + + return { success: true, message: 'OTP sent successfully' }; + } catch (error) { + console.error('Error sending OTP:', error); + throw new functions.https.HttpsError('internal', 'Failed to send OTP'); + } } ); @@ -58,7 +61,7 @@ export const verifyOTP = functions .runWith({ timeoutSeconds: 60 }) .https.onCall(async (data: { email: string; otp: string }, _context) => { const { email, otp } = data; - + const otpDoc = await admin.firestore().collection('otps').doc(email).get(); if (!otpDoc.exists) { @@ -85,14 +88,20 @@ export const verifyOTP = functions try { const userRecord = await admin.auth().getUserByEmail(email); uid = userRecord.uid; - } catch (error) { - const newUser = await admin.auth().createUser({ email }); - uid = newUser.uid; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (error: any) { + if (error.code === 'auth/user-not-found') { + const newUser = await admin.auth().createUser({ email }); + uid = newUser.uid; + } else { + console.error('Error getting user:', error); + throw new functions.https.HttpsError('internal', 'Error verifying user'); + } } const customToken = await admin.auth().createCustomToken(uid); await otpDoc.ref.delete(); - + return { success: true, customToken }; });