Skip to content

Commit c70c7ae

Browse files
ログインページを作成する #7
1 parent 5b2cb50 commit c70c7ae

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ model AuthToken {
3434
id Int @id @default(autoincrement())
3535
created_at DateTime @default(now())
3636
updated_at DateTime @updatedAt
37-
token String
3837
user_id Int
38+
token String
3939
user User @relation(fields: [user_id], references: [id])
4040
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { db } from '$lib/database'
2+
import type { Actions, PageServerLoad } from '.svelte-kit/types/src/routes/register/$types'
3+
import { invalid, redirect } from '@sveltejs/kit'
4+
import bcrypt from 'bcrypt'
5+
6+
export const load: PageServerLoad = async () => {
7+
// todo
8+
}
9+
10+
export const actions: Actions = {
11+
default: async ({ cookies, request }) => {
12+
const data = await request.formData()
13+
const username = data.get('username') as string
14+
const password = data.get('password') as string
15+
16+
if (!username || !password) return invalid(404, { missing: true, username })
17+
18+
const user = await db.user.findUnique({ where: { username } })
19+
20+
if (!user) return invalid(400, { credentials: true, username })
21+
22+
const password_valid = await bcrypt.compare(password, user.password)
23+
24+
if (!password_valid) return invalid(400, { credentials: true, username })
25+
26+
await db.authToken.deleteMany({ where: { user_id: user.id } })
27+
28+
const auth_token = await db.authToken.create({
29+
data: {
30+
user_id: user.id,
31+
token: crypto.randomUUID(),
32+
},
33+
})
34+
35+
cookies.set('session_id', auth_token.token, {
36+
path: '/',
37+
maxAge: 60 * 60 * 24 * 30,
38+
sameSite: 'lax',
39+
secure: true,
40+
// secure: process.env.NODE_ENV === 'production',
41+
httpOnly: true,
42+
})
43+
44+
throw redirect(302, '/')
45+
},
46+
}

src/routes/login/+page.svelte

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
<h1>Log in</h1>
1+
<script lang="ts">
2+
import type { ActionData } from './$types'
3+
4+
export let form: ActionData
5+
</script>
6+
7+
<h1>Log in</h1>
8+
9+
<form method="POST">
10+
<input type="text" name="username" placeholder="Username" required value={form?.username ?? ''} />
11+
<input type="password" name="password" placeholder="Password" required />
12+
13+
{#if form?.missing}<p class="error">Username and password is required.</p>{/if}
14+
{#if form?.credentials}<p class="error">You have entered the wrong credentials.</p>{/if}
15+
16+
<button type="submit">Log in</button>
17+
</form>

src/routes/register/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<input type="email" name="email" placeholder="email" required value={form?.email ?? ''} />
1212
<input type="password" name="password" placeholder="Password" required />
1313

14-
{#if form?.missing}<p class="error">Username, email and password are required.</p>{/if}
14+
{#if form?.missing}<p class="error">Username, email and password is required.</p>{/if}
1515
{#if form?.user_exists}<p class="error">Username or email is used.</p>{/if}
1616

17-
<input type="submit" value="Register" />
17+
<button type="submit">Register</button>
1818
</form>

0 commit comments

Comments
 (0)