Skip to content

Commit

Permalink
handle expired link better
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Aug 7, 2023
1 parent 7155ef6 commit b4eef1d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
45 changes: 22 additions & 23 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,25 @@ jobs:
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run postinstall (db stuff)
run: yarn postinstall

- name: Build in node mode
run: yarn nodebuild

- name: Install playwright & dependencies
run: yarn exec playwright install --with-deps

- name: Run tests
run: yarn test

- name: Checkout repository
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run postinstall (db stuff)
run: yarn postinstall

- name: Build in node mode
run: yarn nodebuild

- name: Install playwright & dependencies
run: yarn exec playwright install --with-deps

- name: Run tests
run: yarn test
13 changes: 13 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ async function main() {
.deleteMany()
.catch(() => console.log('No friend request table to delete'));

const expiredLink = {
token: '3e99472f1003794c',
phone: '+12015550121',
expires: new Date('8/5/2020')
};
await prisma.magicLink.upsert({
where: {
id: 1
},
update: expiredLink,
create: expiredLink
});

// User 1
await prisma.user.upsert({
where: {
Expand Down
24 changes: 5 additions & 19 deletions src/routes/login/[phone]/[token]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,24 @@ export const load = (async ({ params, cookies }) => {
}
});
} catch {
return new Response(
JSON.stringify({
message: "Can't verify token"
}),
{
status: 403
}
);
console.error("Can't verify token");
throw redirect(308, `/?phone=${params.phone}`);
}

// check DB's expiration date
const { phone, expires } = magicLinkInfo as { phone: string; expires: Date };

if (expires < new Date()) {
return new Response(
JSON.stringify({
message: 'Token has expired'
}),
{
status: 403
}
);
console.error('Token has expired');
throw redirect(308, `/?phone=${params.phone}`);
}

let crypto;
try {
crypto = await import('node:crypto');
} catch (err) {
console.error('crypto support is disabled!');
return {
token: null
};
throw redirect(308, `/?phone=${params.phone}`);
}

const sessionCreatedAt = new Date();
Expand Down
17 changes: 17 additions & 0 deletions tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test, expect } from '@playwright/test';
import { run } from '../prisma/seed';

const host = 'http://localhost:5173';

test.beforeEach(async () => {
await run();
});

test.only('Redirect to login page w/ prefilled phone num on expired magic link', async ({
page
}) => {
await page.goto('http://localhost:5173/login/12015550121/3e99472f1003794c');

await page.waitForURL(`${host}?phone=12015550121`, { waitUntil: 'networkidle' });
await expect(page).toHaveURL(`${host}?phone=12015550121`);
});

0 comments on commit b4eef1d

Please sign in to comment.