Skip to content

Commit

Permalink
feat: implement session logout button
Browse files Browse the repository at this point in the history
Closes #11.
  • Loading branch information
BastiDood committed Jul 15, 2024
1 parent 4274064 commit b1435fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/routes/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { error } from '@sveltejs/kit';

export const actions = {
async logout({ locals: { db }, cookies }) {
const sid = cookies.get('sid');
if (typeof sid === 'undefined') error(401);

const session = await db.deleteValidSession(sid);
if (session === null) db.logger.warn('attempt to delete non-existent/expired session');
else db.logger.info({ deleteValidSession: session });

cookies.delete('sid', { path: '/', httpOnly: true, sameSite: 'lax' });
},
};
29 changes: 24 additions & 5 deletions src/routes/SideBar.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<script lang="ts">
import {
AcademicCap,
ArrowRightStartOnRectangle,
Beaker,
ClipboardDocumentList,
Clock,
Home,
QueueList,
UserCircle,
Users,
} from '@steeze-ui/heroicons';
import { AppRail, AppRailAnchor, Avatar, LightSwitch } from '@skeletonlabs/skeleton';
import { Icon } from '@steeze-ui/svelte-icon';
import type { User } from '$lib/models/user';
import { assert } from '$lib/assert';
import { enhance } from '$app/forms';
import { page } from '$app/stores';
// eslint-disable-next-line init-declarations
Expand All @@ -30,7 +32,7 @@
</AppRailAnchor>
{#if typeof user !== 'undefined' && user.user_id !== null}
<AppRailAnchor href="/profile/" selected={pathname === '/profile/'}>
<Icon src={UserCircle} slot="lead" class="h-8" />
<Avatar slot="lead" width="w-8" src={user.avatar} />
<span>Profile</span>
</AppRailAnchor>
{#if user.lab_id === null}
Expand Down Expand Up @@ -67,9 +69,26 @@
<Icon src={Clock} slot="lead" class="h-8" />
<span>History</span>
</AppRailAnchor>
<div slot="trail" class="my-4 flex aspect-square flex-col items-center justify-center gap-2">
<svelte:fragment slot="trail">
{#if typeof user !== 'undefined'}
<Avatar src={user.avatar} />
<form
method="post"
action="/?/logout"
class="p-2"
use:enhance={({ submitter }) => {
assert(submitter !== null);
assert(submitter instanceof HTMLButtonElement);
submitter.disabled = true;
return async ({ update }) => {
submitter.disabled = false;
await update();
};
}}
>
<button type="submit" class="variant-soft-primary btn-icon btn-icon-sm aspect-square w-full">
<Icon slot="trail" src={ArrowRightStartOnRectangle} class="w-full p-2" />
</button>
</form>
{/if}
</div>
</svelte:fragment>
</AppRail>

0 comments on commit b1435fb

Please sign in to comment.