Skip to content

Commit

Permalink
Fetch APIのクレデンシャル指定方法をcredentialsからheader内のCookieに変更
Browse files Browse the repository at this point in the history
  Cloudflare WorkersはFetch APIの`credentials: 'include'`に対応していない。
  そのため、リクエストに含まれるcookieをFetch APIのリクエストヘッダに直接追加することで解決。

  また、+page.jsではなく+page.server.jsでないとcookiesを参照できない。
  • Loading branch information
shinosaki committed Aug 14, 2023
1 parent 5c9f8a2 commit 0b16ab9
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion front/.env.development
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PUBLIC_API_ENDPOINT = /api
PUBLIC_API_ENDPOINT = http://localhost:8787/api
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
11 changes: 0 additions & 11 deletions front/src/routes/(mail)/i/inbox/+page.js

This file was deleted.

13 changes: 13 additions & 0 deletions front/src/routes/(mail)/i/inbox/+page.server.js
Original file line number Diff line number Diff line change
@@ -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
};
}
11 changes: 0 additions & 11 deletions front/src/routes/(mail)/i/message/[id]/+page.js

This file was deleted.

13 changes: 13 additions & 0 deletions front/src/routes/(mail)/i/message/[id]/+page.server.js
Original file line number Diff line number Diff line change
@@ -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
};
}

0 comments on commit 0b16ab9

Please sign in to comment.