From cfaf19cc9d09e52432641405cc0a6a5c761e4e2a Mon Sep 17 00:00:00 2001 From: Snowball_233 Date: Mon, 26 Aug 2024 18:51:31 +0800 Subject: [PATCH] fix: admin detach and parts of page error fix --- api/AdminApi.ts | 71 +++++++++++++++++++++ app/(root)/backstage/components/Tools.tsx | 3 +- app/(root)/backstage/layout.tsx | 21 +++--- app/(root)/oauth/components/LoginBanner.tsx | 4 +- app/(root)/survey/[id]/page.tsx | 1 - 5 files changed, 85 insertions(+), 15 deletions(-) diff --git a/api/AdminApi.ts b/api/AdminApi.ts index c983c92..f11c9da 100644 --- a/api/AdminApi.ts +++ b/api/AdminApi.ts @@ -26,6 +26,72 @@ export default class AdminApi { return res.json(); } + + public static async getAdminTokenInfo(): Promise> { + const myHeaders = new Headers(); + myHeaders.append('token', Cookie.getCookie('token')); + + const requestOptions: RequestInit = { + method: 'GET', + headers: myHeaders, + redirect: 'follow', + }; + + const result: Response = await fetch(`${SERVER_URL}/api/token/admin`, requestOptions); + + if (!result.ok) { + return { + ok: false, + result: { + id: 0, + username: 'unknown', + disabled: true, + }, + }; + } + + const info: AdminInfo = await result.json(); + + return { + ok: true, + result: info, + }; + } + + public static async getTokenInfo(): Promise> { + const myHeaders = new Headers(); + myHeaders.append('token', Cookie.getCookie('token')); + + const requestOptions: RequestInit = { + method: 'GET', + headers: myHeaders, + redirect: 'follow', + }; + + const result: Response = await fetch(`${SERVER_URL}/api/token`, requestOptions); + + if (!result.ok) { + return { + ok: false, + result: { + uid: '0', + username: 'unknown', + }, + }; + } + + const info: UserInfo = await result.json(); + + return { + ok: true, + result: info, + }; + } +} + +export interface TokenQueryResponse { + ok: boolean; + result: T } export interface AdminInfo { @@ -33,3 +99,8 @@ export interface AdminInfo { username: string; disabled: boolean; } + +export interface UserInfo { + uid: string; + username: string; +} diff --git a/app/(root)/backstage/components/Tools.tsx b/app/(root)/backstage/components/Tools.tsx index f7d343e..7481396 100644 --- a/app/(root)/backstage/components/Tools.tsx +++ b/app/(root)/backstage/components/Tools.tsx @@ -12,7 +12,8 @@ export default function Tools() { function logOut() { Cookie.clearAllCookies(); - goHome(); + sessionStorage.setItem('logOutAndRedirect', 'true'); + window.location.reload(); } return ( diff --git a/app/(root)/backstage/layout.tsx b/app/(root)/backstage/layout.tsx index 8590b81..be06bec 100644 --- a/app/(root)/backstage/layout.tsx +++ b/app/(root)/backstage/layout.tsx @@ -22,18 +22,17 @@ export default function BackStageLayout({ children }: { children: React.ReactNod setUserName(decodeURI(userNameFromCookie)); setUserId(userIdFromCookie); - if (userIdFromCookie) { - AdminApi.getAdminInfo(Number(userIdFromCookie)) - .then(() => { - setLoading(false); - }) - .catch(() => { + AdminApi.getAdminTokenInfo() + .then((res) => { + if (!res.ok) { sessionStorage.setItem('adminAccessDenied', 'true'); - router.push('/'); - }); - } else { - router.push('/'); - } + // router.push('/'); + } + + setUserId(res.result.id.toString()); + setUserName(res.result.username); + setLoading(false); + }); } }, [router]); diff --git a/app/(root)/oauth/components/LoginBanner.tsx b/app/(root)/oauth/components/LoginBanner.tsx index cecfed0..cab36d1 100644 --- a/app/(root)/oauth/components/LoginBanner.tsx +++ b/app/(root)/oauth/components/LoginBanner.tsx @@ -24,8 +24,8 @@ export function LoginBanner() {
- - + +
diff --git a/app/(root)/survey/[id]/page.tsx b/app/(root)/survey/[id]/page.tsx index a27dbb5..2c7840e 100644 --- a/app/(root)/survey/[id]/page.tsx +++ b/app/(root)/survey/[id]/page.tsx @@ -110,7 +110,6 @@ export default function SurveyPage({ params }: { params: { id: number } }) { const newAnswers = new Map(answers); newAnswers.set(id, value); setAnswers(newAnswers); - // console.log(newAnswers); }; const getAnswerGetter = (id: string) => answers.get(id) || undefined;