Skip to content

Commit

Permalink
feat: add way to list users from redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Nov 9, 2024
1 parent ac3231d commit d8842f3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
33 changes: 30 additions & 3 deletions src/lib/usernames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,40 @@ export async function deleteUsername(username: string) {

const multi = redis.multi();

await multi.del(usernameKey);
multi.del(usernameKey);
if (user.subspace) {
await multi.del(USER_SUBSPACES_PREFIX + user.subspace);
multi.del(USER_SUBSPACES_PREFIX + user.subspace);
}
if (user.rauthyId) {
await multi.del(USER_RAUTHY_IDS_PREFIX + user.rauthyId);
multi.del(USER_RAUTHY_IDS_PREFIX + user.rauthyId);
}

await multi.exec();
}

export async function listUsers(): Promise<
{
username: string;
rauthyId: string;
subspace: string;
}[]
> {
const userKeys = await redis.keys(USER_NAMES_PREFIX + '*');
return (
await Promise.all(
userKeys.map(async (key) => {
try {
const segments = key.split(':');
const username = segments[segments.length - 1];
const data = await redis.hGetAll(key);
const rauthyId = data.rauthyId;
const subspace = data.subspace;

return { username, rauthyId, subspace };
} catch (_) {
return undefined;
}
})
)
).filter((x) => !!x) as { username: string; rauthyId: string; subspace: string }[];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { claimUsername, deleteUsername } from '$lib/usernames';
import { claimUsername, deleteUsername, listUsers } from '$lib/usernames';
import type { ServerLoad } from '@sveltejs/kit';
import type { Actions } from './$types';

export const load: ServerLoad = async ({}) => {
return { users: await listUsers() };
};

export const actions = {
claimUsername: async ({ request }) => {
const formData = await request.formData();
Expand Down
25 changes: 23 additions & 2 deletions src/routes/(internal)/__internal__/admin/usernames/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import type { ActionData } from './$types';
import type { ActionData, PageData } from './$types';
const { form }: { form: ActionData } = $props();
const { form, data }: { form: ActionData; data: PageData } = $props();
</script>

{#if form?.error}
Expand Down Expand Up @@ -35,3 +35,24 @@
<button>Delete</button>
</fieldset>
</form>

<h2>Users</h2>

<table>
<thead>
<tr>
<td>Username</td>
<td>Rauthy ID</td>
<td>Subspace</td>
</tr>
</thead>
<tbody>
{#each data.users as user}
<tr>
<td><a href={`/${user.username}`}>{user.username}</a></td>
<td>{user.rauthyId}</td>
<td>{user.subspace}</td>
</tr>
{/each}
</tbody>
</table>

0 comments on commit d8842f3

Please sign in to comment.