Skip to content

Commit ec6f2ed

Browse files
committed
handleMfaStartError helper
1 parent 8648354 commit ec6f2ed

4 files changed

Lines changed: 34 additions & 24 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { LocationInfo } from '../../../rust-api/types';
2+
import { shouldShowPostureError } from '../api/startClientMfaSession';
3+
import { LocationCardViews, type LocationCardViewsValue } from '../context/types';
4+
5+
type HandleMfaStartErrorParams = {
6+
err: unknown;
7+
location: LocationInfo;
8+
setPostureError: (error: string | null) => void;
9+
setView: (view: LocationCardViewsValue) => void;
10+
};
11+
12+
export const handleMfaStartError = ({
13+
err,
14+
location,
15+
setPostureError,
16+
setView,
17+
}: HandleMfaStartErrorParams): boolean => {
18+
if (!shouldShowPostureError(err, location)) {
19+
return false;
20+
}
21+
22+
setPostureError(err.message);
23+
setView(LocationCardViews.PostureCheckFail);
24+
return true;
25+
};

new-ui/src/shared/components/LocationCard/hooks/useMfaConnect.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import { useCallback, useEffect, useRef, useState } from 'react';
55
import { api } from '../../../rust-api/api';
66
import { getInstancesQueryOptions } from '../../../rust-api/query';
77
import type { EdgeRequestHeaders } from '../../../rust-api/types';
8-
import {
9-
CLIENT_MFA_ENDPOINT,
10-
shouldShowPostureError,
11-
startClientMfaSession,
12-
} from '../api/startClientMfaSession';
8+
import { CLIENT_MFA_ENDPOINT, startClientMfaSession } from '../api/startClientMfaSession';
139
import { useLocationCardContext } from '../context/context';
1410
import { LocationCardViews } from '../context/types';
11+
import { handleMfaStartError } from './handleMfaStartError';
1512

1613
type MfaFinishResponse = {
1714
preshared_key: string;
@@ -66,9 +63,7 @@ export const useMfaConnect = (method: 0 | 1) => {
6663
setRequestHeaders(headers);
6764
setToken(response.token);
6865
} catch (err) {
69-
if (shouldShowPostureError(err, location)) {
70-
setPostureError(err.message);
71-
setView(LocationCardViews.PostureCheckFail);
66+
if (handleMfaStartError({ err, location, setPostureError, setView })) {
7267
return;
7368
}
7469
setStartError(err instanceof Error ? err.message : 'Failed to start MFA');

new-ui/src/shared/components/LocationCard/hooks/useMfaMobileConnect.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import { useMutation } from '@tanstack/react-query';
33
import { error } from '@tauri-apps/plugin-log';
44
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
55
import { api } from '../../../rust-api/api';
6-
import {
7-
CLIENT_MFA_ENDPOINT,
8-
shouldShowPostureError,
9-
startClientMfaSession,
10-
} from '../api/startClientMfaSession';
6+
import { CLIENT_MFA_ENDPOINT, startClientMfaSession } from '../api/startClientMfaSession';
117
import { useLocationCardContext } from '../context/context';
128
import { LocationCardViews } from '../context/types';
9+
import { handleMfaStartError } from './handleMfaStartError';
1310

1411
type TokenData = {
1512
token: string;
@@ -150,9 +147,7 @@ export const useMfaMobileConnect = () => {
150147

151148
setTokenData({ token: response.token, challenge: response.challenge });
152149
} catch (e) {
153-
if (shouldShowPostureError(e, location)) {
154-
setPostureError(e.message);
155-
setView(LocationCardViews.PostureCheckFail);
150+
if (handleMfaStartError({ err: e, location, setPostureError, setView })) {
156151
return;
157152
}
158153
setStartError(

new-ui/src/shared/components/LocationCard/hooks/useMfaOidcConnect.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import { error } from '@tauri-apps/plugin-log';
44
import { useCallback, useEffect, useRef, useState } from 'react';
55
import { api } from '../../../rust-api/api';
66
import { getInstancesQueryOptions } from '../../../rust-api/query';
7-
import {
8-
CLIENT_MFA_ENDPOINT,
9-
shouldShowPostureError,
10-
startClientMfaSession,
11-
} from '../api/startClientMfaSession';
7+
import { CLIENT_MFA_ENDPOINT, startClientMfaSession } from '../api/startClientMfaSession';
128
import { useLocationCardContext } from '../context/context';
139
import { LocationCardViews } from '../context/types';
10+
import { handleMfaStartError } from './handleMfaStartError';
1411

1512
const POLL_INTERVAL_MS = 5_000;
1613
const POLL_TIMEOUT_MS = 5 * 60 * 1_000; // 5 minutes
@@ -140,9 +137,7 @@ export const useMfaOidcConnect = () => {
140137
await api.openLink(`${instance.proxy_url}openid/mfa?token=${response.token}`);
141138
startPolling(response.token, instance.proxy_url, headers);
142139
} catch (e) {
143-
if (shouldShowPostureError(e, location)) {
144-
setPostureError(e.message);
145-
setView(LocationCardViews.PostureCheckFail);
140+
if (handleMfaStartError({ err: e, location, setPostureError, setView })) {
146141
return;
147142
}
148143
setStartError(

0 commit comments

Comments
 (0)