Skip to content

Commit ef3a28e

Browse files
ログアウト処理を実装する #11
1 parent 041e04e commit ef3a28e

File tree

7 files changed

+54
-9
lines changed

7 files changed

+54
-9
lines changed

src/routes/+layout.server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { LayoutServerLoad } from './$types'
2+
3+
export const load: LayoutServerLoad = async ({ locals }) => {
4+
return {
5+
user: locals.user,
6+
}
7+
}

src/routes/+layout.svelte

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores'
3+
</script>
4+
5+
<svelte:head>
6+
<title> SvelteKit Authentication</title>
7+
</svelte:head>
8+
9+
<nav>
10+
{#if $page.data.user}
11+
<!-- <a href="/admin">Admin</a> -->
12+
13+
<form action="/logout" method="POST">
14+
<button type="submit">Log out</button>
15+
</form>
16+
{:else}
17+
<a href="/login">Log in</a>
18+
<a href="/register">Register</a>
19+
{/if}
20+
</nav>
21+
22+
<main>
23+
<slot />
24+
</main>

src/routes/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PageServerLoad } from "./$types";
22

33
export const load: PageServerLoad = async (event) => {
4-
console.log(event)
4+
// console.log(event)
55
}

src/routes/+page.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<h1>Welcome to SvelteKit Authentication</h1>
22
<p>Visit <a href="https://github.com/sinProject-Inc/sveltekit_authentication">GitHub Repository</a> to read the documentation</p>
33

4-
<button on:click={() => window.location.href = '/logout'}>Log out</button>

src/routes/login/+page.server.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { goto } from '$app/navigation'
12
import { db } from '$lib/database'
23
import type { Actions, PageServerLoad } from '.svelte-kit/types/src/routes/register/$types'
34
import { invalid, redirect } from '@sveltejs/kit'
45
import bcrypt from 'bcrypt'
56

6-
export const load: PageServerLoad = async () => {
7-
// todo
7+
export const load: PageServerLoad = async ({ locals }) => {
8+
if (locals.user) throw redirect(302, '/')
89
}
910

1011
export const actions: Actions = {
@@ -37,7 +38,5 @@ export const actions: Actions = {
3738
// secure: process.env.NODE_ENV === 'production',
3839
httpOnly: true,
3940
})
40-
41-
throw redirect(303, '/')
4241
},
4342
}

src/routes/logout/+page.server.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { redirect, type Actions } from "@sveltejs/kit";
2+
import type { PageServerLoad } from "./$types";
3+
4+
export const load: PageServerLoad = async () => {
5+
throw redirect(302, "/")
6+
}
7+
8+
export const actions: Actions = {
9+
default: async ({ cookies }) => {
10+
cookies.set('session_id', '', {
11+
path: '/',
12+
expires: new Date(0),
13+
})
14+
15+
throw redirect(302, '/login')
16+
}
17+
}

src/routes/register/+page.server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ enum Roles {
88
user = 'user',
99
}
1010

11-
export const load: PageServerLoad = async () => {
12-
// todo
11+
export const load: PageServerLoad = async ({ locals }) => {
12+
if (locals.user) throw redirect(302, '/')
1313
}
1414

1515
export const actions: Actions = {
@@ -40,4 +40,3 @@ export const actions: Actions = {
4040
throw redirect(303, '/login')
4141
},
4242
}
43-

0 commit comments

Comments
 (0)