From a6410d708a48e98a0b0dfa799124e759cbfc0cd3 Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Wed, 7 Jun 2023 10:42:40 +0900 Subject: [PATCH] =?UTF-8?q?=E8=AA=8D=E5=8F=AF=E3=81=AB=E3=82=AB=E3=82=B9?= =?UTF-8?q?=E3=82=BF=E3=83=A0=E8=A6=81=E7=B4=A0=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/auth.ts | 160 +---------------------------- src/contexts/AuthContext.tsx | 43 +++++++- src/layouts/dashbord/navigator.tsx | 2 +- src/routes/auth.ts | 35 +++++-- 4 files changed, 67 insertions(+), 173 deletions(-) diff --git a/src/api/auth.ts b/src/api/auth.ts index 332b1d5..28dfb42 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -9,7 +9,8 @@ type MeResponse = { contract_id: string; role: UserRole; name: string; - custom: CustomCode[]; + custom?: CustomCode[]; + contract_name?: string; }; } & APICommonResponse; @@ -44,160 +45,3 @@ export const logout = async () => { }); return res; }; -// export const getMe = async () => { -// const res = await request({ -// url: getUrl(ApiId.ME), -// method: HttpMethod.GET, -// }); -// return res; -// }; - -// export const login = async (email: string, password: string) => { -// const data = new URLSearchParams({ -// email, -// password, -// }); -// const res = await request({ -// url: getUrl(ApiId.NORMAL_LOGIN), -// method: HttpMethod.POST, -// data, -// }); -// return res; -// }; - -// export const loginAdmin = async (email: string, password: string) => { -// const data = new URLSearchParams({ -// email, -// password, -// }); -// const res = await request({ -// url: getUrl(ApiId.ADMIN_LOGIN), -// method: HttpMethod.POST, -// data, -// }); -// return res; -// }; - -// export const logout = async () => { -// const res = await request({ -// url: getUrl(ApiId.LOGOUT), -// method: HttpMethod.POST, -// }); -// return res; -// }; - -// export type StartEmailVerifyParams = { -// email: string; -// for_entry?: boolean; -// customer_code?: string; -// parking_management_code?: string; -// }; - -// export const startEmailVerify = async (data: StartEmailVerifyParams) => { -// const sendData = new URLSearchParams(makeParam(data)); -// const res = await request({ -// url: getUrl(ApiId.EMAIL_VERIFY_START), -// method: HttpMethod.POST, -// data: sendData, -// }); -// return res; -// }; - -// export const verifyEmail = async ({ token }: { token: string }) => { -// const sendData = new URLSearchParams(makeParam({ token })); -// const res = await request({ -// url: getUrl(ApiId.EMAIL_VERIFY), -// method: HttpMethod.POST, -// data: sendData, -// }); -// return res; -// }; - -// // パスワードリセット開始 -// export type ResetPasswordStartParams = { -// email: string; -// customer_code?: string; -// parking_management_code?: string; -// }; -// export const resetPasswordStart = async (data: ResetPasswordStartParams) => { -// const sendData = new URLSearchParams(makeParam(data)); -// const res = await request({ -// url: getUrl(ApiId.RESET_PASSWORD_START), -// method: HttpMethod.POST, -// data: sendData, -// }); -// return res; -// }; - -// // パスワードリセットトークンチェック -// export type ResetPasswordVerifyParams = { -// token: string; -// }; -// export const resetPasswordVerify = async (data: ResetPasswordVerifyParams) => { -// const sendData = new URLSearchParams(makeParam(data)); -// const res = await request({ -// url: getUrl(ApiId.RESET_PASSWORD_VERIFY), -// method: HttpMethod.POST, -// data: sendData, -// }); -// return res; -// }; - -// // パスワードリセット -// export type ResetPasswordParams = { -// password: string; -// token: string; -// }; -// export const resetPassword = async (data: ResetPasswordParams) => { -// const sendData = new URLSearchParams(makeParam(data)); -// const res = await request({ -// url: getUrl(ApiId.RESET_PASSWORD), -// method: HttpMethod.POST, -// data: sendData, -// }); -// return res; -// }; - -// // 利用者登録 -// export const RegisterUserParamKeyName = { -// TOKEN: 'token', -// PASSWORD: 'password', -// FIRST_NAME: 'first_name', -// LAST_NAME: 'last_name', -// FIRST_NAME_KANA: 'first_name_kana', -// LAST_NAME_KANA: 'last_name_kana', -// ZIP_CODE: 'zip_code', -// PREF_CODE: 'pref_code', -// ADDRESS1: 'address1', -// ADDRESS2: 'address2', -// ADDRESS3: 'address3', -// PHONE_NUMBER: 'phone_number', -// CONFIRM_PRIVACY_POLICY: 'confirm_privacy_policy', -// } as const; -// export type RegisterUserParamKeyName = -// typeof RegisterUserParamKeyName[keyof typeof RegisterUserParamKeyName]; - -// export type RegisterUserParam = { -// [RegisterUserParamKeyName.TOKEN]: string; -// [RegisterUserParamKeyName.PASSWORD]: string; -// [RegisterUserParamKeyName.FIRST_NAME]: string; -// [RegisterUserParamKeyName.LAST_NAME]: string; -// [RegisterUserParamKeyName.FIRST_NAME_KANA]: string; -// [RegisterUserParamKeyName.LAST_NAME_KANA]: string; -// [RegisterUserParamKeyName.ZIP_CODE]: string; -// [RegisterUserParamKeyName.PREF_CODE]: string; -// [RegisterUserParamKeyName.ADDRESS1]: string; -// [RegisterUserParamKeyName.ADDRESS2]: string; -// [RegisterUserParamKeyName.ADDRESS3]: string; -// [RegisterUserParamKeyName.PHONE_NUMBER]: string; -// [RegisterUserParamKeyName.CONFIRM_PRIVACY_POLICY]: boolean; -// }; -// export const registerUser = async (data: RegisterUserParam) => { -// const sendData = new URLSearchParams(makeParam(data)); -// const res = await request({ -// url: getUrl(ApiId.REGISTER_USER), -// method: HttpMethod.POST, -// data: sendData, -// }); -// return res; -// }; diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 30e2b46..1403bcb 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -71,7 +71,7 @@ function AuthContextProvider({ children }: Props) { setContractId(res.data.contract_id); setRole(res.data.role); setName(res.data.name); - setCustom(res.data.custom); + setCustom(res.data.custom ?? []); setInitialized(true); }, onFailed: () => { @@ -85,6 +85,9 @@ function AuthContextProvider({ children }: Props) { onSuccess: (res) => { setContractId(res.data.contract_id); setRole(res.data.role); + setName(res.data.name); + setCustom(res.data.custom ?? []); + setInitialized(true); }, }); @@ -98,6 +101,8 @@ function AuthContextProvider({ children }: Props) { const clear = () => { setRole(UserRole.NONE); setContractId(null); + setName(""); + setCustom([]); }; const login = async (email: string, password: string) => { @@ -125,10 +130,40 @@ function AuthContextProvider({ children }: Props) { const canAccess = useCallback( (pageId: PageID): boolean => { - const roles = AUTH[pageId] ?? []; - return roles.includes(role); + const authorization = AUTH[pageId]; + + // デバッグ用 + // console.log( + // "RET", + // pageId, + // role, + // custom, + // !!authorization && + // authorization.role.includes(role) && + // (authorization.custom.length === 0 || + // !!custom.find((c) => { + // return authorization.custom.includes(c); + // })), + // [ + // !!authorization, + // authorization.role.includes(role), + // authorization.custom.length === 0, + // !!custom.find((c) => { + // return authorization.custom.includes(c); + // }), + // ] + // ); + + return ( + !!authorization && + authorization.role.includes(role) && + (authorization.custom.length === 0 || + !!custom.find((c) => { + return authorization.custom.includes(c); + })) + ); }, - [role] + [initialized, role, custom] ); useEffect(() => { diff --git a/src/layouts/dashbord/navigator.tsx b/src/layouts/dashbord/navigator.tsx index 0b56604..ddbbf9c 100644 --- a/src/layouts/dashbord/navigator.tsx +++ b/src/layouts/dashbord/navigator.tsx @@ -224,7 +224,7 @@ function useContents(children: Child[]) { ); }); - }, [pageId, initialized]); + }, [pageId, initialized, canAccess]); return { elements, diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 64e86af..ec6b839 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -1,6 +1,11 @@ +import { CustomCode as C } from "codes/custom"; import { PageID as P } from "codes/page"; import { UserRole as R } from "codes/user"; +type AuthConfiguration = { + role: R[]; + custom: C[]; +}; export const AUTH = { [P.NONE]: setAuth("all"), [P.LOGIN]: setAuth("all"), @@ -13,15 +18,18 @@ export const AUTH = { [P.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE_CUSTOM_HELLO_TECHNO]: setAuth( "ge", - R.NORMAL_ADMIN + R.NORMAL_ADMIN, + [C.HELLO_TECHNO] ), [P.DASHBOARD_RECEIPT_ISSUING_ORDER_LIST_CUSTOM_HELLO_TECHNO]: setAuth( "ge", - R.NORMAL_ADMIN + R.NORMAL_ADMIN, + [C.HELLO_TECHNO] ), [P.DASHBOARD_RECEIPT_ISSUING_ORDER_DETAIL_CUSTOM_HELLO_TECHNO]: setAuth( "ge", - R.NORMAL_ADMIN + R.NORMAL_ADMIN, + [C.HELLO_TECHNO] ), [P.PAGE_403]: setAuth("all"), @@ -30,14 +38,18 @@ export const AUTH = { type Target = "ge" | "le" | "eq" | "all"; type UserRoleKey = keyof typeof R; -function setAuth(target: Target, targetRole?: R): R[] { - const ret: R[] = []; +function setAuth( + target: Target, + targetRole?: R, + custom: C[] = [] +): AuthConfiguration { + const roles: R[] = []; for (const key in R) { const role = R[key as UserRoleKey]; if (target === "all") { - ret.push(role); + roles.push(role); continue; } @@ -46,18 +58,21 @@ function setAuth(target: Target, targetRole?: R): R[] { } if (target === "ge" && role >= targetRole) { - ret.push(role); + roles.push(role); continue; } if (target === "le" && role <= targetRole) { - ret.push(role); + roles.push(role); continue; } if (target === "eq" && role === targetRole) { - ret.push(role); + roles.push(role); continue; } } - return ret; + return { + role: roles, + custom: custom, + }; }