From bb96d8b1e2fb6cd4495a8ddd9e4045a63a0e8105 Mon Sep 17 00:00:00 2001 From: "MSI\\2Fi" Date: Wed, 11 Jun 2025 18:01:26 +0800 Subject: [PATCH] Modify Session Type --- src/components/PoDetail/PoInputGrid.tsx | 6 +++++- src/config/authConfig.ts | 4 +++- types/next-auth.d.ts | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 types/next-auth.d.ts diff --git a/src/components/PoDetail/PoInputGrid.tsx b/src/components/PoDetail/PoInputGrid.tsx index ccb565a..6afb7fb 100644 --- a/src/components/PoDetail/PoInputGrid.tsx +++ b/src/components/PoDetail/PoInputGrid.tsx @@ -56,6 +56,7 @@ import { fetchPoQrcode } from "@/app/api/pdf/actions"; import { fetchQcResult } from "@/app/api/qc/actions"; import PoQcStockInModal from "./PoQcStockInModal" import DoDisturbIcon from "@mui/icons-material/DoDisturb"; +import { useSession } from "next-auth/react"; interface ResultWithId { id: number; @@ -135,6 +136,8 @@ function PoInputGrid({ return total; }); + const { data: session } = useSession(); + useEffect(() => { const completedList = entries.filter((e) => e.status === "completed"); const processedQty = completedList.reduce( @@ -475,7 +478,8 @@ function PoInputGrid({ disabled={ stockInLineStatusMap[status] === 9 || stockInLineStatusMap[status] <= 2 || - stockInLineStatusMap[status] >= 7 + stockInLineStatusMap[status] >= 7 || + (!session?.user?.abilities?.includes("APPROVAL") && stockInLineStatusMap[status] >= 3 && stockInLineStatusMap[status] <= 5) } // set _isNew to false after posting // or check status diff --git a/src/config/authConfig.ts b/src/config/authConfig.ts index 95de947..90010d6 100644 --- a/src/config/authConfig.ts +++ b/src/config/authConfig.ts @@ -28,7 +28,6 @@ export const authOptions: AuthOptions = { }); const user = await res.json(); - if (res.ok && user) { return user; @@ -56,6 +55,9 @@ export const authOptions: AuthOptions = { refreshToken: token.refreshToken as string | undefined, abilities: token.abilities as string[] }; + if (sessionWithToken.user) { + sessionWithToken.user.abilities = token.abilities as string[]; + } return sessionWithToken; }, }, diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts new file mode 100644 index 0000000..c0d27ea --- /dev/null +++ b/types/next-auth.d.ts @@ -0,0 +1,14 @@ +// types/next-auth.d.ts +import NextAuth from "next-auth"; + +declare module "next-auth" { + interface Session { + user?: { + name?: string | null; + email?: string | null; + image?: string | null; + abilities?: string[] | null; // <-- Add your custom fields here + }; + expires: ISODateString; + } +} \ No newline at end of file