index.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 ExternalLink from 'sentry/components/links/externalLink';
  7. import Link from 'sentry/components/links/link';
  8. import NarrowLayout from 'sentry/components/narrowLayout';
  9. import {t, tct} from 'sentry/locale';
  10. import ConfigStore from 'sentry/stores/configStore';
  11. import {space} from 'sentry/styles/space';
  12. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  13. import {browserHistory} from 'sentry/utils/browserHistory';
  14. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  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 = DeprecatedAsyncView['state'] & {
  27. acceptError: boolean | undefined;
  28. accepting: boolean | undefined;
  29. inviteDetails: InviteDetails;
  30. };
  31. class AcceptOrganizationInvite extends DeprecatedAsyncView<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<DeprecatedAsyncView['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. getTitle() {
  52. return t('Accept Organization Invite');
  53. }
  54. handleLogout = (e: React.MouseEvent) => {
  55. e.preventDefault();
  56. logout(this.api);
  57. };
  58. handleLogoutAndRetry = (e: React.MouseEvent) => {
  59. const {memberId, token} = this.props.params;
  60. e.preventDefault();
  61. logout(this.api, `/accept/${memberId}/${token}/`);
  62. };
  63. handleAcceptInvite = async () => {
  64. const {memberId, token} = this.props.params;
  65. this.setState({accepting: true});
  66. try {
  67. if (this.orgSlug) {
  68. await this.api.requestPromise(
  69. `/accept-invite/${this.orgSlug}/${memberId}/${token}/`,
  70. {
  71. method: 'POST',
  72. }
  73. );
  74. } else {
  75. await this.api.requestPromise(`/accept-invite/${memberId}/${token}/`, {
  76. method: 'POST',
  77. });
  78. }
  79. browserHistory.replace(`/${this.state.inviteDetails.orgSlug}/`);
  80. } catch {
  81. this.setState({acceptError: true});
  82. }
  83. this.setState({accepting: false});
  84. };
  85. get existingMemberAlert() {
  86. const user = ConfigStore.get('user');
  87. return (
  88. <Alert type="warning" data-test-id="existing-member">
  89. {tct(
  90. 'Your account ([email]) is already a member of this organization. [switchLink:Switch accounts]?',
  91. {
  92. email: user.email,
  93. switchLink: (
  94. <Link
  95. to=""
  96. data-test-id="existing-member-link"
  97. onClick={this.handleLogout}
  98. />
  99. ),
  100. }
  101. )}
  102. </Alert>
  103. );
  104. }
  105. get authenticationActions() {
  106. const {inviteDetails} = this.state;
  107. return (
  108. <Fragment>
  109. {!inviteDetails.requireSso && (
  110. <p data-test-id="action-info-general">
  111. {t(
  112. `To continue, you must either create a new account, or login to an
  113. existing Sentry account.`
  114. )}
  115. </p>
  116. )}
  117. {inviteDetails.hasAuthProvider && (
  118. <p data-test-id="action-info-sso">
  119. {inviteDetails.requireSso
  120. ? tct(
  121. `Note that [orgSlug] has required Single Sign-On (SSO) using
  122. [authProvider]. You may create an account by authenticating with
  123. the organization's SSO provider.`,
  124. {
  125. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  126. authProvider: inviteDetails.ssoProvider,
  127. }
  128. )
  129. : tct(
  130. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  131. [authProvider]. You may create an account by authenticating with
  132. the organization's SSO provider.`,
  133. {
  134. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  135. authProvider: inviteDetails.ssoProvider,
  136. }
  137. )}
  138. </p>
  139. )}
  140. <Actions>
  141. <ActionsLeft>
  142. {inviteDetails.hasAuthProvider && (
  143. <LinkButton
  144. data-test-id="sso-login"
  145. priority="primary"
  146. href={`/auth/login/${inviteDetails.orgSlug}/`}
  147. >
  148. {t('Join with %s', inviteDetails.ssoProvider)}
  149. </LinkButton>
  150. )}
  151. {!inviteDetails.requireSso && (
  152. <LinkButton
  153. data-test-id="create-account"
  154. priority="primary"
  155. href="/auth/register/"
  156. >
  157. {t('Create a new account')}
  158. </LinkButton>
  159. )}
  160. </ActionsLeft>
  161. {!inviteDetails.requireSso && (
  162. <ExternalLink
  163. href="/auth/login/"
  164. openInNewTab={false}
  165. data-test-id="link-with-existing"
  166. >
  167. {t('Login using an existing account')}
  168. </ExternalLink>
  169. )}
  170. </Actions>
  171. </Fragment>
  172. );
  173. }
  174. get warning2fa() {
  175. const {inviteDetails} = this.state;
  176. return (
  177. <Fragment>
  178. <p data-test-id="2fa-warning">
  179. {tct(
  180. 'To continue, [orgSlug] requires all members to configure two-factor authentication.',
  181. {orgSlug: inviteDetails.orgSlug}
  182. )}
  183. </p>
  184. <Actions>
  185. <LinkButton priority="primary" to="/settings/account/security/">
  186. {t('Configure Two-Factor Auth')}
  187. </LinkButton>
  188. </Actions>
  189. </Fragment>
  190. );
  191. }
  192. get acceptActions() {
  193. const {inviteDetails, accepting} = this.state;
  194. return (
  195. <Fragment>
  196. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  197. <p data-test-id="action-info-sso">
  198. {tct(
  199. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  200. [authProvider]. You may join the organization by authenticating with
  201. the organization's SSO provider or via your standard account authentication.`,
  202. {
  203. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  204. authProvider: inviteDetails.ssoProvider,
  205. }
  206. )}
  207. </p>
  208. )}
  209. <Actions>
  210. <ActionsLeft>
  211. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  212. <LinkButton
  213. data-test-id="sso-login"
  214. priority="primary"
  215. href={`/auth/login/${inviteDetails.orgSlug}/`}
  216. >
  217. {t('Join with %s', inviteDetails.ssoProvider)}
  218. </LinkButton>
  219. )}
  220. <Button
  221. data-test-id="join-organization"
  222. priority="primary"
  223. disabled={accepting}
  224. onClick={this.handleAcceptInvite}
  225. >
  226. {t('Join the %s organization', inviteDetails.orgSlug)}
  227. </Button>
  228. </ActionsLeft>
  229. </Actions>
  230. </Fragment>
  231. );
  232. }
  233. renderError() {
  234. /**
  235. * NOTE (mifu67): this error view could show up for multiple reasons, including:
  236. * invite link expired, signed into account that is already in the inviting
  237. * org, and invite not approved. Previously, the message seemed to indivate that
  238. * the link had expired, regardless of which error prompted it, so update the
  239. * error message to be a little more helpful.
  240. */
  241. return (
  242. <NarrowLayout>
  243. <Alert type="warning">
  244. {tct(
  245. 'This organization invite link is invalid. It may be expired, or you may need to [switchLink:sign in with a different account].',
  246. {
  247. switchLink: (
  248. <Link
  249. to=""
  250. data-test-id="existing-member-link"
  251. onClick={this.handleLogoutAndRetry}
  252. />
  253. ),
  254. }
  255. )}
  256. </Alert>
  257. </NarrowLayout>
  258. );
  259. }
  260. renderBody() {
  261. const {inviteDetails, acceptError} = this.state;
  262. return (
  263. <NarrowLayout>
  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;