From 2ccabf835c2c923d6986d9cafb4e878f5110b91a Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Thu, 31 Oct 2024 17:22:58 +0100 Subject: [PATCH] feat: add ability to define expiration of one time link --- frontend/src/lib/services/user-service.ts | 4 +- .../admin/users/one-time-link-modal.svelte | 68 +++++++++++++++++-- .../settings/admin/users/user-list.svelte | 31 +++------ frontend/tests/user-settings.spec.ts | 5 ++ 4 files changed, 79 insertions(+), 29 deletions(-) diff --git a/frontend/src/lib/services/user-service.ts b/frontend/src/lib/services/user-service.ts index e5c6ea7..e93de92 100644 --- a/frontend/src/lib/services/user-service.ts +++ b/frontend/src/lib/services/user-service.ts @@ -42,10 +42,10 @@ export default class UserService extends APIService { await this.api.delete(`/users/${id}`); } - async createOneTimeAccessToken(userId: string) { + async createOneTimeAccessToken(userId: string, expiresAt: Date) { const res = await this.api.post(`/users/${userId}/one-time-access-token`, { userId, - expiresAt: new Date(Date.now() + 1000 * 60 * 5).toISOString() + expiresAt }); return res.data.token; } diff --git a/frontend/src/routes/settings/admin/users/one-time-link-modal.svelte b/frontend/src/routes/settings/admin/users/one-time-link-modal.svelte index 75b2380..184fc83 100644 --- a/frontend/src/routes/settings/admin/users/one-time-link-modal.svelte +++ b/frontend/src/routes/settings/admin/users/one-time-link-modal.svelte @@ -1,22 +1,51 @@ - + One Time Link @@ -25,9 +54,36 @@ have lost it. -
- + {#if oneTimeLink === null} +
+ + + (selectedExpiration = v!.value as keyof typeof availableExpirations)} + > + + {selectedExpiration} + + + {#each Object.keys(availableExpirations) as key} + {key} + {/each} + + +
+ + {:else} + -
+ {/if}
diff --git a/frontend/src/routes/settings/admin/users/user-list.svelte b/frontend/src/routes/settings/admin/users/user-list.svelte index b43b8cb..5faa2c2 100644 --- a/frontend/src/routes/settings/admin/users/user-list.svelte +++ b/frontend/src/routes/settings/admin/users/user-list.svelte @@ -1,9 +1,9 @@ - - + + + Toggle menu - createOneTimeAccessToken(item.id)} + (userIdToCreateOneTimeLink = item.id)} >One-time link - goto(`/settings/admin/users/${item.id}`)} > Edit deleteUser(item)} + onclick={() => deleteUser(item)} >Delete @@ -106,4 +95,4 @@ {/snippet} - + diff --git a/frontend/tests/user-settings.spec.ts b/frontend/tests/user-settings.spec.ts index 3603ea8..bd60938 100644 --- a/frontend/tests/user-settings.spec.ts +++ b/frontend/tests/user-settings.spec.ts @@ -57,8 +57,13 @@ test('Create one time access token', async ({ page }) => { .getByRole('row', { name: `${users.craig.firstname} ${users.craig.lastname}` }) .getByRole('button') .click(); + await page.getByRole('menuitem', { name: 'One-time link' }).click(); + await page.getByLabel('One Time Link').getByRole('combobox').click(); + await page.getByRole('option', { name: '12 hours' }).click(); + await page.getByRole('button', { name: 'Generate Link' }).click(); + await expect(page.getByRole('textbox', { name: 'One Time Link' })).toHaveValue( /http:\/\/localhost\/login\/.*/ );