Use session to change Nav and implement logout.

This commit is contained in:
Bradley Shellnut 2022-05-13 10:22:50 -07:00
parent f9aae20f91
commit a005acc1d8
2 changed files with 28 additions and 4 deletions

View file

@ -1,14 +1,23 @@
<script lang="ts">
import { session } from '$app/stores'
</script>
<svelte:head>
<title>SvelteKit Auth</title>
</svelte:head>
<nav>
<a href="/">Home</a>
{#if !$session.user}
<a href="/auth/login">Login</a>
<a href="/auth/register">Register</a>
{/if}
{#if $session.user}
<a href="/protected">Admin</a>
<a href="/auth/logout">Log out</a>
{/if}
</nav>
<slot />

View file

@ -0,0 +1,15 @@
import type { RequestHandler } from '@sveltejs/kit'
import * as cookie from 'cookie'
export const get: RequestHandler = async () => {
return {
status: 303,
headers: {
'Set-Cookie': cookie.serialize('session', '', {
path: '/',
expires: new Date(0),
}),
location: '/',
},
}
}