Skip to content

Commit

Permalink
Automate login, Add logout button
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis960 committed Sep 2, 2024
1 parent 1b5bf1d commit 6abeb70
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Blumy-Server/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { LayoutServerLoad } from "./$types";

export const load = (({ locals }) => {
return {
authenticated: locals.user !== null
}
}) satisfies LayoutServerLoad;
13 changes: 13 additions & 0 deletions Blumy-Server/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import { pwaInfo } from 'virtual:pwa-info';
import { PUBLIC_MODE } from '$env/static/public';
export let data;
onMount(async () => {
// @ts-expect-error no declaration file
await import('@tabler/core/dist/js/tabler.js');
Expand Down Expand Up @@ -47,6 +49,17 @@
</li>
</ul>
</div>
{#if data.authenticated}
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<form method="POST" action="/auth?/logout">
<button class="nav-link" data-testid="logout-button">Log out</button>
</form>
</li>
</ul>
</div>
{/if}
</div>
</header>
<div class="page-wrapper">
Expand Down
24 changes: 21 additions & 3 deletions Blumy-Server/src/routes/login/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
<script lang="ts">
import LoginGoogleButton from '$lib/components/auth/LoginGoogleButton.svelte';
</script>
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { onMount } from 'svelte';
import { z } from 'zod';
onMount(async () => {
if (browser) {
const response = await fetch('/auth?/loginGoogle', { method: 'POST', body: '' });
const redirect = z
.object({
status: z.number(),
location: z.string()
})
.parse(await response.json());
<LoginGoogleButton />
// if code is 302, redirect to the google login page
if (redirect.status === 302) {
window.location.href = redirect.location;
}
}
});
</script>

0 comments on commit 6abeb70

Please sign in to comment.