From 5c9f8a2e3f74b7246a21f5bde9d5b4b0ccad2495 Mon Sep 17 00:00:00 2001 From: Shinosaki Date: Mon, 14 Aug 2023 14:49:34 +0900 Subject: [PATCH 1/2] =?UTF-8?q?front/wrangler.toml=20=E3=82=92=E4=BD=9C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit wrangler.tomlがない場合、上位のwrangler.tomlが読み込まれてしまうから --- front/wrangler.toml | 1 + 1 file changed, 1 insertion(+) create mode 100644 front/wrangler.toml diff --git a/front/wrangler.toml b/front/wrangler.toml new file mode 100644 index 0000000..f2eb115 --- /dev/null +++ b/front/wrangler.toml @@ -0,0 +1 @@ +name = "workers-mail-front" From 0b16ab95570234cb920d18f3b06f8c608c474536 Mon Sep 17 00:00:00 2001 From: Shinosaki Date: Mon, 14 Aug 2023 14:51:17 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Fetch=20API=E3=81=AE=E3=82=AF=E3=83=AC?= =?UTF-8?q?=E3=83=87=E3=83=B3=E3=82=B7=E3=83=A3=E3=83=AB=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E3=82=92credentials=E3=81=8B=E3=82=89header?= =?UTF-8?q?=E5=86=85=E3=81=AECookie=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cloudflare WorkersはFetch APIの`credentials: 'include'`に対応していない。 そのため、リクエストに含まれるcookieをFetch APIのリクエストヘッダに直接追加することで解決。 また、+page.jsではなく+page.server.jsでないとcookiesを参照できない。 --- front/.env.development | 2 +- .../routes/(app)/{+layout.js => +layout.server.js} | 6 ++++-- .../(app)/register/{+page.js => +page.server.js} | 6 ++---- .../routes/(mail)/{+layout.js => +layout.server.js} | 6 ++++-- front/src/routes/(mail)/i/inbox/+page.js | 11 ----------- front/src/routes/(mail)/i/inbox/+page.server.js | 13 +++++++++++++ front/src/routes/(mail)/i/message/[id]/+page.js | 11 ----------- .../routes/(mail)/i/message/[id]/+page.server.js | 13 +++++++++++++ 8 files changed, 37 insertions(+), 31 deletions(-) rename front/src/routes/(app)/{+layout.js => +layout.server.js} (67%) rename front/src/routes/(app)/register/{+page.js => +page.server.js} (56%) rename front/src/routes/(mail)/{+layout.js => +layout.server.js} (69%) delete mode 100644 front/src/routes/(mail)/i/inbox/+page.js create mode 100644 front/src/routes/(mail)/i/inbox/+page.server.js delete mode 100644 front/src/routes/(mail)/i/message/[id]/+page.js create mode 100644 front/src/routes/(mail)/i/message/[id]/+page.server.js diff --git a/front/.env.development b/front/.env.development index 68579e0..57f76c7 100644 --- a/front/.env.development +++ b/front/.env.development @@ -1 +1 @@ -PUBLIC_API_ENDPOINT = /api \ No newline at end of file +PUBLIC_API_ENDPOINT = http://localhost:8787/api \ No newline at end of file diff --git a/front/src/routes/(app)/+layout.js b/front/src/routes/(app)/+layout.server.js similarity index 67% rename from front/src/routes/(app)/+layout.js rename to front/src/routes/(app)/+layout.server.js index 4205b8b..b39f158 100644 --- a/front/src/routes/(app)/+layout.js +++ b/front/src/routes/(app)/+layout.server.js @@ -1,9 +1,11 @@ import { redirect } from "@sveltejs/kit"; import { PUBLIC_API_ENDPOINT } from '$env/static/public'; -export const load = async ({ fetch }) => { +export const load = async ({ cookies, fetch }) => { const res = await fetch(`${PUBLIC_API_ENDPOINT}/v1/ping`, { - credentials: 'include' + headers: { + cookie: cookies.get('id') + } }); if (res.status === 200) { diff --git a/front/src/routes/(app)/register/+page.js b/front/src/routes/(app)/register/+page.server.js similarity index 56% rename from front/src/routes/(app)/register/+page.js rename to front/src/routes/(app)/register/+page.server.js index 8245968..b3ba3e9 100644 --- a/front/src/routes/(app)/register/+page.js +++ b/front/src/routes/(app)/register/+page.server.js @@ -1,9 +1,7 @@ import { PUBLIC_API_ENDPOINT } from '$env/static/public'; -export const load = async ({ fetch }) => { - const { status } = await fetch(`${PUBLIC_API_ENDPOINT}/auth/register`, { - credentials: 'include' - }).then(r=>r.json()); +export const load = async ({ cookies, fetch }) => { + const { status } = await fetch(`${PUBLIC_API_ENDPOINT}/auth/register`).then(r=>r.json()); return { status diff --git a/front/src/routes/(mail)/+layout.js b/front/src/routes/(mail)/+layout.server.js similarity index 69% rename from front/src/routes/(mail)/+layout.js rename to front/src/routes/(mail)/+layout.server.js index e8cbc1b..6b2ceaf 100644 --- a/front/src/routes/(mail)/+layout.js +++ b/front/src/routes/(mail)/+layout.server.js @@ -1,9 +1,11 @@ import { redirect } from "@sveltejs/kit"; import { PUBLIC_API_ENDPOINT } from '$env/static/public'; -export const load = async ({ fetch }) => { +export const load = async ({ cookies, fetch }) => { const res = await fetch(`${PUBLIC_API_ENDPOINT}/v1/ping`, { - credentials: 'include' + headers: { + cookie: cookies.get('id') + } }); if (res.status === 401) { diff --git a/front/src/routes/(mail)/i/inbox/+page.js b/front/src/routes/(mail)/i/inbox/+page.js deleted file mode 100644 index 225df5a..0000000 --- a/front/src/routes/(mail)/i/inbox/+page.js +++ /dev/null @@ -1,11 +0,0 @@ -import { PUBLIC_API_ENDPOINT as API } from '$env/static/public'; - -export const load = async ({ fetch }) => { - const messages = await fetch(`${API}/v1/messages`, { - credentials: 'include' - }).then(r=>r.json()); - - return { - messages - }; -} \ No newline at end of file diff --git a/front/src/routes/(mail)/i/inbox/+page.server.js b/front/src/routes/(mail)/i/inbox/+page.server.js new file mode 100644 index 0000000..0cb7e6c --- /dev/null +++ b/front/src/routes/(mail)/i/inbox/+page.server.js @@ -0,0 +1,13 @@ +import { PUBLIC_API_ENDPOINT } from '$env/static/public'; + +export const load = async ({ cookies, fetch }) => { + const messages = await fetch(`${PUBLIC_API_ENDPOINT}/v1/messages`, { + headers: { + cookie: cookies.get('id') + } + }).then(r=>r.json()); + + return { + messages + }; +} \ No newline at end of file diff --git a/front/src/routes/(mail)/i/message/[id]/+page.js b/front/src/routes/(mail)/i/message/[id]/+page.js deleted file mode 100644 index 28f9e80..0000000 --- a/front/src/routes/(mail)/i/message/[id]/+page.js +++ /dev/null @@ -1,11 +0,0 @@ -import { PUBLIC_API_ENDPOINT as API } from '$env/static/public'; - -export const load = async ({ fetch, params }) => { - const message = await fetch(`${API}/v1/message/${params.id}`, { - credentials: 'include' - }).then(r=>r.json()); - - return { - message - }; -} \ No newline at end of file diff --git a/front/src/routes/(mail)/i/message/[id]/+page.server.js b/front/src/routes/(mail)/i/message/[id]/+page.server.js new file mode 100644 index 0000000..2bdf460 --- /dev/null +++ b/front/src/routes/(mail)/i/message/[id]/+page.server.js @@ -0,0 +1,13 @@ +import { PUBLIC_API_ENDPOINT } from '$env/static/public'; + +export const load = async ({ cookies, fetch, params }) => { + const message = await fetch(`${PUBLIC_API_ENDPOINT}/v1/message/${params.id}`, { + headers: { + cookie: cookies.get('id') + } + }).then(r=>r.json()); + + return { + message + }; +} \ No newline at end of file