feat(2FA): create hooks to use API
This commit is contained in:
1 parent
ab73667d7b
commit
4a7e337954
3 files changed
+44
No files matched your search
@@ -32,6 +32,8 @@ export * from './queries/useLinkQuery';
|
||||
export * from './queries/useLinkSharesQuery';
|
||||
export * from './queries/useLinksQuery';
|
||||
export * from './queries/useLoginQuery';
|
||||
export * from './queries/useTwoFactorStatusQuery';
|
||||
export * from './queries/useTwoFactorVerifyMutation';
|
||||
export * from './queries/usePixelQuery';
|
||||
export * from './queries/usePixelSharesQuery';
|
||||
export * from './queries/usePixelsQuery';
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { useApi } from '../useApi';
|
||||
|
||||
export function useTwoFactorStatusQuery(enabled: boolean) {
|
||||
const { get, useQuery } = useApi();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['2fa-status'],
|
||||
queryFn: () => get('/2fa/status'),
|
||||
enabled,
|
||||
refetchOnWindowFocus: true,
|
||||
refetchOnReconnect: true,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { httpPost } from '@/lib/fetch';
|
||||
import { useApi } from '../useApi';
|
||||
|
||||
interface TwoFactorVerifyParams {
|
||||
partialToken: string;
|
||||
token?: string;
|
||||
backupCode?: string;
|
||||
}
|
||||
|
||||
export function useTwoFactorVerifyMutation() {
|
||||
const { useMutation } = useApi();
|
||||
const basePath = process.env.basePath || '';
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ partialToken, token, backupCode }: TwoFactorVerifyParams) => {
|
||||
const body = backupCode ? { backupCode } : { token };
|
||||
const res = await httpPost(`${basePath}/api/2fa/verify`, body, {
|
||||
Authorization: `Bearer ${partialToken}`,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const { message, code, lockedUntil } = res?.data?.error || {};
|
||||
throw Object.assign(new Error(message), { code, status: res.status, lockedUntil });
|
||||
}
|
||||
|
||||
return res.data as { token: string; user: any };
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user