index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {logout} from 'sentry/actionCreators/account';
  4. import {Alert} from 'sentry/components/alert';
  5. import {Button, LinkButton} from 'sentry/components/button';
  6. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  7. import ExternalLink from 'sentry/components/links/externalLink';
  8. import Link from 'sentry/components/links/link';
  9. import NarrowLayout from 'sentry/components/narrowLayout';
  10. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  11. import {t, tct} from 'sentry/locale';
  12. import ConfigStore from 'sentry/stores/configStore';
  13. import {space} from 'sentry/styles/space';
  14. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. type InviteDetails = {
  17. existingMember: boolean;
  18. hasAuthProvider: boolean;
  19. needs2fa: boolean;
  20. needsAuthentication: boolean;
  21. orgSlug: string;
  22. requireSso: boolean;
  23. ssoProvider?: string;
  24. };
  25. type Props = RouteComponentProps<{memberId: string; token: string; orgId?: string}, {}>;
  26. type State = DeprecatedAsyncComponent['state'] & {
  27. acceptError: boolean | undefined;
  28. accepting: boolean | undefined;
  29. inviteDetails: InviteDetails;
  30. };
  31. class AcceptOrganizationInvite extends DeprecatedAsyncComponent<Props, State> {
  32. disableErrorReport = false;
  33. get orgSlug(): string | null {
  34. const {params} = this.props;
  35. if (params.orgId) {
  36. return params.orgId;
  37. }
  38. const customerDomain = ConfigStore.get('customerDomain');
  39. if (customerDomain?.subdomain) {
  40. return customerDomain.subdomain;
  41. }
  42. return null;
  43. }
  44. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  45. const {memberId, token} = this.props.params;
  46. if (this.orgSlug) {
  47. return [['inviteDetails', `/accept-invite/${this.orgSlug}/${memberId}/${token}/`]];
  48. }
  49. return [['inviteDetails', `/accept-invite/${memberId}/${token}/`]];
  50. }
  51. handleLogout = (e: React.MouseEvent) => {
  52. e.preventDefault();
  53. logout(this.api);
  54. };
  55. handleLogoutAndRetry = (e: React.MouseEvent) => {
  56. const {memberId, token} = this.props.params;
  57. e.preventDefault();
  58. logout(this.api, `/accept/${memberId}/${token}/`);
  59. };
  60. handleAcceptInvite = async () => {
  61. const {memberId, token} = this.props.params;
  62. this.setState({accepting: true});
  63. try {
  64. if (this.orgSlug) {
  65. await this.api.requestPromise(
  66. `/accept-invite/${this.orgSlug}/${memberId}/${token}/`,
  67. {
  68. method: 'POST',
  69. }
  70. );
  71. } else {
  72. await this.api.requestPromise(`/accept-invite/${memberId}/${token}/`, {
  73. method: 'POST',
  74. });
  75. }
  76. // This forces a hard refresh, needed for the app to refetch the initial config
  77. // Please see https://github.com/getsentry/sentry/blob/5f1fef10806db1d4d048912702f5c12cb38c2c08/static/app/bootstrap/index.tsx#L20
  78. window.location.href = `/${this.state.inviteDetails.orgSlug}/`;
  79. } catch {
  80. this.setState({acceptError: true});
  81. }
  82. this.setState({accepting: false});
  83. };
  84. get existingMemberAlert() {
  85. const user = ConfigStore.get('user');
  86. return (
  87. <Alert type="warning" data-test-id="existing-member">
  88. {tct(
  89. 'Your account ([email]) is already a member of this organization. [switchLink:Switch accounts]?',
  90. {
  91. email: user.email,
  92. switchLink: (
  93. <Link
  94. to=""
  95. data-test-id="existing-member-link"
  96. onClick={this.handleLogout}
  97. />
  98. ),
  99. }
  100. )}
  101. </Alert>
  102. );
  103. }
  104. get authenticationActions() {
  105. const {inviteDetails} = this.state;
  106. return (
  107. <Fragment>
  108. {!inviteDetails.requireSso && (
  109. <p data-test-id="action-info-general">
  110. {t(
  111. `To continue, you must either create a new account, or login to an
  112. existing Sentry account.`
  113. )}
  114. </p>
  115. )}
  116. {inviteDetails.hasAuthProvider && (
  117. <p data-test-id="action-info-sso">
  118. {inviteDetails.requireSso
  119. ? tct(
  120. `Note that [orgSlug] has required Single Sign-On (SSO) using
  121. [authProvider]. You may create an account by authenticating with
  122. the organization's SSO provider.`,
  123. {
  124. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  125. authProvider: inviteDetails.ssoProvider,
  126. }
  127. )
  128. : tct(
  129. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  130. [authProvider]. You may create an account by authenticating with
  131. the organization's SSO provider.`,
  132. {
  133. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  134. authProvider: inviteDetails.ssoProvider,
  135. }
  136. )}
  137. </p>
  138. )}
  139. <Actions>
  140. <ActionsLeft>
  141. {inviteDetails.hasAuthProvider && (
  142. <LinkButton
  143. data-test-id="sso-login"
  144. priority="primary"
  145. href={`/auth/login/${inviteDetails.orgSlug}/`}
  146. >
  147. {t('Join with %s', inviteDetails.ssoProvider)}
  148. </LinkButton>
  149. )}
  150. {!inviteDetails.requireSso && (
  151. <LinkButton
  152. data-test-id="create-account"
  153. priority="primary"
  154. href="/auth/register/"
  155. >
  156. {t('Create a new account')}
  157. </LinkButton>
  158. )}
  159. </ActionsLeft>
  160. {!inviteDetails.requireSso && (
  161. <ExternalLink
  162. href="/auth/login/"
  163. openInNewTab={false}
  164. data-test-id="link-with-existing"
  165. >
  166. {t('Login using an existing account')}
  167. </ExternalLink>
  168. )}
  169. </Actions>
  170. </Fragment>
  171. );
  172. }
  173. get warning2fa() {
  174. const {inviteDetails} = this.state;
  175. return (
  176. <Fragment>
  177. <p data-test-id="2fa-warning">
  178. {tct(
  179. 'To continue, [orgSlug] requires all members to configure two-factor authentication.',
  180. {orgSlug: inviteDetails.orgSlug}
  181. )}
  182. </p>
  183. <Actions>
  184. <LinkButton priority="primary" to="/settings/account/security/">
  185. {t('Configure Two-Factor Auth')}
  186. </LinkButton>
  187. </Actions>
  188. </Fragment>
  189. );
  190. }
  191. get acceptActions() {
  192. const {inviteDetails, accepting} = this.state;
  193. return (
  194. <Fragment>
  195. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  196. <p data-test-id="action-info-sso">
  197. {tct(
  198. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  199. [authProvider]. You may join the organization by authenticating with
  200. the organization's SSO provider or via your standard account authentication.`,
  201. {
  202. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  203. authProvider: inviteDetails.ssoProvider,
  204. }
  205. )}
  206. </p>
  207. )}
  208. <Actions>
  209. <ActionsLeft>
  210. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  211. <LinkButton
  212. data-test-id="sso-login"
  213. priority="primary"
  214. href={`/auth/login/${inviteDetails.orgSlug}/`}
  215. >
  216. {t('Join with %s', inviteDetails.ssoProvider)}
  217. </LinkButton>
  218. )}
  219. <Button
  220. data-test-id="join-organization"
  221. priority="primary"
  222. disabled={accepting}
  223. onClick={this.handleAcceptInvite}
  224. >
  225. {t('Join the %s organization', inviteDetails.orgSlug)}
  226. </Button>
  227. </ActionsLeft>
  228. </Actions>
  229. </Fragment>
  230. );
  231. }
  232. renderError() {
  233. /**
  234. * NOTE (mifu67): this error view could show up for multiple reasons, including:
  235. * invite link expired, signed into account that is already in the inviting
  236. * org, and invite not approved. Previously, the message seemed to indivate that
  237. * the link had expired, regardless of which error prompted it, so update the
  238. * error message to be a little more helpful.
  239. */
  240. return (
  241. <NarrowLayout>
  242. <Alert type="warning">
  243. {tct(
  244. 'This organization invite link is invalid. It may be expired, or you may need to [switchLink:sign in with a different account].',
  245. {
  246. switchLink: (
  247. <Link
  248. to=""
  249. data-test-id="existing-member-link"
  250. onClick={this.handleLogoutAndRetry}
  251. />
  252. ),
  253. }
  254. )}
  255. </Alert>
  256. </NarrowLayout>
  257. );
  258. }
  259. renderBody() {
  260. const {inviteDetails, acceptError} = this.state;
  261. return (
  262. <NarrowLayout>
  263. <SentryDocumentTitle title={t('Accept Organization Invite')} />
  264. <SettingsPageHeader title={t('Accept organization invite')} />
  265. {acceptError && (
  266. <Alert type="error">
  267. {t('Failed to join this organization. Please try again')}
  268. </Alert>
  269. )}
  270. <InviteDescription data-test-id="accept-invite">
  271. {tct('[orgSlug] is using Sentry to track and debug errors.', {
  272. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  273. })}
  274. </InviteDescription>
  275. {inviteDetails.needsAuthentication
  276. ? this.authenticationActions
  277. : inviteDetails.existingMember
  278. ? this.existingMemberAlert
  279. : inviteDetails.needs2fa
  280. ? this.warning2fa
  281. : inviteDetails.requireSso
  282. ? this.authenticationActions
  283. : this.acceptActions}
  284. </NarrowLayout>
  285. );
  286. }
  287. }
  288. const Actions = styled('div')`
  289. display: flex;
  290. align-items: center;
  291. justify-content: space-between;
  292. margin-bottom: ${space(3)};
  293. `;
  294. const ActionsLeft = styled('span')`
  295. > a {
  296. margin-right: ${space(1)};
  297. }
  298. `;
  299. const InviteDescription = styled('p')`
  300. font-size: 1.2em;
  301. `;
  302. export default AcceptOrganizationInvite;