Skip to content

Commit d95f85d

Browse files
PINコード認証を行う #27
1 parent e85b5f0 commit d95f85d

File tree

14 files changed

+363
-239
lines changed

14 files changed

+363
-239
lines changed

package-lock.json

Lines changed: 158 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"type": "module",
3636
"dependencies": {
3737
"@prisma/client": "^4.5.0",
38-
"bcrypt": "^5.1.0",
3938
"nodemailer": "^6.8.0"
4039
}
4140
}

prisma/schema.prisma

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ model User {
2323
created_at DateTime @default(now())
2424
updated_at DateTime @updatedAt
2525
role_id Int
26-
username String @unique
2726
email String @unique
28-
password String
2927
role Role @relation(fields: [role_id], references: [id])
28+
auth_pin AuthPin[]
3029
auth_token AuthToken[]
3130
}
3231

@@ -38,3 +37,12 @@ model AuthToken {
3837
token String @unique
3938
user User @relation(fields: [user_id], references: [id])
4039
}
40+
41+
model AuthPin {
42+
id Int @id @default(autoincrement())
43+
created_at DateTime @default(now())
44+
updated_at DateTime @updatedAt
45+
user_id Int @unique
46+
pin_code String @unique
47+
user User @relation(fields: [user_id], references: [id])
48+
}

src/app.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
declare namespace App {
55
interface Locals {
66
user: {
7-
username: string
7+
email: string
88
role: string
99
}
1010
}

src/hooks.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const handle: Handle = async ({ event, resolve }) => {
2323
if (!auth_token) return await resolve(event)
2424

2525
event.locals.user = {
26-
username: auth_token.user.username,
26+
email: auth_token.user.email,
2727
role: auth_token.user.role.name,
2828
}
2929

src/routes/+page.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@
1919
<button type="submit">Log out</button>
2020
</form>
2121
{:else}
22-
<a href="/login">Log in</a>
23-
<a href="/register">Register</a>
22+
<a href="/login">Log in / Register</a>
2423
{/if}

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

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
1-
import { CookiesManager } from '$lib/cookies_manager'
2-
import { db } from '$lib/database'
3-
import { NodemailerManager } from '$lib/nodemailer_manager'
4-
import type { Actions, PageServerLoad } from '.svelte-kit/types/src/routes/register/$types'
5-
import { invalid, redirect } from '@sveltejs/kit'
6-
import bcrypt from 'bcrypt'
1+
import type { PageServerLoad } from '.svelte-kit/types/src/routes/$types'
2+
import { redirect } from '@sveltejs/kit'
73

8-
export const load: PageServerLoad = async ({ locals, url }) => {
9-
if (locals.user) {
10-
const redirect_url = url.searchParams.get('redirect') || ' /'
11-
throw redirect(302, redirect_url)
12-
}
13-
}
14-
15-
async function sendMail(username: string) {
16-
const nodemailerManager = new NodemailerManager()
17-
18-
try {
19-
await nodemailerManager.sendMail(
20-
'info@sinproject.net',
21-
'SvelteKit Authentication',
22-
`${username} logged in `
23-
)
24-
} catch (error) {
25-
console.error(error)
26-
}
27-
}
28-
29-
export const actions: Actions = {
30-
default: async ({ cookies, request }) => {
31-
const data = await request.formData()
32-
const username = data.get('username') as string
33-
const password = data.get('password') as string
34-
35-
if (!username || !password) return invalid(404, { missing: true })
36-
37-
const user = await db.user.findUnique({ where: { username } })
38-
39-
if (!user) return invalid(400, { credentials: true })
40-
41-
const password_valid = await bcrypt.compare(password, user.password)
42-
43-
if (!password_valid) return invalid(400, { credentials: true })
44-
45-
sendMail(username)
46-
47-
const auth_token = await db.authToken.upsert({
48-
where: { user_id: user.id },
49-
update: { token: crypto.randomUUID() },
50-
create: { user_id: user.id, token: crypto.randomUUID() },
51-
})
52-
53-
new CookiesManager(cookies).setSessionId(auth_token.token)
54-
55-
return { success: true }
56-
},
4+
export const load: PageServerLoad = async ({ locals }) => {
5+
if (locals.user) throw redirect(302, '/')
576
}

src/routes/login/+page.svelte

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
<script lang="ts">
2-
import { enhance } from '$app/forms'
2+
import { page } from '$app/stores'
33
import { onMount } from 'svelte'
4-
import type { ActionData } from './$types'
4+
5+
let first_element: HTMLInputElement
56
6-
export let form: ActionData
7-
8-
let username_element: HTMLInputElement
7+
const redirect_url = $page.url.searchParams.get('redirect_url') ?? ''
8+
const encoded_redirect_url = encodeURIComponent(redirect_url)
99
1010
onMount(() => {
1111
document.onfocus = (event) => {
1212
if (event.target instanceof HTMLInputElement) event.target.select()
1313
}
1414
15-
username_element.select()
15+
first_element.select()
1616
})
1717
</script>
1818

19-
<h1>Log in</h1>
20-
21-
<form method="POST" use:enhance>
22-
<input
23-
type="text"
24-
name="username"
25-
placeholder="Username"
26-
required
27-
bind:this={username_element}
28-
/>
29-
<input type="password" name="password" placeholder="Password" required />
19+
<h1>Log in / Register</h1>
3020

31-
{#if form?.missing}<p class="error">Username and password is required.</p>{/if}
32-
{#if form?.credentials}<p class="error">You have entered the wrong credentials.</p>{/if}
21+
<form method="POST" action="/pin_code?/login&redirect_url={encoded_redirect_url}">
22+
<input type="email" name="email" placeholder="Email" required bind:this={first_element} />
3323

3424
<button type="submit">Log in</button>
3525
</form>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ import type { PageServerLoad } from "./$types";
44
// NOTE: https://github.com/sveltejs/kit/issues/3912
55

66
export const load: PageServerLoad = async ({ locals, url }) => {
7-
if (!locals.user) throw redirect(302, `/login?redirect=${url.pathname}`)
7+
const redirect_url = new URL(url.origin)
8+
9+
redirect_url.pathname = '/login'
10+
redirect_url.searchParams.set('redirect_url', url.pathname)
11+
12+
if (!locals.user) throw redirect(302, redirect_url.toString())
813
}

src/routes/main/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</svelte:head>
88

99
{#if $page.data.user}
10-
<h1>Welcome {$page.data.user.username}</h1>
10+
<h1>Welcome {$page.data.user.email}</h1>
1111
Role: {$page.data.user.role}
1212
{:else}
1313
<h1>Welcome</h1>

0 commit comments

Comments
 (0)