feat(auth): add tab to show user's identities

This commit is contained in:
Steven Nguyen committed 2024-04-24 12:45:19 -07:00
1 parent 7ab1c4d465
commit 1d377dfab8
6 files changed
+316

No files matched your search

+1
View File
@@ -163,6 +163,7 @@ export enum Submit {
UserUpdateStatus = 'submit_user_update_status',
UserUpdateVerificationEmail = 'submit_user_update_verification_email',
UserUpdateVerificationPhone = 'submit_user_update_verification_phone',
UserIdentityDelete = 'submit_user_identity_delete',
UserTargetCreate = 'submit_user_target_create',
UserTargetDelete = 'submit_user_target_delete',
UserAuthenticatorDelete = 'submit_user_authenticator_delete',
+1
View File
@@ -18,6 +18,7 @@ export enum Dependencies {
ACCOUNT_SESSIONS = 'dependency:account_sessions',
USER = 'dependency:user',
USERS = 'dependency:users',
USER_IDENTITIES = 'dependency:user_identities',
USER_TARGETS = 'dependency:user_targets',
SESSIONS = 'dependency:sessions',
TEAM = 'dependency:team',
@@ -19,6 +19,11 @@
title: 'Memberships',
event: 'memberships'
},
{
href: `${path}/identities`,
title: 'Identities',
event: 'identities'
},
{
href: `${path}/targets`,
title: 'Targets',
@@ -0,0 +1,104 @@
<script lang="ts">
import {
PaginationWithLimit,
Heading,
ViewSelector,
EmptyFilter,
EmptySearch,
DropList
} from '$lib/components';
import { Container } from '$lib/layout';
import type { PageData } from './$types';
import Table from './table.svelte';
import { Filters, hasPageQueries } from '$lib/components/filters';
import { View } from '$lib/helpers/load';
import type { Column } from '$lib/helpers/types';
import { writable } from 'svelte/store';
import { Pill } from '$lib/elements';
export let data: PageData;
let showMoreInfo = false;
const columns = writable<Column[]>([
{ id: '$id', title: 'Identity ID', type: 'string', show: true, width: 140 },
{ id: 'provider', title: 'Provider', type: 'string', show: true, width: 80 },
{ id: 'providerUid', title: 'Provider UID', type: 'string', show: false, width: 80 },
{ id: 'providerEmail', title: 'Email', type: 'string', show: true, width: 80 },
{
id: 'providerAccessTokenExpiry',
title: 'Expiry Date',
type: 'datetime',
show: true,
width: 60
},
{ id: '$createdAt', title: 'Created', type: 'datetime', show: true, width: 80 },
{ id: '$updatedAt', title: 'Updated', type: 'datetime', show: false, width: 80 }
]);
</script>
<Container>
<div class="u-flex u-flex-vertical">
<div class="u-flex u-main-space-between u-cross-center">
<div class="u-flex u-gap-16 u-cross-center">
<Heading tag="h2" size="5">Identities</Heading>
<DropList bind:show={showMoreInfo} width="16">
<Pill button on:click={() => (showMoreInfo = !showMoreInfo)}>
<span class="icon-info" />More info
</Pill>
<svelte:fragment slot="list">
<slot name="tooltip">
<p>
User identities are the user's connected OAuth accounts. The user
can sign in using these identities.
</p>
</slot>
</svelte:fragment>
</DropList>
</div>
<!-- TODO: Remove when searching is added -->
<div class="u-flex u-main-end u-gap-16 is-not-mobile">
<Filters query={data.query} {columns} />
<div>
<ViewSelector
view={View.Table}
{columns}
hideView
allowNoColumns
showColsTextMobile />
</div>
</div>
</div>
<div class="u-flex u-gap-16 is-only-mobile u-margin-block-start-16">
<div class="u-flex-basis-50-percent">
<ViewSelector
view={View.Table}
{columns}
hideView
allowNoColumns
showColsTextMobile
fullWidthMobile />
</div>
<div class="u-flex-basis-50-percent">
<Filters query={data.query} {columns} fullWidthMobile />
</div>
</div>
</div>
{#if data.identities.total}
<Table columns={$columns} {data} />
<PaginationWithLimit
name="Identities"
limit={data.limit}
offset={data.offset}
total={data.identities.total} />
{:else if $hasPageQueries}
<EmptyFilter resource="identities" />
{:else}
<EmptySearch hidePagination>
<div class="u-text-center">
<p>This user has no identities.</p>
<p>Once this user signs in via OAuth, you'll see it here.</p>
</div>
</EmptySearch>
{/if}
</Container>
@@ -0,0 +1,38 @@
import { queries, queryParamToMap } from '$lib/components/filters';
import { Dependencies, PAGE_LIMIT } from '$lib/constants';
import { getLimit, getPage, getQuery, getSearch, pageToOffset } from '$lib/helpers/load';
import { sdk } from '$lib/stores/sdk';
import { Query } from '@appwrite.io/console';
import type { PageLoad } from './$types';
export const load: PageLoad = async ({ params, url, route, depends }) => {
depends(Dependencies.USER_IDENTITIES);
const page = getPage(url);
const limit = getLimit(url, route, PAGE_LIMIT);
const offset = pageToOffset(page, limit);
const query = getQuery(url);
const search = getSearch(url);
const parsedQueries = queryParamToMap(query || '[]');
queries.set(parsedQueries);
const identities = await sdk.forProject.users.listIdentities(
[
Query.equal('userId', params.user),
Query.limit(limit),
Query.offset(offset),
Query.orderDesc(''),
...parsedQueries.values()
],
search
);
return {
offset,
limit,
query,
search,
identities
};
};
@@ -0,0 +1,167 @@
<script lang="ts">
import { FloatingActionBar, Id, Modal } from '$lib/components';
import { Button } from '$lib/elements/forms';
import {
TableBody,
TableCell,
TableCellCheck,
TableCellHead,
TableCellHeadCheck,
TableCellText,
TableHeader,
TableRow,
TableScroll
} from '$lib/elements/table';
import type { PageData } from './$types';
import { toLocaleDateTime } from '$lib/helpers/date';
import { sdk } from '$lib/stores/sdk';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Dependencies } from '$lib/constants';
import { addNotification } from '$lib/stores/notifications';
import { invalidate } from '$app/navigation';
import type { Column } from '$lib/helpers/types';
import { OAuthProviders, type Provider } from '$lib/stores/oauth-providers';
import { project } from '$routes/console/project-[project]/store';
import { app } from '$lib/stores/app';
export let columns: Column[];
export let data: PageData;
let selectedIds: string[] = [];
let showDelete = false;
let deleting = false;
let providerLookup: Record<string, Provider> = {};
async function handleDelete() {
showDelete = false;
const promises = selectedIds.map((id) => sdk.forProject.users.deleteIdentity(id));
try {
await Promise.all(promises);
trackEvent(Submit.UserIdentityDelete, {
total: selectedIds.length
});
addNotification({
type: 'success',
message: `${selectedIds.length} target${selectedIds.length > 1 ? 's' : ''} deleted`
});
invalidate(Dependencies.USER_IDENTITIES);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.UserIdentityDelete);
} finally {
selectedIds = [];
showDelete = false;
}
}
$: OAuthProviders.load($project);
$: providerLookup = $OAuthProviders.providers.reduce((acc, provider) => {
acc[provider.key] = provider;
return acc;
}, {});
</script>
<TableScroll>
<TableHeader>
<TableCellHeadCheck
bind:selected={selectedIds}
pageItemsIds={data.identities.identities.map((d) => d.$id)} />
{#each columns as column}
{#if column.show}
<TableCellHead width={column.width}>{column.title}</TableCellHead>
{/if}
{/each}
</TableHeader>
<TableBody>
{#each data.identities.identities as identity (identity.$id)}
<TableRow>
<TableCellCheck bind:selectedIds id={identity.$id} />
{#each columns as column}
{#if column.show}
{#if column.id === '$id'}
{#key columns}
<TableCell title={column.title}>
<Id value={identity[column.id]}>
{identity[column.id]}
</Id>
</TableCell>
{/key}
{:else if column.id === 'provider'}
{@const provider = providerLookup[identity[column.id]]}
<TableCellText title={column.title} width={column.width}>
<div class="u-inline-flex u-cross-center u-gap-8">
<div class="avatar is-size-small">
<img
style="--p-text-size: 1rem"
height="20"
width="20"
src={`/icons/${$app.themeInUse}/color/${provider.icon}.svg`}
alt={provider.name} />
</div>
{provider.name}
</div>
</TableCellText>
{:else if column.type === 'datetime'}
<TableCellText title={column.title} width={column.width}>
{#if !identity[column.id]}
-
{:else}
{toLocaleDateTime(identity[column.id])}
{/if}
</TableCellText>
{:else}
<TableCellText title={column.title} width={column.width}>
{identity[column.id]}
</TableCellText>
{/if}
{/if}
{/each}
</TableRow>
{/each}
</TableBody>
</TableScroll>
<FloatingActionBar show={selectedIds.length > 0}>
<div class="u-flex u-cross-center u-main-space-between actions">
<div class="u-flex u-cross-center u-gap-8">
<span class="indicator body-text-2 u-bold">{selectedIds.length}</span>
<p>
<span class="is-only-desktop">
{selectedIds.length > 1 ? 'identities' : 'identity'}
</span>
selected
</p>
</div>
<div class="u-flex u-cross-center u-gap-8">
<Button text on:click={() => (selectedIds = [])}>Cancel</Button>
<Button secondary on:click={() => (showDelete = true)}>
<p>Delete</p>
</Button>
</div>
</div>
</FloatingActionBar>
<Modal
title="Delete identity"
icon="exclamation"
state="warning"
bind:show={showDelete}
onSubmit={handleDelete}
headerDivider={false}
closable={!deleting}>
<p class="text" data-private>
Are you sure you want to delete <b>{selectedIds.length}</b>
{selectedIds.length > 1 ? 'identities' : 'identity'}?
</p>
<svelte:fragment slot="footer">
<Button text on:click={() => (showDelete = false)} disabled={deleting}>Cancel</Button>
<Button secondary submit disabled={deleting}>Delete</Button>
</svelte:fragment>
</Modal>