Skip to content

Commit f48f54c

Browse files
use:enhance を使い、リロードせずに form 送信処理を行う #9
1 parent a5126fc commit f48f54c

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export const actions: Actions = {
1313
const username = data.get('username') as string
1414
const password = data.get('password') as string
1515

16-
if (!username || !password) return invalid(404, { missing: true, username })
16+
if (!username || !password) return invalid(404, { missing: true })
1717

1818
const user = await db.user.findUnique({ where: { username } })
1919

20-
if (!user) return invalid(400, { credentials: true, username })
20+
if (!user) return invalid(400, { credentials: true })
2121

2222
const password_valid = await bcrypt.compare(password, user.password)
2323

24-
if (!password_valid) return invalid(400, { credentials: true, username })
24+
if (!password_valid) return invalid(400, { credentials: true })
2525

2626
await db.authToken.deleteMany({ where: { user_id: user.id } })
2727

@@ -41,6 +41,6 @@ export const actions: Actions = {
4141
httpOnly: true,
4242
})
4343

44-
throw redirect(302, '/')
44+
throw redirect(303, '/')
4545
},
4646
}

src/routes/login/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { enhance } from '$app/forms'
23
import { onMount } from 'svelte'
34
import type { ActionData } from './$types'
45
@@ -17,13 +18,12 @@
1718

1819
<h1>Log in</h1>
1920

20-
<form method="POST">
21+
<form method="POST" use:enhance>
2122
<input
2223
type="text"
2324
name="username"
2425
placeholder="Username"
2526
required
26-
value={form?.username ?? ''}
2727
bind:this={username_element}
2828
/>
2929
<input type="password" name="password" placeholder="Password" required />

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const actions: Actions = {
1919
const email = data.get('email') as string
2020
const password = data.get('password') as string
2121

22-
if (!username || !email || !password) return invalid(404, { missing: true, username, email })
22+
if (!username || !email || !password) return invalid(404, { missing: true })
2323

2424
try {
2525
await db.user.create({
@@ -32,7 +32,7 @@ export const actions: Actions = {
3232
})
3333
} catch (error) {
3434
console.error(error)
35-
return invalid(400, { user_exists: true, username, email })
35+
return invalid(400, { user_exists: true })
3636
}
3737

3838
console.log('aaa')

src/routes/register/+page.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { enhance } from '$app/forms'
23
import { onMount } from 'svelte'
34
import type { ActionData } from './$types'
45
@@ -17,16 +18,15 @@
1718

1819
<h1>Register</h1>
1920

20-
<form method="POST">
21+
<form method="POST" use:enhance>
2122
<input
2223
type="text"
2324
name="username"
2425
placeholder="Username"
2526
required
26-
value={form?.username ?? ''}
2727
bind:this={username_element}
2828
/>
29-
<input type="email" name="email" placeholder="email" required value={form?.email ?? ''} />
29+
<input type="email" name="email" placeholder="email" required />
3030
<input type="password" name="password" placeholder="Password" required />
3131

3232
{#if form?.missing}<p class="error">Username, email and password is required.</p>{/if}

0 commit comments

Comments
 (0)