accountSecurityWrapper.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {cloneElement, useCallback} from 'react';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {fetchOrganizations} from 'sentry/actionCreators/organizations';
  4. import LoadingError from 'sentry/components/loadingError';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import {t} from 'sentry/locale';
  7. import type {Authenticator} from 'sentry/types/auth';
  8. import type {OrganizationSummary} from 'sentry/types/organization';
  9. import type {UserEmail} from 'sentry/types/user';
  10. import {defined} from 'sentry/utils';
  11. import {useApiQuery, useMutation, useQuery} from 'sentry/utils/queryClient';
  12. import useApi from 'sentry/utils/useApi';
  13. import {useParams} from 'sentry/utils/useParams';
  14. const ENDPOINT = '/users/me/authenticators/';
  15. interface Props {
  16. children: React.ReactElement;
  17. }
  18. function AccountSecurityWrapper({children}: Props) {
  19. const api = useApi();
  20. const {authId} = useParams<{authId?: string}>();
  21. const orgRequest = useQuery<OrganizationSummary[]>(
  22. ['organizations'],
  23. () => fetchOrganizations(api),
  24. {staleTime: 0}
  25. );
  26. const emailsRequest = useApiQuery<UserEmail[]>(['/users/me/emails/'], {staleTime: 0});
  27. const authenticatorsRequest = useApiQuery<Authenticator[]>([ENDPOINT], {staleTime: 0});
  28. const handleRefresh = useCallback(() => {
  29. orgRequest.refetch();
  30. authenticatorsRequest.refetch();
  31. emailsRequest.refetch();
  32. }, [orgRequest, authenticatorsRequest, emailsRequest]);
  33. const disableAuthenticatorMutation = useMutation(
  34. async (auth: Authenticator) => {
  35. if (!auth || !auth.authId) {
  36. return;
  37. }
  38. await api.requestPromise(`${ENDPOINT}${auth.authId}/`, {method: 'DELETE'});
  39. },
  40. {
  41. onSuccess: () => {
  42. handleRefresh();
  43. },
  44. onError: (_, auth) => {
  45. addErrorMessage(t('Error disabling %s', auth.name));
  46. },
  47. }
  48. );
  49. const regenerateBackupCodesMutation = useMutation(
  50. async () => {
  51. if (!authId) {
  52. return;
  53. }
  54. await api.requestPromise(`${ENDPOINT}${authId}/`, {
  55. method: 'PUT',
  56. });
  57. },
  58. {
  59. onSuccess: () => {
  60. handleRefresh();
  61. },
  62. onError: () => {
  63. addErrorMessage(t('Error regenerating backup codes'));
  64. },
  65. }
  66. );
  67. if (
  68. orgRequest.isLoading ||
  69. emailsRequest.isPending ||
  70. authenticatorsRequest.isPending ||
  71. disableAuthenticatorMutation.isLoading ||
  72. regenerateBackupCodesMutation.isLoading
  73. ) {
  74. return <LoadingIndicator />;
  75. }
  76. if (authenticatorsRequest.isError || emailsRequest.isError || orgRequest.isError) {
  77. return <LoadingError onRetry={handleRefresh} />;
  78. }
  79. const authenticators = authenticatorsRequest.data;
  80. const emails = emailsRequest.data;
  81. const organizations = orgRequest.data;
  82. const enrolled =
  83. authenticators.filter(auth => auth.isEnrolled && !auth.isBackupInterface) || [];
  84. const countEnrolled = enrolled.length;
  85. const orgsRequire2fa = organizations.filter(org => org.require2FA) || [];
  86. const deleteDisabled = orgsRequire2fa.length > 0 && countEnrolled === 1;
  87. const hasVerifiedEmail = !!emails.find(({isVerified}) => isVerified);
  88. // This happens when you switch between children views and the next child
  89. // view is lazy loaded, it can potentially be `null` while the code split
  90. // package is being fetched
  91. if (!defined(children)) {
  92. return null;
  93. }
  94. return cloneElement(children, {
  95. onDisable: disableAuthenticatorMutation.mutate,
  96. onRegenerateBackupCodes: regenerateBackupCodesMutation.mutate,
  97. authenticators,
  98. deleteDisabled,
  99. orgsRequire2fa,
  100. countEnrolled,
  101. hasVerifiedEmail,
  102. handleRefresh,
  103. });
  104. }
  105. export default AccountSecurityWrapper;