From 0bab1f2e797575dda820d7513d399b9c66417860 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 29 Dec 2023 17:52:01 +0900 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20=E5=9B=9E=E7=AD=94=E3=81=8C=E9=80=81?= =?UTF-8?q?=E4=BF=A1=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=84=E3=81=AE=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/features/form/api/form.ts | 6 +++--- src/features/user/api/mcToken.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/features/form/api/form.ts b/src/features/form/api/form.ts index 49671190..1be7f42f 100644 --- a/src/features/form/api/form.ts +++ b/src/features/form/api/form.ts @@ -59,11 +59,11 @@ export const postAnswers = async ( token: string ): Promise => { const answersJson = JSON.stringify({ - uuid: '3fa85f64-5717-4562-b3fc-2c963f66afa6', //todo: user側の処理を実装したら書き換える - timestamp: new Date(), - form_id, + title: 'aaa', + form_id: Number(form_id), answers, }); + const response = await fetch(`http://localhost:9000/forms/answers`, { method: 'POST', headers: { diff --git a/src/features/user/api/mcToken.ts b/src/features/user/api/mcToken.ts index a839becf..40132832 100644 --- a/src/features/user/api/mcToken.ts +++ b/src/features/user/api/mcToken.ts @@ -6,7 +6,7 @@ import type { RequestCookies } from 'next/dist/compiled/@edge-runtime/cookies'; const KEY = 'SEICHI_PORTAL__MC_TOKEN'; -export const getCachedToken = (cookie?: RequestCookies) => { +export const getCachedToken = (cookie?: RequestCookies): string | undefined => { const cache = cookie ? cookie.get(KEY) : cookies().get(KEY); return cache?.value; From 9b2705a310b20e6ed010a99347d30361ff44bb6a Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 29 Dec 2023 21:33:53 +0900 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=E5=9B=9E=E7=AD=94=E3=81=8C=E9=80=81?= =?UTF-8?q?=E4=BF=A1=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=84=E3=81=AE=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/admin/page.tsx | 2 +- src/app/forms/[formId]/page.tsx | 2 +- src/app/forms/page.tsx | 2 +- src/features/form/api/form.ts | 1 - src/features/form/components/AnswerForm.tsx | 2 +- src/features/user/api/mcToken.ts | 8 +++++--- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx index cbf9af95..b3910c6b 100644 --- a/src/app/admin/page.tsx +++ b/src/app/admin/page.tsx @@ -6,7 +6,7 @@ import { getCachedToken } from '@/features/user/api/mcToken'; import styles from '../page.module.css'; const Home = async () => { - const token = getCachedToken() ?? ''; + const token = (await getCachedToken()) ?? ''; const answers = await getAllAnswers(token); return (
diff --git a/src/app/forms/[formId]/page.tsx b/src/app/forms/[formId]/page.tsx index 4103a088..1c3834db 100644 --- a/src/app/forms/[formId]/page.tsx +++ b/src/app/forms/[formId]/page.tsx @@ -3,7 +3,7 @@ import AnswerForm from '@/features/form/components/AnswerForm'; import { getCachedToken } from '@/features/user/api/mcToken'; const Home = async ({ params }: { params: { formId: number } }) => { - const token = getCachedToken() ?? ''; + const token = (await getCachedToken()) ?? ''; const questions = await getFormQuestions(params.formId, token); return ; diff --git a/src/app/forms/page.tsx b/src/app/forms/page.tsx index ef074e6a..a6deea77 100644 --- a/src/app/forms/page.tsx +++ b/src/app/forms/page.tsx @@ -3,7 +3,7 @@ import FormList from '@/features/form/components/FormList'; import { getCachedToken } from '@/features/user/api/mcToken'; const Home = async () => { - const token = getCachedToken() ?? ''; + const token = (await getCachedToken()) ?? ''; const forms = await getForms(token); return ; }; diff --git a/src/features/form/api/form.ts b/src/features/form/api/form.ts index 1be7f42f..9ece1422 100644 --- a/src/features/form/api/form.ts +++ b/src/features/form/api/form.ts @@ -59,7 +59,6 @@ export const postAnswers = async ( token: string ): Promise => { const answersJson = JSON.stringify({ - title: 'aaa', form_id: Number(form_id), answers, }); diff --git a/src/features/form/components/AnswerForm.tsx b/src/features/form/components/AnswerForm.tsx index 2a3a1912..f68823fe 100644 --- a/src/features/form/components/AnswerForm.tsx +++ b/src/features/form/components/AnswerForm.tsx @@ -94,7 +94,7 @@ const AnswerForm = ({ questions: questions, formId }: Props) => { } }); - const token = getCachedToken() ?? ''; + const token = await getCachedToken() ?? ''; if (await postAnswers(formId, formAnswers, token)) { toggleIsSubmitted(true); reset(); diff --git a/src/features/user/api/mcToken.ts b/src/features/user/api/mcToken.ts index 40132832..26d75b27 100644 --- a/src/features/user/api/mcToken.ts +++ b/src/features/user/api/mcToken.ts @@ -6,10 +6,12 @@ import type { RequestCookies } from 'next/dist/compiled/@edge-runtime/cookies'; const KEY = 'SEICHI_PORTAL__MC_TOKEN'; -export const getCachedToken = (cookie?: RequestCookies): string | undefined => { - const cache = cookie ? cookie.get(KEY) : cookies().get(KEY); +export const getCachedToken = (cookie?: RequestCookies): Promise => { + return new Promise((resolve, _reject) => { + const cache = cookie ? cookie.get(KEY) : cookies().get(KEY); - return cache?.value; + resolve(cache?.value); + }) }; export const saveTokenToCache = ({ From f673d90655bb04edb351707f89e78084a5d8fcc7 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 29 Dec 2023 21:51:59 +0900 Subject: [PATCH 3/6] style: fmt, lint --- src/features/form/components/AnswerForm.tsx | 2 +- src/features/user/api/mcToken.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/features/form/components/AnswerForm.tsx b/src/features/form/components/AnswerForm.tsx index f68823fe..55570123 100644 --- a/src/features/form/components/AnswerForm.tsx +++ b/src/features/form/components/AnswerForm.tsx @@ -94,7 +94,7 @@ const AnswerForm = ({ questions: questions, formId }: Props) => { } }); - const token = await getCachedToken() ?? ''; + const token = (await getCachedToken()) ?? ''; if (await postAnswers(formId, formAnswers, token)) { toggleIsSubmitted(true); reset(); diff --git a/src/features/user/api/mcToken.ts b/src/features/user/api/mcToken.ts index 26d75b27..754bbfa6 100644 --- a/src/features/user/api/mcToken.ts +++ b/src/features/user/api/mcToken.ts @@ -6,12 +6,14 @@ import type { RequestCookies } from 'next/dist/compiled/@edge-runtime/cookies'; const KEY = 'SEICHI_PORTAL__MC_TOKEN'; -export const getCachedToken = (cookie?: RequestCookies): Promise => { +export const getCachedToken = ( + cookie?: RequestCookies +): Promise => { return new Promise((resolve, _reject) => { const cache = cookie ? cookie.get(KEY) : cookies().get(KEY); resolve(cache?.value); - }) + }); }; export const saveTokenToCache = ({ From fb34387864c52d2344c1d4d789ed6a96794be8ba Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 29 Dec 2023 21:59:14 +0900 Subject: [PATCH 4/6] style: lint --- src/features/user/components/AuthenticatedTemplate.tsx | 4 ++-- src/features/user/components/UnauthenticatedTemplate.tsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/features/user/components/AuthenticatedTemplate.tsx b/src/features/user/components/AuthenticatedTemplate.tsx index 10cec9de..b289e4d0 100644 --- a/src/features/user/components/AuthenticatedTemplate.tsx +++ b/src/features/user/components/AuthenticatedTemplate.tsx @@ -3,8 +3,8 @@ import type { ReactNode } from 'react'; type Props = { children: ReactNode }; -export const AuthenticatedTemplate = ({ children }: Props) => { - const isAuthenticated = !!getCachedToken(); +export const AuthenticatedTemplate = async ({ children }: Props) => { + const isAuthenticated = !!await getCachedToken(); return isAuthenticated ? <>{children} : null; }; diff --git a/src/features/user/components/UnauthenticatedTemplate.tsx b/src/features/user/components/UnauthenticatedTemplate.tsx index 3316545a..c38ba96f 100644 --- a/src/features/user/components/UnauthenticatedTemplate.tsx +++ b/src/features/user/components/UnauthenticatedTemplate.tsx @@ -3,8 +3,8 @@ import type { ReactNode } from 'react'; type Props = { children: ReactNode }; -export const UnauthenticatedTemplate = ({ children }: Props) => { - const isUnauthenticated = !getCachedToken(); +export const UnauthenticatedTemplate = async ({ children }: Props) => { + const isUnauthenticated = !await getCachedToken(); return isUnauthenticated ? <>{children} : null; }; From 1c169c6c8da005018bb7fe9ffbb52f48af639539 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:01:32 +0900 Subject: [PATCH 5/6] style: apply fmt --- src/features/user/components/AuthenticatedTemplate.tsx | 2 +- src/features/user/components/UnauthenticatedTemplate.tsx | 2 +- src/middleware.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/features/user/components/AuthenticatedTemplate.tsx b/src/features/user/components/AuthenticatedTemplate.tsx index b289e4d0..44737f89 100644 --- a/src/features/user/components/AuthenticatedTemplate.tsx +++ b/src/features/user/components/AuthenticatedTemplate.tsx @@ -4,7 +4,7 @@ import type { ReactNode } from 'react'; type Props = { children: ReactNode }; export const AuthenticatedTemplate = async ({ children }: Props) => { - const isAuthenticated = !!await getCachedToken(); + const isAuthenticated = !!(await getCachedToken()); return isAuthenticated ? <>{children} : null; }; diff --git a/src/features/user/components/UnauthenticatedTemplate.tsx b/src/features/user/components/UnauthenticatedTemplate.tsx index c38ba96f..12371509 100644 --- a/src/features/user/components/UnauthenticatedTemplate.tsx +++ b/src/features/user/components/UnauthenticatedTemplate.tsx @@ -4,7 +4,7 @@ import type { ReactNode } from 'react'; type Props = { children: ReactNode }; export const UnauthenticatedTemplate = async ({ children }: Props) => { - const isUnauthenticated = !await getCachedToken(); + const isUnauthenticated = !(await getCachedToken()); return isUnauthenticated ? <>{children} : null; }; diff --git a/src/middleware.ts b/src/middleware.ts index 3eb88316..3d38b92b 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -2,7 +2,7 @@ import { NextResponse } from 'next/server'; import { getCachedToken } from '@/features/user/api/mcToken'; import type { NextRequest } from 'next/server'; -export const middleware = (request: NextRequest) => { +export const middleware = async (request: NextRequest) => { if (request.method !== 'GET') { return; } @@ -11,7 +11,7 @@ export const middleware = (request: NextRequest) => { return; } - if (!!getCachedToken(request.cookies)) { + if (!!await getCachedToken(request.cookies)) { return; } From 4b7362b2e4368446b15a8919e69ad8e46d45e90c Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:03:45 +0900 Subject: [PATCH 6/6] style: apply fmt --- src/middleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middleware.ts b/src/middleware.ts index 3d38b92b..d15a896e 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -11,7 +11,7 @@ export const middleware = async (request: NextRequest) => { return; } - if (!!await getCachedToken(request.cookies)) { + if (!!(await getCachedToken(request.cookies))) { return; }