Skip to content

Commit

Permalink
12hr login link
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Aug 12, 2023
1 parent e2ac595 commit 81f624d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/lib/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DateTime } from 'luxon';

export const toLocalTimezone = (d: Date, timeZone: string) =>
DateTime.fromJSDate(d, { zone: 'utc' }).setZone(timeZone);
DateTime.fromJSDate(d).setZone(timeZone);
2 changes: 1 addition & 1 deletion src/lib/server/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const prisma = new PrismaClient();
export const generate = async () => {
const createdAt = new Date();
const expires = new Date();
expires.setHours(createdAt.getHours() + 1);
expires.setHours(createdAt.getHours() + 12);

let crypto;
try {
Expand Down
23 changes: 21 additions & 2 deletions src/lib/server/twilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PrismaClient, type User } from '@prisma/client';
import { circleNotif } from './sanitize';
import { generate, save } from './login';
import { toLocalTimezone } from '../date';
import { DateTime } from 'luxon';

const prisma = new PrismaClient();
const MessagingResponse = Twilio.twiml.MessagingResponse;
Expand All @@ -20,9 +21,27 @@ const msgToSend = async (
let msg;
switch (type) {
case 'login': {
const { phone, time, token } = msgComps;
const { phone, token, timeZone } = msgComps;
const magicLink = await prisma.magicLink
.findUnique({
where: {
token
},
select: {
expires: true
}
})
.catch((err) => console.error(err));
if (!magicLink) {
throw error(404, {
message: `No magic link with token ${token}`
});
}

msg = `Your login link to playdate.help will expire at ${time}: ${url}/login/${phone.slice(
const localTime = toLocalTimezone(magicLink.expires, timeZone).toLocaleString(
DateTime.TIME_SIMPLE
);
msg = `Your login link to playdate.help will expire at ${localTime}: ${url}/login/${phone.slice(
1
)}/${token}`;
break;
Expand Down
6 changes: 4 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
const { time, token } = await res.json();
if (public_env.PUBLIC_ENV === 'test') console.log('PHONE_TOKEN', phone, token);
const region = new Intl.DateTimeFormat();
const { timeZone } = region.resolvedOptions();
const msgRes = await writeReq('/twilio?nocookie=true', {
phone,
type: 'login',
time,
token
token,
timeZone
});
const { message } = await msgRes.json();
if (msgRes.status === 200) {
Expand Down
10 changes: 2 additions & 8 deletions src/routes/login/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generate, save } from '$lib/server/login';
import { PrismaClient } from '@prisma/client';
import { DateTime } from 'luxon';

const prisma = new PrismaClient({
log: ['query', 'info', 'warn', 'error']
Expand All @@ -19,7 +20,7 @@ export async function POST({ request }: { request: Request }) {
}

const { token, createdAt, expires } = await generate();

console.log({ expires });
if (!token) {
console.error('token generation failed');
return new Response(
Expand All @@ -43,15 +44,8 @@ export async function POST({ request }: { request: Request }) {
process.exit(1);
});

// put the expiration date in a human-readable format for the text message
const hrs = expires.getHours();
const time = `${hrs > 12 ? hrs - 12 : hrs}:${
expires.getMinutes() < 10 ? '0' : ''
}${expires.getMinutes()}${hrs >= 12 ? 'PM' : 'AM'}`;

return new Response(
JSON.stringify({
time,
token
}),
{
Expand Down

0 comments on commit 81f624d

Please sign in to comment.