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
@@ -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