Skip to content

Commit 1c4ed8d

Browse files
evanpurkhiseriamrajjoshi
authored andcommitted
fix(sudo-modal): Wrap handleWebAuthn in useCallback to avoid re-trigger (#91464)
The WebAuthnAssert component was triggering multiple times here
1 parent 492d83b commit 1c4ed8d

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

static/app/components/modals/sudoModal.tsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Fragment, useState} from 'react';
1+
import {Fragment, useCallback, useState} from 'react';
22
import styled from '@emotion/styled';
33
import trimEnd from 'lodash/trimEnd';
44

@@ -148,7 +148,7 @@ function SudoModal({
148148
}
149149
};
150150

151-
const handleSuccess = () => {
151+
const handleSuccess = useCallback(() => {
152152
if (isSuperuser) {
153153
navigate(
154154
{pathname: location.pathname, state: {forceUpdate: new Date()}},
@@ -169,9 +169,9 @@ function SudoModal({
169169
setState(prevState => ({...prevState, showAccessForms: true}));
170170
closeModal();
171171
});
172-
};
172+
}, [closeModal, isSuperuser, location.pathname, navigate, needsReload, retryRequest]);
173173

174-
const handleError = (err: any) => {
174+
const handleError = useCallback((err: any) => {
175175
let newErrorType = ''; // Create a new variable to store the error type
176176

177177
if (err.status === 403) {
@@ -196,16 +196,25 @@ function SudoModal({
196196
errorType: newErrorType,
197197
showAccessForms: true,
198198
}));
199-
};
200-
201-
const handleWebAuthn = async (data: WebAuthnParams) => {
202-
data.isSuperuserModal = isSuperuser;
203-
data.superuserAccessCategory = state.superuserAccessCategory;
204-
data.superuserReason = state.superuserReason;
205-
// It's ok to throw from here, u2fInterface will handle it.
206-
await api.requestPromise('/auth/', {method: 'PUT', data});
207-
handleSuccess();
208-
};
199+
}, []);
200+
201+
const handleWebAuthn = useCallback(
202+
async (data: WebAuthnParams) => {
203+
data.isSuperuserModal = isSuperuser;
204+
data.superuserAccessCategory = state.superuserAccessCategory;
205+
data.superuserReason = state.superuserReason;
206+
// It's ok to throw from here, u2fInterface will handle it.
207+
await api.requestPromise('/auth/', {method: 'PUT', data});
208+
handleSuccess();
209+
},
210+
[
211+
api,
212+
handleSuccess,
213+
isSuperuser,
214+
state.superuserAccessCategory,
215+
state.superuserReason,
216+
]
217+
);
209218

210219
const getAuthLoginPath = (): string => {
211220
const authLoginPath = `/auth/login/?next=${encodeURIComponent(window.location.href)}`;

0 commit comments

Comments
 (0)