index.tsx 10 KB

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