Rework Header Component for Auth handling

This commit is contained in:
Neshura 2023-12-11 20:16:20 +01:00
parent c571f084d7
commit a458f1d600
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
3 changed files with 73 additions and 42 deletions

View file

@ -2,7 +2,6 @@
<script lang="ts"> <script lang="ts">
import '../app.postcss'; import '../app.postcss';
import { page } from '$app/stores';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import { themes } from '$lib/types/themes' import { themes } from '$lib/types/themes'
@ -13,6 +12,11 @@
import AdminSelectedGameStore from '$lib/stores/admin-page/GameStore'; import AdminSelectedGameStore from '$lib/stores/admin-page/GameStore';
import type { ChellarisGameInfo } from '$lib/types/chellaris'; import type { ChellarisGameInfo } from '$lib/types/chellaris';
import UserSettingsStore from '$lib/stores/UserSettingsStore'; import UserSettingsStore from '$lib/stores/UserSettingsStore';
import AuthTokenStore from '$lib/stores/AuthTokenStore';
if (browser) {
$AuthTokenStore = document.cookie.split("=")[document.cookie.split("=").length - 1];
}
/* $: { /* $: {
fetch(apiBaseUrl + '/v3/ethics').then((res) => { fetch(apiBaseUrl + '/v3/ethics').then((res) => {
@ -39,7 +43,7 @@
</script> </script>
<Header {page}/> <Header />
<div class="m-4 border-2 border-accent rounded-lg h-[calc(100%-5.75rem-1px)]"> <div class="m-4 border-2 border-accent rounded-lg h-[calc(100%-5.75rem-1px)]">
<slot /> <slot />
</div> </div>

View file

@ -1,16 +1,15 @@
<svelte:options runes={true} /> <svelte:options runes={true} />
<script lang="ts"> <script lang="ts">
import { page } from '$app/stores';
import discord from '$lib/images/discord.svg'; import discord from '$lib/images/discord.svg';
import AuthTokenStore from '$lib/stores/AuthTokenStore';
import { Exit, EyeClosed, EyeOpen } from 'radix-icons-svelte';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import { themes } from '$lib/types/themes';
import UserSettingsStore from '$lib/stores/UserSettingsStore';
import * as Avatar from '$lib/components/ui/avatar';
import { CaretDown, CaretRight, Exit, EyeClosed, EyeOpen } from 'radix-icons-svelte';
import { Separator } from '$lib/components/ui/separator'; import { Separator } from '$lib/components/ui/separator';
import { Input } from '$lib/components/ui/input';
import * as Popover from '$lib/components/ui/popover'; import * as Popover from '$lib/components/ui/popover';
import * as Avatar from '$lib/components/ui/avatar';
let { page } = $props()
type User = { type User = {
token: string, token: string,
@ -20,29 +19,46 @@
admin: boolean admin: boolean
} }
/*let user: User | undefined = $state({
user.loggedIn = true;
tokenVisible: false,
discord: "neshura",
picture: "/avatar.png",
admin: true, // DEBUG
})*/
let user: User | undefined = $state(undefined); let user: User | undefined = $state(undefined);
function logout() { function handleLogout() {
$AuthTokenStore = ""
document.cookie = "authToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"
user = undefined user = undefined
} }
function nextTheme() { async function handleLogin() {
if ($UserSettingsStore.themeId + 1 < themes.length) { // pretend we are handling the login via a fetch
$UserSettingsStore.themeId += 1 await new Promise((resolve) => setTimeout(resolve, 200));
let ret = {
token: $AuthTokenStore,
discord: "neshura",
picture: "/avatar.png",
admin: true
}
if (true) {
const d = new Date();
let authExpiryDays = 14
d.setTime(d.getTime() + (authExpiryDays*24*60*60*1000));
document.cookie = "authToken=" + ret.token + "; expires=" + d.toUTCString() + "; path=/";
user = {
token: ret.token,
tokenVisible: false,
discord: ret.discord ?? undefined,
picture: ret.picture ?? undefined,
admin: ret.admin
}
} }
else { else {
$UserSettingsStore.themeId = 0 // handle login failure
} }
} }
if ($AuthTokenStore != "") {
handleLogin()
}
</script> </script>
<div class="flex gap-2 items-center justify-center py-3 bg-primary-foreground"> <div class="flex gap-2 items-center justify-center py-3 bg-primary-foreground">
@ -62,14 +78,14 @@
<Button <Button
variant="ghost" variant="ghost"
href="/" href="/"
class="{$page.url.pathname == '/' ? 'text-accent-foreground bg-accent' : ''}" class="{$page.url.pathname === '/' ? 'text-accent-foreground bg-accent' : ''}"
> >
Home Home
</Button> </Button>
<Button <Button
variant="ghost" variant="ghost"
href="/current" href="/current"
class="{$page.url.pathname == '/current' ? 'text-accent-foreground bg-accent' : ''}" class="{$page.url.pathname === '/current' ? 'text-accent-foreground bg-accent' : ''}"
> >
Game [Current Game] Game [Current Game]
</Button> </Button>
@ -80,23 +96,23 @@
> >
Archives Archives
</Button> </Button>
{#if user != undefined && user.admin} {#if user !== undefined && user.admin}
<Button <Button
variant="ghost" variant="ghost"
href="/admin" href="/admin"
class="{$page.url.pathname == '/admin' ? 'text-accent-foreground bg-accent' : ''}" class="{$page.url.pathname === '/admin' ? 'text-accent-foreground bg-accent' : ''}"
> >
Admin Admin
</Button> </Button>
<Button <Button
variant="ghost" variant="ghost"
href="/admin/new" href="/admin/new"
class="{$page.url.pathname == '/admin/new' ? 'text-accent-foreground bg-accent' : ''}" class="{$page.url.pathname === '/admin/new' ? 'text-accent-foreground bg-accent' : ''}"
> >
Admin (New) Admin (New)
</Button> </Button>
{/if} {/if}
{#if user != undefined} {#if user !== undefined}
<Popover.Root> <Popover.Root>
<Popover.Trigger> <Popover.Trigger>
<Button <Button
@ -118,7 +134,7 @@
{:else} {:else}
<p class="text-sm font-medium leading-none w-12">******</p> <p class="text-sm font-medium leading-none w-12">******</p>
{/if} {/if}
<Button variant="ghost" size="sm" class="p-0 h-2 justify-start" on:click={() => {user.tokenVisible = !user.tokenVisible}}> <Button variant="ghost" size="sm" class="p-0 h-2 justify-start" on:click={() => {if (user !== undefined) user.tokenVisible = !user.tokenVisible}}>
{#if user.tokenVisible} {#if user.tokenVisible}
<EyeOpen /> <EyeOpen />
{:else} {:else}
@ -142,7 +158,7 @@
</div> </div>
<Separator orientation="horizontal" /> <Separator orientation="horizontal" />
<div class="w-full p-1"> <div class="w-full p-1">
<Button variant="ghost" class="flex flex-row items-center justify-start h-8 space-x-1 px-2 py-1.5 text-sm w-full" on:click={logout}> <Button variant="ghost" class="flex flex-row items-center justify-start h-8 space-x-1 px-2 py-1.5 text-sm w-full" on:click={handleLogout}>
<p>Log out </p> <p>Log out </p>
<Exit /> <Exit />
</Button> </Button>
@ -159,16 +175,18 @@
Log In Log In
</Button> </Button>
</Popover.Trigger> </Popover.Trigger>
<Popover.Content class="w-56 p-1"> <Popover.Content class="w-40 p-1 ">
<Button variant="ghost" class="justify-start h-8 px-2 py-1.5 text-sm w-full" href="/profile"> <form class="flex flex-col items-center gap-1" on:submit={handleLogin}>
Token: <Input type="text" placeholder="User Token" bind:value={$AuthTokenStore} />
<div class="flex flex-row">
Log In <Button variant="link" class="flex flex-row items-center h-8 space-x-1 px-2 py-1.5 text-sm w-full text-muted-foreground" href="/sign-up" >
</Button> Sign Up
<Button variant="ghost" class="flex flex-row items-center justify-start h-8 space-x-1 px-2 py-1.5 text-sm w-full" on:click={logout}> </Button>
<p>Sign Up </p> <Button variant="secondary" class="justify-start h-8 px-2 py-1.5 text-sm w-full" type="submit">
<Exit /> Log In
</Button> </Button>
</div>
</form>
</Popover.Content> </Popover.Content>
</Popover.Root> </Popover.Root>
{/if} {/if}

View file

@ -1,3 +1,12 @@
<div> <script lang="ts">
LOGIN PAGE
</script>
<div class="flex flex-col items-center justify-center h-full">
<p>
Get an Account!
</p>
<p>
WIP
</p>
</div> </div>