mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-30 08:48:12 +00:00
feat: implement two-factor authentication flow during sign-in and add utility hooks for configuration management
This commit is contained in:
21
libs/auth/src/lib/hooks/useTwoFactor.ts
Normal file
21
libs/auth/src/lib/hooks/useTwoFactor.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { useApiMutation, useApiQuery } from "@ema-platform/api";
|
||||
import { twoFactorRequest } from "./two-factor-request";
|
||||
|
||||
type AccountConfig = { id: string; isMFARequired: boolean };
|
||||
|
||||
/** Reads and writes the signed-in user's IAM two-step verification setting. */
|
||||
export function useTwoFactor() {
|
||||
const { data, refetch, isLoading } = useApiQuery<{ items: AccountConfig[] }>({
|
||||
url: "/account-configurations/my-config",
|
||||
});
|
||||
const [save, { isLoading: isSaving }] = useApiMutation();
|
||||
|
||||
const config = data?.items?.[0];
|
||||
|
||||
const setEnabled = async (isMFARequired: boolean) => {
|
||||
await save(twoFactorRequest(config, isMFARequired)).unwrap();
|
||||
await refetch();
|
||||
};
|
||||
|
||||
return { enabled: !!config?.isMFARequired, isLoading, isSaving, setEnabled };
|
||||
}
|
||||
Reference in New Issue
Block a user