mirror of
https://github.com/BradNut/sveltekit-auth-cookies
synced 2025-09-08 17:40:33 +00:00
Use session to change Nav and implement logout.
This commit is contained in:
parent
f9aae20f91
commit
a005acc1d8
2 changed files with 28 additions and 4 deletions
|
|
@ -1,14 +1,23 @@
|
||||||
|
<script lang="ts">
|
||||||
|
import { session } from '$app/stores'
|
||||||
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
<title>SvelteKit Auth</title>
|
<title>SvelteKit Auth</title>
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<nav>
|
<nav>
|
||||||
<a href="/">Home</a>
|
<a href="/">Home</a>
|
||||||
<a href="/auth/login">Login</a>
|
|
||||||
<a href="/auth/register">Register</a>
|
|
||||||
|
|
||||||
<a href="/protected">Admin</a>
|
{#if !$session.user}
|
||||||
<a href="/auth/logout">Log out</a>
|
<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>
|
</nav>
|
||||||
|
|
||||||
<slot />
|
<slot />
|
||||||
15
src/routes/auth/logout/index.ts
Normal file
15
src/routes/auth/logout/index.ts
Normal 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: '/',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue