feat: setup auth schema

This commit is contained in:
Firman Ramdhani
2026-07-30 16:43:27 +07:00
parent 8a6a16ce9a
commit fdbc5dfe99
13 changed files with 217 additions and 45 deletions
@@ -22,3 +22,26 @@ export const defaultPrivileges: PrivilegeEntity = {
ALLOW_LOGS: true,
ALLOW_NOTES: true,
};
export const noPrivileges: PrivilegeEntity = {
ALLOW_VIEW: false,
ALLOW_CREATE: false,
ALLOW_EDIT: false,
ALLOW_DELETE: false,
ALLOW_PRINT: false,
ALLOW_PRINT_COPY: false,
ALLOW_APPROVAL: false,
ALLOW_ACTIVATE: false,
ALLOW_DEACTIVATE: false,
ALLOW_CONFIRM: false,
ALLOW_CANCEL: false,
ALLOW_ROLLBACK: false,
ALLOW_HOLD: false,
ALLOW_LOGS: false,
ALLOW_NOTES: false,
};
@@ -1,4 +1,4 @@
import { useMemo, useState, ReactNode } from 'react';
import { useMemo, useState, ReactNode, useEffect } from 'react';
import type { UseBoundStore, StoreApi } from 'zustand';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from '@repo/core-i18n';
@@ -19,7 +19,7 @@ import {
EnterpriseModalContext,
EnterpriseTranslationContext,
} from '../hooks/use-module.context';
import { defaultPrivileges } from '../constant/default-privilege';
import { defaultPrivileges, noPrivileges } from '../constant/default-privilege';
import { Forbidden } from '../../../components';
export interface EnterpriseModuleProviderProps<
@@ -45,19 +45,34 @@ export function EnterpriseModuleProvider<
// ---------------------------------------------------------------------------
// Config Slice (Static)
// ---------------------------------------------------------------------------
const [parsedPrivileges, setParsedPrivileges] = useState<PrivilegeEntity>(defaultPrivileges);
const storePrivileges = store((state: S) => state.privileges);
useEffect(() => {
const fetchPrivilegeData = async () => {
try {
// FIXME: Uncomment and complete the logic below to fetch real data.
// Currently, this is hardcoded to use `defaultPrivileges`.
// You need to retrieve the user privileges from the local database,
// extract the specific config using `moduleKey`, and apply the mapping function.
// const privilege: any = await appDatabase.getItem(AppDatabaseKey.USER_PRIVILEGE);
// const moduleKey: string | undefined = config?.moduleKey;
// const privilegeConfig = privilege[moduleKey];
// TODO: Replace this with the mapped result, e.g., setParsedPrivileges(mappingUserPrivilege(privilegeConfig));
setParsedPrivileges(defaultPrivileges);
} catch (error) {
setParsedPrivileges(noPrivileges);
console.error('Failed to retrieve privilege data:', error);
}
};
fetchPrivilegeData();
}, [config?.moduleKey]);
const configSlice: ConfigSlice = useMemo(() => {
// Parsing privilege: mapping string array from store to PrivilegeEntity format (boolean)
const parsedPrivileges: PrivilegeEntity = defaultPrivileges;
return {
config,
privileges: parsedPrivileges,
IS_MACOS: IS_MACOS,
};
}, [config, storePrivileges]);
return { config, privileges: parsedPrivileges, IS_MACOS: IS_MACOS };
}, [config, parsedPrivileges]);
// ---------------------------------------------------------------------------
// Translation Slice (Dedicated context — decoupled from config)