Skip to content

Commit

Permalink
reminder magiclink
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Aug 7, 2023
1 parent b4eef1d commit a0cea22
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
36 changes: 36 additions & 0 deletions src/lib/server/login.ts
Original file line number Diff line number Diff line change
@@ -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
}
});
}
24 changes: 23 additions & 1 deletion src/lib/server/twilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
34 changes: 1 addition & 33 deletions src/routes/login/+server.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit a0cea22

Please sign in to comment.