履歴: プログラミング/svelte
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 35 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2024/9/7 0:08:14 | プログラミング/svelte 最新版 | 5,314 | +5 | -2 | 表示 | 差分 | — |
| 2024/9/6 18:23:34 | プログラミング/svelte | 5,216 | +6 | -0 | 表示 | 差分 | 選択 |
| 2024/8/30 19:02:58 | プログラミング/svelte | 5,042 | +2 | -0 | 表示 | 差分 | 選択 |
| 2024/7/30 3:52:46 | プログラミング/svelte | 4,990 | +1 | -0 | 表示 | 差分 | 選択 |
| 2024/7/21 3:07:50 | プログラミング/svelte | 4,945 | +1 | -1 | 表示 | 差分 | 選択 |
| 2024/7/19 0:55:05 | プログラミング/svelte | 4,911 | +1 | -0 | 表示 | 差分 | 選択 |
| 2024/7/2 7:41:30 | プログラミング/svelte | 4,878 | +112 | -9 | 表示 | 差分 | 選択 |
| 2024/7/1 11:29:30 | プログラミング/svelte | 2,417 | +3 | -0 | 表示 | 差分 | 選択 |
| 2023/12/3 2:48:52 | プログラミング/svelte | 2,360 | +15 | -0 | 表示 | 差分 | 選択 |
| 2023/12/2 0:45:57 | プログラミング/svelte | 2,026 | +1 | -0 | 表示 | 差分 | 選択 |
| 2023/11/30 15:59:20 | プログラミング/svelte | 1,988 | +3 | -30 | 表示 | 差分 | 選択 |
| 2023/11/29 18:54:52 | プログラミング/svelte | 2,505 | +17 | -0 | 表示 | 差分 | 選択 |
| 2023/11/29 3:15:43 | プログラミング/svelte | 2,101 | +1 | -0 | 表示 | 差分 | 選択 |
| 2023/11/28 10:21:04 | プログラミング/svelte | 2,066 | +10 | -4390 | 表示 | 差分 | 選択 |
| 2023/11/27 15:12:30 | プログラミング/svelte | 136,600 | +364 | -1 | 表示 | 差分 | 選択 |
| 2023/11/27 7:26:06 | プログラミング/svelte | 127,338 | +284 | -0 | 表示 | 差分 | 選択 |
| 2023/11/26 16:26:54 | プログラミング/svelte | 118,533 | +675 | -15 | 表示 | 差分 | 選択 |
| 2023/11/26 10:53:18 | プログラミング/svelte | 93,589 | +50 | -6 | 表示 | 差分 | 選択 |
| 2023/11/25 1:44:35 | プログラミング/svelte | 91,814 | +66 | -0 | 表示 | 差分 | 選択 |
| 2023/11/24 16:23:08 | プログラミング/svelte | 90,481 | +460 | -3 | 表示 | 差分 | 選択 |
| 2023/11/24 8:25:16 | プログラミング/svelte | 76,694 | +213 | -23 | 表示 | 差分 | 選択 |
| 2023/11/23 22:42:10 | プログラミング/svelte | 66,195 | +7 | -7 | 表示 | 差分 | 選択 |
| 2023/11/23 12:53:25 | プログラミング/svelte | 66,212 | +18 | -29 | 表示 | 差分 | 選択 |
| 2023/11/23 9:32:58 | プログラミング/svelte | 66,711 | +512 | -38 | 表示 | 差分 | 選択 |
| 2023/11/23 0:44:28 | プログラミング/svelte | 52,259 | +300 | -55 | 表示 | 表示中 | 選択 |
| 2023/11/22 14:35:58 | プログラミング/svelte | 45,266 | +81 | -0 | 表示 | 差分 | 選択 |
| 2023/11/22 7:59:00 | プログラミング/svelte | 43,056 | +124 | -5 | 表示 | 差分 | 選択 |
| 2023/11/22 0:32:39 | プログラミング/svelte | 39,942 | +210 | -86 | 表示 | 差分 | 選択 |
| 2023/11/21 14:18:13 | プログラミング/svelte | 36,876 | +93 | -5 | 表示 | 差分 | 選択 |
| 2023/11/21 11:45:28 | プログラミング/svelte | 34,915 | +825 | -5 | 表示 | 差分 | 選択 |
| 2023/7/5 14:16:38 | プログラミング/svelte | 12,623 | +1 | -1 | 表示 | 差分 | 選択 |
| 2023/6/1 13:11:31 | プログラミング/svelte | 12,622 | +12 | -1 | 表示 | 差分 | 基準 |
| 2023/6/1 6:47:49 | プログラミング/svelte | 12,354 | +412 | -2 | 表示 | 差分 | 選択 |
| 2023/5/22 9:38:55 | プログラミング/svelte | 592 | +9 | -0 | 表示 | 差分 | 選択 |
| 2023/5/15 2:26:56 | プログラミング/svelte | 271 | — | — | 表示 | — | 選択 |
基準: プログラミング/svelte (2023/6/1 13:11:31) → 比較: プログラミング/svelte (2023/11/23 0:44:28)
@@ -2,6 +2,8 @@
* svelte や svelte kit を使いこなしたい [#o86663ca]
+VS Code ありきで作られた最新のエコシステムの恩恵を受けるために勉強してみよう。
+
#contents
** いまのところ情報収集中 [#o6d339e1]
@@ -12,6 +14,10 @@
https://zenn.dev/kosei28/articles/d965f221a656fd
- Vitest / Playwrightを使ってSvelteのコンポーネントをテストする~
https://qiita.com/oekazuma/items/925ddbf48870fb999c19#vitest%E3%81%A8%E3%81%AF
+- TypeScript 4.9のas const satisfiesが便利。型チェックとwidening防止を同時に行う~
+https://zenn.dev/moneyforward/articles/typescript-as-const-satisfies
+- コーディング不要でGraphQLサーバが作れるPrismaを触ってみて可能性を感じた - SMARTCAMP Engineer Blog~
+https://tech.smartcamp.co.jp/entry/started-prisma
* VS Code + Git Bash + nvm のインストール [#d5c705f1]
@@ -42,7 +48,7 @@
$ node
bash: node: command not found
$ nvm list
-
+
18.16.0
のような場合は、
@@ -132,7 +138,7 @@
│ Yes, using TypeScript syntax
│
◇ Select additional options (use arrow keys/space bar)
-│ Add ESLint for code linting, Add Prettier for code formatting, Add Playwright for browser testing, Add Vitest for unit testing
+ │ Add ESLint for code linting, Add Prettier for code formatting, Add Playwright for browser testing, Add Vitest for unit testing
│
└ Your project is ready!
@@ -424,17 +430,1488 @@
何にしても、ユニットテストばかりでなく E2E テストまで Out of the box で簡単に行えるのはすごい。~
apache2 や nginx どころか WSL すらセットアップの必要がないとは。。。
-* Prisma と Lucia を使った認証システム [#a42dca10]
+* Prisma と Lucia を使った認証システムを作ってみる [#a42dca10]
SvelteKit+Superforms+Prisma+Luciaでログイン機能を爆速で実装する~
https://zenn.dev/gawarago/articles/f75f5113a3803d
-を参考に。
+を参考に以下を使ったサイトを作る基本を身に着けたいなと。
-合わせて読みたい記事:
+- Svelte
+- SvelteKit
+- SuperForms : フォームバリデーション
+- Prisma : データベースアクセス
+- Lucia : 認証ライブラリ
+- sveltekit-flash-message : flash メッセージの管理
+- svelte-french-toast : flash メッセージ(など)を一定時間だけ表示する
-- TypeScript 4.9のas const satisfiesが便利。型チェックとwidening防止を同時に行う~
-https://zenn.dev/moneyforward/articles/typescript-as-const-satisfies
+** スケルトンプロジェクトを作成 [#m0ce9b51]
+ LANG:console
+ $ pnpm create svelte@latest authtest
+ .../Local/pnpm/store/v3/tmp/dlx-26152 | +6 +
+ Packages are hard linked from the content-addressable store to the virtual store.
+ Content-addressable store is at: C:\Users\osamu\AppData\Local\pnpm\store\v3
+ Virtual store is at: ../../AppData/Local/pnpm/store/v3/tmp/dlx-26152/node_modules/.pnpm
+ .../Local/pnpm/store/v3/tmp/dlx-26152 | Progress: resolved 6, reused 6, downloaded 0, added 6, done
+
+ create-svelte version 5.3.1
+
+ ┌ Welcome to SvelteKit!
+ │
+ ◇ Which Svelte app template?
+ │ Skeleton project
+ │
+ ◇ Add type checking with TypeScript?
+ │ Yes, using TypeScript syntax
+ │
+ ◇ Select additional options (use arrow keys/space bar)
+ │ Add ESLint for code linting, Add Prettier for code formatting, Add Playwright for browser testing, Add Vitest for unit testing
+ │
+ └ Your project is ready!
+
+ ✔ Typescript
+ Inside Svelte components, use <script lang="ts">
+
+ ✔ ESLint
+ https://github.com/sveltejs/eslint-plugin-svelte
+
+ ✔ Prettier
+ https://prettier.io/docs/en/options.html
+ https://github.com/sveltejs/prettier-plugin-svelte#options
+
+ ✔ Playwright
+ https://playwright.dev
+
+ ✔ Vitest
+ https://vitest.dev
+
+ Install community-maintained integrations:
+ https://github.com/svelte-add/svelte-add
+
+ Next steps:
+ 1: cd authtest
+ 2: pnpm install
+ 3: git init && git add -A && git commit -m "Initial commit" (optional)
+ 4: pnpm run dev -- --open
+
+ To close the dev server, hit Ctrl-C
+
+ Stuck? Visit us at https://svelte.dev/chat
+
+ $ cd authtest
+
+ $ pnpm install
+ Packages: +271
+ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Packages are hard linked from the content-addressable store to the virtual store.
+ Content-addressable store is at: C:\Users\osamu\AppData\Local\pnpm\store\v3
+ Virtual store is at: node_modules/.pnpm
+ Progress: resolved 293, reused 267, downloaded 4, added 271, done
+ node_modules/.pnpm/svelte-preprocess@5.1.0_postcss@8.4.31_svelte@4.0.5_typescript@5.3.2/node_modules/svelte-preprocess: Running postinstall script, done in 57ms
+ node_modules/.pnpm/@sveltejs+kit@1.27.4_svelte@4.0.5_vite@4.4.2/node_modules/@sveltejs/kit: Running postinstall script, done in 6.4s
+
+ devDependencies:
+ + @playwright/test 1.28.1 (1.34.3 is available)
+ + @sveltejs/adapter-auto 2.0.0 (2.1.0 is available)
+ + @sveltejs/kit 1.27.4 (1.27.6 is available)
+ + @typescript-eslint/eslint-plugin 6.0.0 (6.11.0 is available)
+ + @typescript-eslint/parser 6.0.0 (6.11.0 is available)
+ + eslint 8.28.0 (8.41.0 is available)
+ + eslint-config-prettier 9.0.0
+ + eslint-plugin-svelte 2.30.0 (2.35.0 is available)
+ + prettier 3.0.0 (3.1.0 is available)
+ + prettier-plugin-svelte 3.0.0 (3.1.0 is available)
+ + svelte 4.0.5 (4.2.5 is available)
+ + svelte-check 3.6.0
+ + tslib 2.4.1 (2.5.2 is available)
+ + typescript 5.0.2 (5.3.2 is available)
+ + vite 4.4.2 (5.0.0 is available)
+ + vitest 0.32.2 (0.34.6 is available)
+
+ Done in 21.5s
+
+ $ git init && git add -A && git commit -m "Initial commit"
+ Initialized empty Git repository in C:/Users/osamu/Desktop/svelte/authtest/.git/
+ [master (root-commit) ab94078] Initial commit
+ 20 files changed, 2586 insertions(+)
+ create mode 100644 .eslintignore
+ create mode 100644 .eslintrc.cjs
+ create mode 100644 .gitignore
+ create mode 100644 .npmrc
+ create mode 100644 .prettierignore
+ create mode 100644 .prettierrc
+ create mode 100644 README.md
+ create mode 100644 package.json
+ create mode 100644 playwright.config.ts
+ create mode 100644 pnpm-lock.yaml
+ create mode 100644 src/app.d.ts
+ create mode 100644 src/app.html
+ create mode 100644 src/index.test.ts
+ create mode 100644 src/lib/index.ts
+ create mode 100644 src/routes/+page.svelte
+ create mode 100644 static/favicon.png
+ create mode 100644 svelte.config.js
+ create mode 100644 tests/test.ts
+ create mode 100644 tsconfig.json
+ create mode 100644 vite.config.ts
+ $ pnpm run dev -- --open
+
+ > authtest@0.0.1 dev C:\Users\osamu\Desktop\svelte\authtest
+ > vite dev "--" "--open"
+
+ VITE v4.4.2 ready in 3242 ms
+
+ ➜ Local: http://localhost:5173/
+ ➜ Network: use --host to expose
+ ➜ press h to show help
-https://zenn.dev/moneyforward/articles/typescript-as-const-satisfies
+これで http://localhost:5173/ に Welcome to SvelteKit が表示された。
+
+** サインアップフォームを作成 [#v0f7cb3f]
+
+src/routes/(loggedOut)/signup/+page.svelte
+ LANG: html
+ <div>
+ <h1>サインアップ</h1>
+ <form method="POST">
+ <label for="name">ユーザー名</label>
+ <input type="text" name="name" />
+
+ <label for="email">メールアドレス</label>
+ <input type="text" name="email" />
+
+ <label for="password">パスワード</label>
+ <input type="password" name="password" />
+
+ <label for="confirmPassword">パスワード(確認)</label>
+ <input type="password" name="confirm"/>
+
+ <div><button>サインアップ</button></div>
+ <a href="/login">ログイン</a>
+ </form>
+ </div>
+
+http://localhost:5173/signup でフォームを表示可能なことを確認
+
+POST に対応する Action を書いていないため「ログイン」を押すと "405 POST method not allowed. No actions exist for this page" になることを確認。
+
+src/routes/(loggedOut)/signup/+page.server.ts
+ LANG: ts
+ import type { Actions } from './$types';
+ import { fail } from '@sveltejs/kit';
+
+ export const actions: Actions = {
+ default: async () => {
+
+ return fail(400, {message: 'サインアップ処理が未実装'})
+
+ }
+ };
+
+これで「ログイン」を押してもエラーは出なくなった一方、エラーメッセージも表示されない。
+
+src/routes/(loggedOut)/signup/+page.svelte の冒頭と末尾を下記のように変更
+ LANG: html
+ <script lang="ts">
+ import type { ActionData } from './$types';
+ export let form: ActionData;
+ </script>
+
+ <div>
+ <h1>サインアップ</h1>
+
+ {#if form?.message}<span class="invalid">{form.message}</span>{/if}
+ ...
+
+ </div>
+ <style>
+ .invalid {
+ color: red;
+ }
+ </style>
+
+これでエラーメッセージが表示されるようになった。
+
+ LANG:console
+ $ git add .
+
+ $ git commit -m "signup ページを作成"
+ [master e758e44] signup ページを作成
+ 2 files changed, 36 insertions(+)
+ create mode 100644 src/routes/(loggedOut)/signup/+page.server.ts
+ create mode 100644 src/routes/(loggedOut)/signup/+page.svelte
+
+** 入力内容の validation を行うために SuperForms を入れる [#o1950c2d]
+
+ LANG:console
+ $ pnpm i -D sveltekit-superforms zod
+ Packages: +2
+ ++
+ Progress: resolved 295, reused 273, downloaded 0, added 2, done
+
+ devDependencies:
+ + sveltekit-superforms 1.10.2
+ + zod 3.22.4
+
+ Done in 3.6s
+
+*** signup フォーム用のスキーマを追加 [#ud9711bc]
+
+src/lib/formSchemas/signupSchema.ts
+ LANG:ts
+ import { z } from 'zod';
+
+ // メールアドレスを表す現実的な正規表現 by @sakuro
+ // https://qiita.com/sakuro/items/1eaa307609ceaaf51123
+ const emailRegexp = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
+
+ export const signupSchema = z
+ .object({
+ name: z.string().min(3, { message: 'ユーザー名は3文字以上で入力してください' }),
+ email: z.string().regex(emailRegexp, { message: 'メールアドレスが不正です' }),
+ password: z
+ .string()
+ .regex(/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/, {
+ message: 'パスワードは小文字・大文字・数字を一文字以上含めて8文字以上で入力してください'
+ }),
+ confirm: z.string(),
+ })
+ .refine((data) => data.password === data.confirm, {
+ message: '確認用パスワードが一致しません',
+ path: ['confirm']
+ });
+
+ export type signupSchemaType = typeof signupSchema;
+
+*** バリデーションを行う [#c81e3fa9]
+
+src/routes/(loggedOut)/signup/+page.server.ts
+ LANG:ts
+ import type { Actions, PageServerLoad } from './$types';
+ import { signupSchema } from '$lib/formSchemas/signupSchema';
+ import { superValidate } from 'sveltekit-superforms/server';
+ import { fail } from '@sveltejs/kit';
+
+ const schema = signupSchema;
+
+ export const load = (async () => {
+ const form = await superValidate(schema);
+ return { form };
+ }) satisfies PageServerLoad;
+
+ export const actions: Actions = {
+ default: async (event) => {
+ // フォームデータのバリデーション
+ const form = await superValidate(event, schema);
+ if (!form.valid) {
+ return fail(400, { form });
+ }
+
+ // TODO: サインアップ処理をここに
+
+ return { form };
+ }
+ };
+
+*** エラーメッセージを表示するためのフィールドを用意する [#vd7d8e82]
+
+src/routes/(loggedOut)/signup/+page.svelte
+ LANG:html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import { superForm } from 'sveltekit-superforms/client';
+
+ export let data: PageData;
+ const { form, message, errors, submitting, capture, restore, enhance } = superForm(data.form, {
+ taintedMessage: false
+ });
+ export const snapshot = { capture, restore };
+ </script>
+
+ <div>
+ <h1>サインアップ</h1>
+ {#if $message}<span class="invalid">{$message}</span>{/if}
+ <form method="POST" use:enhance>
+ <label for="name">ユーザー名</label>
+ <input type="text" name="name" bind:value={$form.name} disabled={$submitting} />
+ {#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
+
+ <label for="password">パスワード</label>
+ <input type="password" name="password" bind:value={$form.password} disabled={$submitting} />
+ {#if $errors.password}<span class="invalid">{$errors.password}</span>{/if}
+
+ <label for="confirmPassword">パスワード(確認)</label>
+ <input type="password" name="confirm" bind:value={$form.confirm} disabled={$submitting} />
+ {#if $errors.confirm}<span class="invalid">{$errors.confirm}</span>{/if}
+
+ <div><button disabled={$submitting}>サインアップ</button></div>
+ <a href="/login">ログイン</a>
+ </form>
+ </div>
+
+ <style>
+ .invalid {
+ color: red;
+ }
+ </style>
+
+これで正しくエラーが検出されるようになった。
+
+** Prisma の導入 [#qcf227f1]
+
+https://www.prisma.io/docs/getting-started/quickstart に従って
+さしあたり `sqlite` を使うよう初期化
+
+ LANG:console
+ $ pnpm i -D prisma && pnpm i @prisma/client
+ Packages: +2
+ ++
+ Progress: resolved 297, reused 275, downloaded 0, added 2, done
+
+ devDependencies:
+ + prisma 5.6.0
+
+ Done in 5.4s
+ Packages: +2
+ ++
+ Progress: resolved 299, reused 277, downloaded 0, added 2, done
+
+ dependencies:
+ + @prisma/client 5.6.0
+
+ Done in 8.5s
+
+ $ npx prisma init --datasource-provider sqlite
+
+ ✔ Your Prisma schema was created at prisma/schema.prisma
+ You can now open it in your favorite editor.
+
+ warn You already have a .gitignore file. Don't forget to add `.env` in it to not commit any private information.
+
+ Next steps:
+ 1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started
+ 2. Run prisma db pull to turn your database schema into a Prisma schema.
+ 3. Run prisma generate to generate the Prisma Client. You can then start querying your database.
+
+ More information in our documentation:
+ https://pris.ly/d/getting-started
+
+ $ cat .env
+ ...
+ DATABASE_URL="file:./dev.db"
+
+データベースは `(app)/prisma/dev.db` に作成される。
+
+コミットされないよう .gitignore に追加しておく
+
+ LANG: console
+ $ echo "/prisma/*.db" >> .gitignore
+
+*** Lucia に対応したテーブルを定義 [#a634da69]
+
+prisma/schema.prisma
+ LANG: prisma
+ model AuthUser {
+ id String @id @unique
+ auth_session AuthSession[]
+ auth_key AuthKey[]
+
+ name String @unique
+ email String @unique
+
+ @@map("auth_user")
+ }
+
+ model AuthSession {
+ id String @id @unique
+ user_id String
+ active_expires BigInt
+ idle_expires BigInt
+ auth_user AuthUser @relation(references: [id], fields: [user_id], onDelete: Cascade)
+
+ @@index([user_id])
+ @@map("auth_session")
+ }
+
+ model AuthKey {
+ id String @id @unique
+ hashed_password String?
+ user_id String
+ primary_key Boolean
+ expires BigInt?
+ auth_user AuthUser @relation(references: [id], fields: [user_id], onDelete: Cascade)
+
+ @@index([user_id])
+ @@map("auth_key")
+ }
+
+*** migrate する [#zcaba667]
+
+ LANG: console
+ $ npx prisma migrate dev --name "add tables for lucia"
+ Environment variables loaded from .env
+ Prisma schema loaded from prisma\schema.prisma
+ Datasource "db": SQLite database "dev.db" at "file:./dev.db"
+
+ Applying migration `20231121075732_add_tables_for_lucia`
+
+ The following migration(s) have been created and applied from new schema changes:
+
+ migrations/
+ └─ 20231121075732_add_tables_for_lucia/
+ └─ migration.sql
+
+ Your database is now in sync with your schema.
+
+ ✔ Generated Prisma Client (v5.6.0) to .\node_modules\.pnpm\@prisma+client@5.6.0_prisma@5.6.0\node_modules\@prisma\client in 344ms
+
+*** ライブラリに追加 [#u2aec515]
+
+サーバー上でしか使わないので lib/server の下に置く
+
+src/lib/server/db.ts
+ LANG: ts
+ import { PrismaClient } from '@prisma/client';
+
+ export const db = new PrismaClient();
+
+*** commit [#hdfc72da]
+
+ LANG: console
+ $ git add .
+
+ $ git status
+ On branch master
+ Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: .gitignore
+ modified: package.json
+ modified: pnpm-lock.yaml
+ new file: prisma/dev.db-journal
+ new file: prisma/migrations/20231121075732_add_tables_for_lucia/migration.sql
+ new file: prisma/migrations/migration_lock.toml
+ new file: prisma/schema.prisma
+
+ $ git commit -m "Prisma を使い Lucia 用のテーブルを準備"
+
+** Lucia を導入 [#p8368e9f]
+
+Lucia は認証やセッション管理を行うライブラリ。
+
+ LANG: console
+ $ pnpm i lucia-auth @lucia-auth/adapter-prisma
+ WARN deprecated lucia-auth@1.8.0: Deprecated - see the docs for migrating to v2
+ Packages: +3
+ +++
+ Progress: resolved 302, reused 280, downloaded 0, added 0, done
+
+ dependencies:
+ + @lucia-auth/adapter-prisma 3.0.2
+ + lucia-auth 1.8.0 deprecated
+
+ Done in 3s
+
+んー、1.8.0 は deprecated だから v2 を使えと言われる?
+
+じゃあ https://lucia-auth.com/getting-started/sveltekit/ を見ながら v2 を使おう
+
+ LANG: console
+ $ pnpm rm lucia-auth @lucia-auth/adapter-prisma
+ Packages: -3
+ ---
+ Progress: resolved 299, reused 277, downloaded 0, added 0, done
+
+ dependencies:
+ - @lucia-auth/adapter-prisma 3.0.2
+ - lucia-auth 1.8.0
+
+ Done in 1.9s
+
+ $ pnpm i lucia @lucia-auth/adapter-prisma
+ Packages: +2
+ ++
+ Progress: resolved 301, reused 279, downloaded 0, added 0, done
+
+ dependencies:
+ + @lucia-auth/adapter-prisma 3.0.2
+ + lucia 2.7.4
+
+ Done in 3.2s
+
+*** Lucia v2 に対応してテーブル定義を変更 [#uc08b5db]
+
+AuthKey から primary_key と expires を削除
+
+prisma/schema.prisma
+ model AuthKey {
+ id String @id @unique
+ hashed_password String?
+ user_id String
+ - primary_key Boolean
+ - expires BigInt?
+ auth_user AuthUser @relation(references: [id], fields: [user_id], onDelete: Cascade)
+
+ @@index([user_id])
+ @@map("auth_key")
+ }
+
+データベースに反映
+
+ LANG: console
+ $ npx prisma migrate dev --name "alter tables for lucia v2"
+
+*** Lucia の初期化 [#qe9c6371]
+
+テーブルの対応や User テーブルに追加した追加の属性をライブラリに教える。
+
+src/lib/server/lucia.ts
+ LANG: ts
+ import { lucia } from "lucia";
+ import { sveltekit } from "lucia/middleware";
+ import { dev } from "$app/environment";
+ import { prisma } from "@lucia-auth/adapter-prisma";
+ import { db } from "$lib/server/db";
+
+ export const auth = lucia({
+ env: dev ? "DEV" : "PROD",
+ middleware: sveltekit(),
+ adapter: prisma(db, {
+ user: "authUser",
+ key: "authKey",
+ session: "authSession"
+ }),
+ getUserAttributes: (data) => {
+ return {
+ // IMPORTANT!!!!
+ // `userId` included by default!!
+ name: data.name,
+ email: data.email
+ };
+ },
+ });
+
+ export type Auth = typeof auth;
+
+*** locals.auth を追加 [#gb60f175]
+
+User および Session に追加したフィールドの型定義を与えるとともに、
+
+locals.auth に認証情報を持てるようにする
+
+src/app.d.ts
+ LANG: ts
+ // See https://kit.svelte.dev/docs/types#app
+ // for information about these interfaces
+
+ declare global {
+ declare namespace Lucia {
+ type Auth = import('$lib/server/lucia').Auth;
+ type DatabaseUserAttributes = {name: string, email: string};
+ type DatabaseSessionAttributes = object;
+ }
+
+ namespace App {
+ // interface Error {}
+ interface Locals {
+ auth: import('lucia').AuthRequest;
+ }
+ // interface PageData {}
+ // interface Platform {}
+ }
+ }
+
+ export {};
+
+*** hook で認証情報を読み込む [#m4cb4b20]
+
+https://lucia-auth.com/basics/handle-requests/#sveltekit の通り、
+hooks で event から認証情報を得て locals.auth へ入れる。
+
+src/hooks.server.ts
+ LANG: ts
+ import { auth } from '$lib/server/lucia';
+ import type { Handle } from '@sveltejs/kit';
+ import { sequence } from '@sveltejs/kit/hooks';
+
+ const authHandler: Handle = async ({ event, resolve }) => {
+ event.locals.auth = auth.handleRequest(event);
+
+ return await resolve(event);
+ };
+
+ export const handle = sequence(authHandler);
+
+** サインアップ処理 [#jb708cb7]
+
+v2 では createUser にて primaryKey の代わりに key とする。
+
+src/routes/(loggedOut)/signup/+page.server.ts
+ LANG: ts
+ // 冒頭に追加
+ import { auth } from '$lib/server/lucia';
+ import { LuciaError } from 'lucia';
+ ...
+ // サインアップ処理
+ try {
+ const user = await auth.createUser({
+ key: {
+ providerId: 'email',
+ providerUserId: form.data.email,
+ password: form.data.password
+ },
+ attributes: {
+ name: form.data.name,
+ email: form.data.email,
+ }
+ });
+
+ // そのままログイン状態にする
+ const session = await auth.createSession({userId: user.userId, attributes: {}});
+ event.locals.auth.setSession(session);
+ } catch (e) {
+ if (e instanceof LuciaError && e.message === `AUTH_DUPLICATE_KEY_ID`) {
+ return fail(400, { form: { ...form, message: '名前またはメールアドレスが既存のアカウントと重複しています' } });
+ }
+ // provided user attributes violates database rules (e.g. unique constraint)
+ // or unexpected database errors
+ return fail(400, { form: { ...form, message: 'サインアップエラー' } });
+ }
+
+ throw redirect(302, '/');
+
+これでサインアップできるようになった。
+
+ここではそのままログイン状態にしてしまっている。
+
+メールアドレスの確認などを行うならログイン処理はせず /login へリダイレクトするのが良い。
+
+*** データベースを確認 [#v8c34fa4]
+
+テスト用にサインアップをしてみた後、
+
+ LANG: console
+ $ pnpm prisma studio
+
+とすると Prisma Studio が立ち上がるので、AuthUser テーブルや AuthKey テーブルに正しく情報が登録していることを確認する。
+
+*** ログイン状態を確認 [#k63bf4ed]
+
+上記のコードではサインアップと同時にログインしているので、
+hooks により locals.auth にログイン情報が入っているはず。
+
+そこで、トップページでログイン情報を参照してみることにする。
+
+src/routes/+page.server.ts
+ LANG: ts
+ import type { PageServerLoad } from "./$types";
+
+ export const load: PageServerLoad = async ({locals}) => {
+ const session = await locals.auth.validate();
+ return {user: session?.user}
+ }
+
+session.user に格納されるログインユーザーの情報をページに渡す。
+
+ページ側でそれを受け取って、
+
+src/routes/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ export let data: PageData;
+ </script>
+
+ <h1>Welcome {data.user ? data.user.name : 'to Svelte'}</h1>
+
+とすれば、ログインしている場合に "Welcome (ユーザー名)" と表示されるはずで・・・
+
+試したところうまく行ってるようだった。
+
+** ログアウト処理 [#rbc8d465]
+
+locals.auth の持つ validate() 結果 (session 情報) の内部キャッシュを無効化し、
+それを参照するクッキーも削除する。
+
+src/routes/(loggedIn)/logout/+server.ts
+ LANG: ts
+ import { redirect } from '@sveltejs/kit';
+ import type { RequestHandler } from './$types';
+
+ export const GET: RequestHandler = async ({ locals }) => {
+ // 読み出し済みセッション情報の無効化
+ locals.auth.invalidate();
+ // クッキーからセッションID削除
+ locals.auth.setSession(null);
+ throw redirect(302, '/');
+ };
+
+確かにログアウトできた。
+
+locals.auth は読み出し済みのセッション情報のキャッシュを消すだけで
+データベース上のセッションレコードは消さないみたいなのだけれどそれでいいのかしら???
+
+** ログイン処理 [#lda79f99]
+
+フォームのバリデーション
+
+メールアドレスやパスワードが正しいかどうかはログイン処理で確認するので
+ここでは1文字でも入力されていれば良いことにする。
+
+src/lib/formSchemas/loginSchema.ts
+ LANG: ts
+ import { z } from 'zod';
+
+ export const loginSchema = z
+ .object({
+ email: z.string().min(1, 'メールアドレスを入力して下さい'),
+ password: z.string().min(1, 'パスワードを入力して下さい'),
+ });
+
+ export type loginSchemaType = typeof loginSchema;
+
+ログインフォーム
+
+src/routes/(loggedOut)/login/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import { superForm } from 'sveltekit-superforms/client';
+
+ export let data: PageData;
+ const { form, message, errors, submitting, capture, restore, enhance } = superForm(data.form, {taintedMessage: false});
+ export const snapshot = { capture, restore };
+ </script>
+
+ <div>
+ <h1>ログイン</h1>
+ {#if $message}<span class="invalid">{$message}</span>{/if}
+ <form method="POST" use:enhance>
+ <label for="email">ユーザー名</label>
+ <input type="text" name="email" bind:value={$form.email} disabled={$submitting} />
+ {#if $errors.email}<span class="invalid">{$errors.email[0]}</span>{/if}
+
+ <label for="password">パスワード</label>
+ <input type="password" name="password" bind:value={$form.password} disabled={$submitting} />
+ {#if $errors.password}<span class="invalid">{$errors.password[0]}</span>{/if}
+
+ <div><button disabled={$submitting}>ログイン</button></div>
+ <a href="/signup">サインアップ</a>
+ </form>
+ </div>
+
+ <style>
+ .invalid {
+ color: red;
+ }
+ </style>
+
+ログイン処理
+
+src/routes/(loggedOut)/login/+page.server.ts
+ LANG: ts
+ import type { Actions, PageServerLoad } from './$types';
+ import { loginSchema as schema } from '$lib/formSchemas/loginSchema';
+ import { superValidate } from 'sveltekit-superforms/server';
+ import { fail } from '@sveltejs/kit';
+
+ import { auth } from '$lib/server/lucia';
+
+ export const load = (async () => {
+ const form = await superValidate(schema);
+ return { form };
+ }) satisfies PageServerLoad;
+
+ export const actions: Actions = {
+ default: async (event) => {
+ // フォームデータのバリデーション
+ const form = await superValidate(event, schema);
+ if (!form.valid) {
+ return fail(400, { form });
+ }
+
+ // ログイン処理
+ try {
+ const key = await auth.useKey('email', form.data.email, form.data.password);
+ const session = await auth.createSession({userId: key.userId, attributes: {}});
+ event.locals.auth.setSession(session);
+ } catch {
+ return fail(400, { form: { ...form, message: 'ログインエラー' } });
+ }
+
+ throw redirect(302, '/');
+ }
+ };
+
+ログインもできるようになった
+
+** ログインあり・なしで閲覧制限する [#l79649c5]
+
+src/hooks.server.ts
+ LANG: ts
+ const authHandler: Handle = async ({ event, resolve }) => {
+ event.locals.auth = auth.handleRequest(event);
+ const session = await event.locals.auth.validate();
+
+ if (event.route.id?.startsWith('/(loggedIn)') && !session?.user) {
+ return Response.redirect(`${event.url.origin}/login`, 302);
+ }
+
+ if (event.route.id?.startsWith('/(loggedOut)') && session?.user) {
+ return Response.redirect(`${event.url.origin}/`, 302);
+ }
+
+ return await resolve(event);
+ };
+
+** 権限の管理を与える [#a538926a]
+
+AuthUser と 多対多 関係を持つ Role を導入する。
+
+prisma/schema.prisma
+ model AuthUser {
+ ...
+ + roles Role[]
+ }
+
+ + model Role {
+ + id String @id @default(uuid())
+ + name String @unique
+ +
+ + users AuthUser[]
+ + }
+
+マイグレーションする。
+
+ LANG: console
+ $ npx prisma migrate dev --name "add Role"
+
+Prisma で多対多関係を扱うためのコードは結構煩雑になるようなので、
+Role を使うためのユーティリティ関数を db に持たせる。
+
+このやり方が良いのかは疑問が残る?
+
+src/lib/server/db.ts
+ LANG: ts
+ import { PrismaClient } from '@prisma/client';
+
+ export class ExtendedPrismaClient extends PrismaClient {
+ constructor() { super() }
+
+ // userId が undefined なら権限は空とみなされる
+ async getRoles(userId: string | undefined) {
+ if(!userId) {
+ return []
+ }
+ return (await this.authUser.findUnique({
+ where: {id: userId},
+ select: {roles: true}
+ }))?.roles || []
+ }
+
+ async getRolesString(userId: string | undefined) {
+ return (await this.getRoles(userId)).map((role)=> role.name);
+ }
+
+ async hasRole(userId: string | undefined, role: string) {
+ return (await this.getRolesString(userId)).includes(role);
+ }
+
+ async addRoles(userId: string, ...roles: string[]) {
+ await this.authUser.update({
+ where: { id: userId },
+ data: {
+ roles: {
+ connectOrCreate: roles.map(role=>
+ ({where: {name: role}, create: {name: role}})
+ )
+ },
+ },
+ })
+ }
+
+ async removeRoles(userId: string, ...roles: string[]) {
+ await this.authUser.update({
+ where: { id: userId },
+ data: {
+ roles: {
+ deleteMany: roles.map(role=>({name: role}))
+ },
+ },
+ })
+ }
+ }
+
+ export const db = new ExtendedPrismaClient();
+
+Prisma の言語仕様を見ていると TypeScript と VSCode の存在がいかに偉大か実感する。
+こんな DSL はエディタ上でエラー検出してくれなきゃ、とてもじゃないけど書けない。
+
+ところで、
+ LANG: ts
+ roles.map(role=>({name: role}))
+
+などという記述に注意が必要。これを
+
+ LANG: ts
+ roles.map(role=>{name: role})
+
+と書いてしまうと role=>{ の { がブロックの開始として解釈されてしまい、うまく行かない。
+(その場合 name: はラベルとして解釈される)
+
+JavaScriptのアロー関数でオブジェクトを返す方法~
+https://dev.classmethod.jp/articles/arrow-func-return-object/
+
+そこで一見すると無駄に見える括弧で括っている。
+
+サインアップ時、最初のユーザーには admin 権限を持たせることにする。
+
+src/routes/(loggedOut)/signup/+page.server.ts
+
+ + import { db } from '$lib/server/db'
+ ...
+
+ + // 最初のユーザーには admin 権限を持たせる
+ + if(await db.authUser.count() == 1) {
+ + await db.addRoles(user.userId, 'admin')
+ + }
+
+/(admin) あるいは /admin から始まるパスには admin 権限を持っているユーザーしかアクセスできない。
+
+src/hooks.server.ts
+ LANG:ts
+ + if (event.route.id?.startsWith('/(admin)') || event.route.id?.startsWith('/admin')) {
+ + if (!await db.hasRole(session?.user?.userId, 'admin')) {
+ + return Response.redirect(`${event.url.origin}/`, 302);
+ + }
+ + }
+
+** flash メッセージ [#mb60d486]
+
+redirect による遷移先で表示するメッセージをページ間で引き継ぐためのライブラリ。
+
+http では遷移元と遷移先のページの表示は独立した処理になるため、
+遷移元ページの処理結果に応じて遷移先ページにメッセージを出すには工夫がいる。
+それを担うのがこのライブラリ。
+
+もっともよくある使い方としては、フォームの POST が成功した場合に redirect で
+別ページに移るのが良い作りとされているので、
+その遷移先で送信成功を示すメッセージを表示するのにこのライブラリを使える。
+
+https://www.npmjs.com/package/sveltekit-flash-message の通りの手順で、
+
+ LANG: console
+ $ pnpm i -D sveltekit-flash-message
+
+src/app.d.ts
+ LANG: ts
+ declare namespace App {
+ ...
+
+ interface PageData {
+ flash?: { type: 'success' | 'error'; message: string };
+ }
+ ...
+ }
+
+src/routes/+layout.server.ts
+ LANG: ts
+ export { load } from 'sveltekit-flash-message/server';
+
+src/routes/+layout.svelte
+ LANG: html
+ <script lang="ts">
+ import { getFlash } from 'sveltekit-flash-message';
+ import { page } from '$app/stores';
+
+ const flash = getFlash(page);
+ </script>
+
+ {#if $flash}
+ {@const bg = $flash.type == 'success' ? '#3D9970' : '#FF4136'}
+ <div style:background-color={bg} class="flash">{$flash.message}</div>
+ {/if}
+
+ <slot />
+
+これで準備完了。
+
+ LANG: console
+ $ git commit -m "sveltekit-flash-message を導入"
+
+*** redirect しているところに片っ端からメッセージを付ける (server 側) [#u39a8996]
+
+@sveltejs/kit の redirect の代わりに~
+sveltekit-flash-message/server の redirect を使いメッセージを追加する。
+
+src/routes/(loggedOut)/signup/+page.svelte
+ LANG: ts
+ - import { fail, redirect } from '@sveltejs/kit';
+ + import { fail } from '@sveltejs/kit';
+ + import { redirect } from 'sveltekit-flash-message/server';
+ ...
+ - throw redirect(302, '/');
+ + throw redirect(302, '/', { type: 'success', message: 'サインアップしました'}, event);
+
+みたいな感じ。
+
+src/routes/(loggedOut)/login/+page.svelte
+ LANG: ts
+ throw redirect(302, '/', { type: 'success', message: 'ログインしました'}, event);
+
+も問題ない。
+
+ところが +server.ts の RequestHandler からだと event が手に入らないため困ってしまう。
+
+この場合どうしたらいいんだろう???
+
+"sveltekit-flash-message" "RequestHandler" で検索しても何も出ないのだけれど...
+
+無理やりダミーの RequestEvent を作ればなんとかはなる。でもこうするしかないのかどうか???
+
+ LANG: ts
+ export const GET: RequestHandler = async ({ locals, cookies }) => {
+ // 読み出し済みセッション情報の無効化
+ locals.auth.invalidate();
+ // クッキーからセッションID削除
+ locals.auth.setSession(null);
+
+ const dummyEvent = {cookies} as RequestEvent;
+ throw redirect(302, '/', { type: 'success', message: 'ログアウトしました'}, dummyEvent);
+ };
+
+src/hooks.server.ts については event は手に入るけど redirect の仕方が違うので、
+redirect と独立して flash メッセージを設定するため setFlash を使う?
+
+と思って
+
+src/hooks.server.ts
+ + import { setFlash } from 'sveltekit-flash-message/server';
+
+ ...
+
+ if (event.route.id?.startsWith('/(loggedOut)') && session?.user) {
+ + setFlash({ type: 'error', message: 'すでにログインしています' }, event);
+ return Response.redirect(`${event.url.origin}/`, 302);
+ }
+
+のようにしてみたけどうまく行かなかった。
+
+event.cookies.set を呼んでももそれは event の中を書き換えるだけで、
+Response.redirect が event を参照しないから何の意味もない。
+
+ここでも throw redirect に event を与える形でリダイレクトするのが正解らしい。
+
+ LANG: ts
+ if (event.route.id?.startsWith('/(loggedOut)') && session?.user) {
+ - return Response.redirect(`${event.url.origin}/`, 302);
+ + throw redirect(302, '/', { type: 'error', message: 'すでにログインしています' }, event);
+ }
+
+
+src/hooks.server.ts
+ LANG: ts
+ + import { redirect } from 'sveltekit-flash-message/server';
+
+ ...
+
+ const authHandler: Handle = async ({ event, resolve }) => {
+ event.locals.auth = auth.handleRequest(event);
+ const session = await event.locals.auth.validate();
+
+ if (event.route.id?.startsWith('/(admin)') || event.route.id?.startsWith('/admin')) {
+ if (!await db.hasRole(session?.user?.userId, 'admin')) {
+ - return Response.redirect(`${event.url.origin}/`, 302);
+ + throw redirect(302, '/', { type: 'error', message: '管理者のみアクセス可能です' }, event);
+ }
+ }
+
+ if (event.route.id?.startsWith('/(loggedIn)') && !session?.user) {
+ - return Response.redirect(`${event.url.origin}/login`, 302);
+ + throw redirect(302, '/', { type: 'error', message: 'ログインユーザーのみアクセス可能です' }, event);
+ }
+
+ if (event.route.id?.startsWith('/(loggedOut)') && session?.user) {
+ - return Response.redirect(`${event.url.origin}/`, 302);
+ + throw redirect(302, '/', { type: 'error', message: 'すでにログインしています' }, event);
+ }
+
+ return await resolve(event);
+ };
+
+としたらうまく行った。
+
+*** 見せ方 [#f2e8d67c]
+
+特にクライアントサイドから表示した場合には
+画面上部に出るだけだと気づけない場合もあるので、~
+「表示領域の上部に出て、クリックされたら、あるいはしばらくしたら、引っ込む」~
+みたいな動作が望ましい気がする。
+
+https://github.com/ciscoheat/sveltekit-flash-message#toast-messages-event-style
+
+によれば svelte-french-toast を使うと良いらしい。
+
+ LANG: console
+ $ pnpm i svelte-french-toast
+ Packages: +2
+ ++
+ Progress: resolved 304, reused 280, downloaded 2, added 2, done
+
+ dependencies:
+ + svelte-french-toast 1.2.0
+
+ Done in 4.9s
+
+src/routes/+layout.svelte
+ LANG: html
+ <script lang="ts">
+ import { getFlash } from 'sveltekit-flash-message';
+ import { page } from '$app/stores';
+ import toast, { Toaster } from 'svelte-french-toast';
+
+ const flash = getFlash(page);
+
+ flash.subscribe(($flash) => {
+ if (!$flash) return;
+
+ toast($flash.message, {
+ icon: $flash.type == 'success' ? '✅' : '❌'
+ });
+
+ // Clearing the flash message could sometimes
+ // be required here to avoid double-toasting.
+ flash.set(undefined);
+ });
+ </script>
+
+ <Toaster />
+ <slot />
+
+これでうまく行った。
+
+*** クライアントサイドで使う例も後で勉強すべき [#l5add4ea]
+
+https://www.npmjs.com/package/sveltekit-flash-message#client-side
+
+どういう風に実装されているかまで理解しないと
+どうしてこういう処理が必要になるのか納得しづらいかも。
+
+** サインアップ時にメールアドレスの確認を行う [#ufa1af42]
+
+いきなりサインアップするのではなく、入力されたメールアドレスに確認リンク入りのメールを送って
+メールアドレスの確認が取れたらサインアップを行う、という動作をしたい。
+
+https://lucia-auth.com/guidebook/email-verification-links/
+
+によると
+
+- authUser テーブルに email_verified フィールドを追加
+- emailVerification テーブルを追加
+
+という形で行う形が紹介されているけれど、
+user テーブルに email_verified フィールドを追加する代わりに、
+そもそも email を verify しないと signup ページに
+たどり着けない形にした方が間違いが生じない気がする?
+
++ /signup/email-verification でメールアドレスのみ入力
++ メールが届く
++ メールに記載のリンク先で名前やパスワードなどメールアドレス以外の情報を入力
+-- /signup/xxxxxxxx
+
+のような。
+
+これを実現するため emailVerification テーブルには以下のフィールドを定義する。
+
+- token @id @default(uuid())
+- email
+- createdAt
+
+流れはこんな感じ
+
+¨(
+skinparam handwritten true
+actor User
+participant System
+database Database
+User -> System : /signup/email-verification へアクセス
+System --> User : メールアドレス入力フォームを表示
+User -> System : メールアドレスを入力
+System -> Database : トークンとメールアドレスを保存
+System --> User : トークン入りの URL をメールで通知
+User -> System : トークン入りの URL へアクセス
+System -> Database : トークンを確認
+Database --> System : メールアドレスを返す
+System --> User : トークン入りのサインアップフォームを表示
+User -> System : トークン入りのサインアップフォームを送信
+System -> Database : トークンを確認
+Database --> System : 確認結果
+System -> Database : ユーザー登録
+System -> Database : トークンを削除
+System --> User : サインアップ完了を表示
+);
+
+*** パスワードリセットもできるようにしたい [#w3f0baa5]
+
+サインアップと同様に以下の流れで行う。
+
++ /reset-password/email-verification にメールアドレスだけを入力
++ メールが届く
++ メールに記載のリンク先で新しいパスワードを入力
+-- /reset-password/xxxxxxxx
+
+¨(
+skinparam handwritten true
+actor User
+participant System
+database Database
+User -> System : /reset-password/email-verification へアクセス
+System --> User : メールアドレス入力フォームを表示
+User -> System : メールアドレスを入力
+System -> Database : トークンとメールアドレスを保存
+System --> User : トークン入りの URL をメールで通知
+User -> System : トークン入りの URL へアクセス
+System -> Database : トークンを確認
+Database --> System : メールアドレスを返す
+System --> User : トークン入りのパスワード入力フォームを表示
+User -> System : トークン入りのパスワード入力フォームを送信
+System -> Database : トークンを確認
+Database --> System : 確認結果
+System -> Database : パスワード変更
+System -> Database : トークンを削除
+System --> User : パスワード変更完了を表示
+);
+
+*** emailVerification テーブル [#gc99d89b]
+
+prisma/schema.prisma
+ + model emailVerification {
+ + id String @id @default(uuid())
+ + email String @unique
+ + createdAt DateTime @default(now()) @map("created_at")
+ + }
+
+id を秘密のトークンとして使う
+
+ LANG:console
+ $ npx prisma migrate dev --name "add emailVerification table"
+
+*** メールアドレス入力フォーム [#j7548045]
+
+メールアドレス確認用の正規表現を使いまわせるようライブラリに切り出す
+
+src/lib/emailRegexp.ts
+ LANG: ts
+ // https://qiita.com/sakuro/items/1eaa307609ceaaf51123
+ export const emailRegexp = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
+
+メールアドレスのチェックを行うスキーマを用意
+
+src/lib/formSchemas/emailVerificationSchema.ts
+ LANG: ts
+ import { z } from 'zod';
+ import { emailRegexp } from '$lib/emailRegexp'
+
+ export const emailVerificationSchema = z
+ .object({
+ email: z.string().regex(emailRegexp, { message: 'メールアドレスが不正です' }),
+ })
+
+ export type emailVerificationSchemaType = typeof emailVerificationSchema;
+
+サインアップとパスワードリセットの両方に使えるようにするため
+matcher を使ったルーティングを行う。
+
+https://kit.svelte.jp/docs/advanced-routing#matching
+
+src/lib/emailVerificationPurposes.ts
+ LANG: ts
+ export const purposes = {
+ signup: 'サインアップ',
+ 'reset-password': 'パスワードリセット'
+ };
+
+src/params/emailVerificationPurpose.ts
+ LANG: ts
+ import type { ParamMatcher } from '@sveltejs/kit';
+ import { purposes } from '$lib/emailVerificationPurposes';
+
+ export const match: ParamMatcher = (param) => {
+ return Object.hasOwn(purposes, param);
+ };
+
+src/routes/(loggedOut)/[purpose=emailVerificationPurpose]/email-verification/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import { superForm } from 'sveltekit-superforms/client';
+ import { purposes } from '$lib/emailVerificationPurposes';
+
+ export let data: PageData;
+ const { form, message, errors, submitting, capture, restore, enhance } = superForm(data.form, {
+ taintedMessage: false
+ });
+ export const snapshot = { capture, restore };
+
+ const purpose = purposes[data.purpose];
+ </script>
+
+ <div>
+ <h1>メールアドレスを入力して下さい</h1>
+ <p>入力されたアドレスへ {purpose} を続けるためのリンクを含むメールを送信します<br />
+ そちらのリンクから {purpose} 手続きを進めて下さい</p>
+ {#if $message}<span class="invalid">{$message}</span>{/if}
+ <form method="POST" use:enhance>
+ <label for="email">メールアドレス</label>
+ <input type="text" name="email" bind:value={$form.email} disabled={$submitting} />
+ {#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
+
+ <div><button disabled={$submitting}>メールを送信</button></div>
+ </form>
+ </div>
+
+ <style>
+ .invalid {
+ color: red;
+ }
+ </style>
+
+src/routes/(loggedOut)/[purpose=emailVerificationPurpose]/email-verification/+page.server.ts
+ LANG: ts
+ import type { Actions, PageServerLoad } from './$types';
+ import { emailVerificationSchema as schema } from '$lib/formSchemas/emailVerificationSchema';
+ import { superValidate } from 'sveltekit-superforms/server';
+ import { fail } from '@sveltejs/kit';
+ import { redirect } from 'sveltekit-flash-message/server';
+ import type { purposes } from '$params/emailVerificationPurpose';
+
+ export const load = (async (event) => {
+ // 目的を確認
+ const purpose = event.params.purpose as keyof typeof purposes;
+
+ const form = await superValidate(schema);
+ return { form, purpose };
+ }) satisfies PageServerLoad;
+
+ export const actions: Actions = {
+ default: async (event) => {
+ // 目的を確認
+ const purpose = event.params.purpose as keyof typeof purposes;
+
+ // フォームデータのバリデーション
+ const form = await superValidate(event, schema);
+ if (!form.valid) {
+ return fail(400, { form, purpose });
+ }
+
+ // TODO: emailVerification レコードを作成
+
+ // TODO: メールを送信
+
+ throw redirect(302, '/', { type: 'success', message: 'メールを送信しました'}, event);
+ }
+ };
+
+*** emailVerification レコードを作成 [#z328139c]
+
+一定時間以内に作成されたものがすでに存在すればエラーにする
+
+src/routes/(loggedOut)/[purpose=emailVerificationPurpose]/email-verification/+page.server.ts
+ LANG: ts
+ // emailVerification レコードが既に存在するか?
+ const existing = await db.emailVerification.findUnique({where: {email: form.data.email}})
+ if(existing) {
+ // 2時間以内に送っていればエラーにする
+ if(existing.createdAt.getTime() > Date.now() - (1000 * 60 * 60 * 2)) {
+ throw redirect(302, '/', { type: 'error', message: '先ほどメールを送信しましたのでしばらく経ってから試してください'}, event);
+ }
+
+ // 既存のものを削除
+ await db.emailVerification.delete({where: {email: form.data.email}});
+ }
+
+ // レコードを作成する
+ const record = await db.emailVerification.create({data: {email: form.data.email}});
+ const token = record.id;
+
+*** メールを送信 [#hb25fe9c]
+
+アドレスは /signup/[token] になる
+
+https://shinobiworks.com/blog/385/ のように nodemailer を使う
+
+ LANG: console
+ $ npm i nodemailer && npm i --save-dev @types/nodemailer
+
+src/lib/server/transporter.ts
+ LANG: ts
+ import { createTransport } from "nodemailer";
+
+ export const transporter = createTransport({
+ host: "localhost",
+ port: 25,
+ });
+
+src/routes/(loggedOut)/[purpose=emailVerificationPurpose]/email-verification/+page.server.ts
+ LANG: ts
+ import { transporter } from '$lib/server/transporter';
+ ...
+
+ // メールを送信
+
+ const url = `${purpose}/${token}`
+
+ try {
+ await transporter.sendMail({
+ from: `"authtest system" <authtest@my.server.net>`,
+ to: form.data.email,
+ subject: `[authtest] ${purposes[purpose]} 用のリンクをお送りします`,
+ text: `次の URL より ${purposes[purpose]} の手続きをお勧めください\n${url}`
+ });
+ } catch (error) {
+ throw redirect(302, event.url, { type: 'error', message: 'メールを送信できませんでした'}, event);
+ }
+
+*** サインアップフォーム [#j99985ea]
+
+以前の src/routes/(loggedOut)/signup/+page.* を
+src/routes/(loggedOut)/signup/[token]/+page.* に移す。
+
+src/routes/(loggedOut)/signup/[token]/+page.server.ts
+ LANG: ts
+ - export const load = (async () => {
+ + export const load = (async (event) => {
+ const form = await superValidate(schema);
+
+ + // トークンをチェックしてメールアドレスを設定
+ + const token = event.params.token;
+ + const record = await db.emailVerification.findUnique({where: {id: token}})
+ + if(!record || Date.now() - (1000 * 60 * 60 * 2) > record.createdAt.getTime()) {
+ + // 見つからないあるいは古すぎる
+ + throw redirect(302, '/', {type: 'error', message: '無効なトークンです'}, event);
+ + }
+ + form.data.email = record.email;
+
+ return { form };
+ }) satisfies PageServerLoad;
+
+ export const actions: Actions = {
+ default: async (event) => {
+ // フォームデータのバリデーション
+ const form = await superValidate(event, schema);
+ if (!form.valid) {
+ return fail(400, { form });
+ }
+
+ + const token = event.params.token;
+ + const record = await db.emailVerification.findUnique({where: {id: token}})
+ + if(!record || Date.now() - (1000 * 60 * 60 * 2) > record.createdAt.getTime()) {
+ + // 見つからないあるいは古すぎる
+ + throw redirect(302, '/', {type: 'error', message: '無効なトークンです'}, event);
+ + }
+ + if(form.data.email != record.email) {
+ + form.errors.email = [...(form.errors.email || []), 'メールアドレスは変更できません'];
+ + return fail(400, { form });
+ + }
+
+こんな感じ?