From a0cea2259db165412e96d1c7a1a63cac2784d8ad Mon Sep 17 00:00:00 2001 From: Jessica Ho Date: Mon, 7 Aug 2023 07:51:30 +0000 Subject: [PATCH] reminder magiclink --- src/lib/server/login.ts | 36 ++++++++++++++++++++++++++++++++++++ src/lib/server/twilio.ts | 24 +++++++++++++++++++++++- src/routes/login/+server.ts | 34 +--------------------------------- 3 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 src/lib/server/login.ts diff --git a/src/lib/server/login.ts b/src/lib/server/login.ts new file mode 100644 index 0000000..bdc502f --- /dev/null +++ b/src/lib/server/login.ts @@ -0,0 +1,36 @@ +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +export const generate = async () => { + const createdAt = new Date(); + const expires = new Date(); + expires.setHours(createdAt.getHours() + 1); + + let crypto; + try { + crypto = await import('node:crypto'); + } catch (err) { + console.error('crypto support is disabled!'); + return { + token: null + }; + } + const token = crypto.randomBytes(8).toString('hex'); + return { + token, + createdAt, + expires + }; +}; + +export async function save(token: string, phone: string, createdAt: Date, expires: Date) { + await prisma.magicLink.create({ + data: { + token, + phone, + expires, + createdAt + } + }); +} diff --git a/src/lib/server/twilio.ts b/src/lib/server/twilio.ts index 117a406..70492a6 100644 --- a/src/lib/server/twilio.ts +++ b/src/lib/server/twilio.ts @@ -4,6 +4,7 @@ import { error, json } from '@sveltejs/kit'; import Twilio from 'twilio'; import { PrismaClient, type User } from '@prisma/client'; import { circleNotif } from './sanitize'; +import { generate, save } from './login'; const prisma = new PrismaClient(); const MessagingResponse = Twilio.twiml.MessagingResponse; @@ -96,7 +97,27 @@ const msgToSend = async ( } case 'reminder': { const { phone } = msgComps; - msg = `Hi! It's your periodic reminder to update your schedule: https://playdate.help/login/${phone}`; + const { token, createdAt, expires } = await generate(); + + if (!token) { + console.error('token generation failed'); + throw error(500, { + message: 'Token generation failed' + }); + } + + // save these attrs to DB + save(token, phone, createdAt, expires) + .then(async () => { + await prisma.$disconnect(); + }) + .catch(async (e) => { + console.error(e); + await prisma.$disconnect(); + process.exit(1); + }); + + msg = `Hi! It's your periodic reminder to update your schedule: ${url}/login/${phone}/${token}`; break; } default: @@ -272,6 +293,7 @@ export async function sendNotif() { const now = new Date(formattedDate); const timeDifference = Math.abs(now.getTime() - reminderDatetime.getTime()); // Get the absolute time difference in milliseconds const minuteInMillis = 60 * 1000; // 1 minute in milliseconds + console.log(user.phone, now, reminderDatetime); if (timeDifference < minuteInMillis) { // currently within a minute of when user should be reminded // send notif diff --git a/src/routes/login/+server.ts b/src/routes/login/+server.ts index a998c83..bf3cf0a 100644 --- a/src/routes/login/+server.ts +++ b/src/routes/login/+server.ts @@ -1,42 +1,10 @@ +import { generate, save } from '$lib/server/login'; import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient({ log: ['query', 'info', 'warn', 'error'] }); -const generate = async () => { - const createdAt = new Date(); - const expires = new Date(); - expires.setHours(createdAt.getHours() + 1); - - let crypto; - try { - crypto = await import('node:crypto'); - } catch (err) { - console.error('crypto support is disabled!'); - return { - token: null - }; - } - const token = crypto.randomBytes(8).toString('hex'); - return { - token, - createdAt, - expires - }; -}; - -async function save(token: string, phone: string, createdAt: Date, expires: Date) { - await prisma.magicLink.create({ - data: { - token, - phone, - expires, - createdAt - } - }); -} - export async function POST({ request }: { request: Request }) { const { phone } = await request.json(); if (!phone) {