add admin logout
This commit is contained in:
parent
5a1fa2cc95
commit
0ab03dd9dc
5
src/lib/components/utils.ts
Normal file
5
src/lib/components/utils.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export async function buttonTriggeredRequest<T>(e: MouseEvent, promise: Promise<T>) {
|
||||||
|
(e.target as HTMLButtonElement).disabled = true;
|
||||||
|
await promise;
|
||||||
|
(e.target as HTMLButtonElement).disabled = false;
|
||||||
|
}
|
@ -2,6 +2,19 @@
|
|||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { env } from '$env/dynamic/public';
|
import { env } from '$env/dynamic/public';
|
||||||
import { IconOutline } from 'svelte-heros-v2';
|
import { IconOutline } from 'svelte-heros-v2';
|
||||||
|
import { buttonTriggeredRequest } from '$lib/components/utils';
|
||||||
|
import { goto } from '$app/navigation';
|
||||||
|
|
||||||
|
async function logout() {
|
||||||
|
const response = await fetch(`${env.PUBLIC_BASE_PATH}/admin/logout`, {
|
||||||
|
method: 'POST'
|
||||||
|
});
|
||||||
|
if (response.ok) {
|
||||||
|
await goto(`${env.PUBLIC_BASE_PATH}/`);
|
||||||
|
} else {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if $page.url.pathname !== `${env.PUBLIC_BASE_PATH}/admin/login`}
|
{#if $page.url.pathname !== `${env.PUBLIC_BASE_PATH}/admin/login`}
|
||||||
@ -14,6 +27,12 @@
|
|||||||
<span class="ml-1">Website Admins</span>
|
<span class="ml-1">Website Admins</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="mt-auto">
|
||||||
|
<button on:click={(e) => buttonTriggeredRequest(e, logout())}>
|
||||||
|
<IconOutline name="arrow-left-on-rectangle-outline" />
|
||||||
|
<span class="ml-1">Ausloggen</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="h-full w-full">
|
<div class="h-full w-full">
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
import { Permissions } from '$lib/permissions';
|
import { Permissions } from '$lib/permissions';
|
||||||
import { env } from '$env/dynamic/public';
|
import { env } from '$env/dynamic/public';
|
||||||
import ErrorToast from '$lib/components/Toast/ErrorToast.svelte';
|
import ErrorToast from '$lib/components/Toast/ErrorToast.svelte';
|
||||||
|
import { buttonTriggeredRequest } from '$lib/components/utils';
|
||||||
|
|
||||||
let allPermissionBadges = {
|
let allPermissionBadges = {
|
||||||
'Admin Read': Permissions.AdminRead,
|
'Admin Read': Permissions.AdminRead,
|
||||||
@ -18,12 +19,6 @@
|
|||||||
let newAdminPassword: string;
|
let newAdminPassword: string;
|
||||||
let newAdminPermissions: number[];
|
let newAdminPermissions: number[];
|
||||||
|
|
||||||
async function buttonTriggeredRequest<T>(e: MouseEvent, promise: Promise<T>) {
|
|
||||||
(e.target as HTMLButtonElement).disabled = true;
|
|
||||||
await promise;
|
|
||||||
(e.target as HTMLButtonElement).disabled = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function addAdmin(username: string, password: string, permissions: Permissions) {
|
async function addAdmin(username: string, password: string, permissions: Permissions) {
|
||||||
const response = await fetch(`${env.PUBLIC_BASE_PATH}/admin/admin`, {
|
const response = await fetch(`${env.PUBLIC_BASE_PATH}/admin/admin`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
16
src/routes/admin/logout/+server.ts
Normal file
16
src/routes/admin/logout/+server.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import type { RequestHandler } from '@sveltejs/kit';
|
||||||
|
import { deleteSession, getSession } from '$lib/server/session';
|
||||||
|
import { Permissions } from '$lib/permissions';
|
||||||
|
|
||||||
|
export const POST = (async ({ cookies }) => {
|
||||||
|
if (getSession(cookies, [Permissions.AdminWrite]) == null) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 401
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteSession(cookies);
|
||||||
|
cookies.delete('session');
|
||||||
|
|
||||||
|
return new Response();
|
||||||
|
}) satisfies RequestHandler;
|
Loading…
x
Reference in New Issue
Block a user