Skip to content

Commit

Permalink
fix: set record after emailing and create acc on specific error
Browse files Browse the repository at this point in the history
  • Loading branch information
esaminu committed Aug 5, 2024
1 parent bbd84ef commit 3f78526
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions packages/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,40 @@ 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,<br><br>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:<br><br>${otp}<br><br>If you did not request this link, you can safely ignore this email.<br><br>Thanks,<br><br>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');
}
}
);

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) {
Expand All @@ -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 };
});

0 comments on commit 3f78526

Please sign in to comment.