@@ -0,0 +1,26 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {UseFormReturn} from 'react-hook-form';
|
||||
import {onFormQueryError} from '@common/errors/on-form-query-error';
|
||||
|
||||
interface Response extends BackendResponse {}
|
||||
|
||||
export interface ConfirmTwoFactorPayload {
|
||||
code: string;
|
||||
}
|
||||
|
||||
export function useConfirmTwoFactor(
|
||||
form: UseFormReturn<ConfirmTwoFactorPayload>,
|
||||
) {
|
||||
return useMutation({
|
||||
mutationFn: (payload: ConfirmTwoFactorPayload) => confirm(payload),
|
||||
onError: r => onFormQueryError(r, form),
|
||||
});
|
||||
}
|
||||
|
||||
function confirm(payload: ConfirmTwoFactorPayload): Promise<Response> {
|
||||
return apiClient
|
||||
.post('auth/user/confirmed-two-factor-authentication', payload)
|
||||
.then(response => response.data);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
|
||||
interface Response extends BackendResponse {}
|
||||
|
||||
export function useDisableTwoFactor() {
|
||||
return useMutation({
|
||||
mutationFn: disable,
|
||||
onError: r => showHttpErrorToast(r),
|
||||
});
|
||||
}
|
||||
|
||||
function disable(): Promise<Response> {
|
||||
return apiClient
|
||||
.delete('auth/user/two-factor-authentication')
|
||||
.then(response => response.data);
|
||||
}
|
||||
19
common/resources/client/auth/ui/two-factor/requests/use-enable-two-factor.ts
Executable file
19
common/resources/client/auth/ui/two-factor/requests/use-enable-two-factor.ts
Executable file
@@ -0,0 +1,19 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
|
||||
interface Response extends BackendResponse {}
|
||||
|
||||
export function useEnableTwoFactor() {
|
||||
return useMutation({
|
||||
mutationFn: enable,
|
||||
onError: r => showHttpErrorToast(r),
|
||||
});
|
||||
}
|
||||
|
||||
function enable(): Promise<Response> {
|
||||
return apiClient
|
||||
.post('auth/user/two-factor-authentication')
|
||||
.then(response => response.data);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
|
||||
interface Response extends BackendResponse {}
|
||||
|
||||
export function useRegenerateTwoFactorCodes() {
|
||||
return useMutation({
|
||||
mutationFn: () => regenerate(),
|
||||
onError: r => showHttpErrorToast(r),
|
||||
});
|
||||
}
|
||||
|
||||
function regenerate(): Promise<Response> {
|
||||
return apiClient
|
||||
.post('auth/user/two-factor-recovery-codes')
|
||||
.then(response => response.data);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {UseFormReturn} from 'react-hook-form';
|
||||
import {onFormQueryError} from '@common/errors/on-form-query-error';
|
||||
import {useHandleLoginSuccess} from '@common/auth/requests/use-login';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
bootstrapData: string;
|
||||
two_factor: false;
|
||||
}
|
||||
|
||||
export interface TwoFactorChallengePayload {
|
||||
code?: string;
|
||||
recovery_code?: string;
|
||||
}
|
||||
export function useTwoFactorChallenge(
|
||||
form: UseFormReturn<TwoFactorChallengePayload>,
|
||||
) {
|
||||
const handleSuccess = useHandleLoginSuccess();
|
||||
return useMutation({
|
||||
mutationFn: (payload: TwoFactorChallengePayload) =>
|
||||
completeChallenge(payload),
|
||||
onSuccess: response => {
|
||||
handleSuccess(response);
|
||||
},
|
||||
onError: r => onFormQueryError(r, form),
|
||||
});
|
||||
}
|
||||
|
||||
function completeChallenge(
|
||||
payload: TwoFactorChallengePayload,
|
||||
): Promise<Response> {
|
||||
return apiClient
|
||||
.post('auth/two-factor-challenge', payload)
|
||||
.then(response => response.data);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import {useQuery} from '@tanstack/react-query';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
svg: string;
|
||||
secret: string;
|
||||
}
|
||||
|
||||
export function useTwoFactorQrCode() {
|
||||
return useQuery({
|
||||
queryKey: ['two-factor-qr-code'],
|
||||
queryFn: () => fetchCode(),
|
||||
});
|
||||
}
|
||||
|
||||
function fetchCode(): Promise<Response> {
|
||||
return apiClient
|
||||
.get('auth/user/two-factor/qr-code')
|
||||
.then(response => response.data);
|
||||
}
|
||||
Reference in New Issue
Block a user