index.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {logout} from 'sentry/actionCreators/account';
  5. import {Alert} from 'sentry/components/alert';
  6. import {Button} from 'sentry/components/button';
  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 {t, tct} from 'sentry/locale';
  11. import ConfigStore from 'sentry/stores/configStore';
  12. import {space} from 'sentry/styles/space';
  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. needsEmailVerification: boolean;
  22. orgSlug: string;
  23. requireSso: boolean;
  24. ssoProvider?: string;
  25. };
  26. type Props = RouteComponentProps<{memberId: string; token: string; orgId?: string}, {}>;
  27. type State = DeprecatedAsyncView['state'] & {
  28. acceptError: boolean | undefined;
  29. accepting: boolean | undefined;
  30. inviteDetails: InviteDetails;
  31. };
  32. class AcceptOrganizationInvite extends DeprecatedAsyncView<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<DeprecatedAsyncView['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. getTitle() {
  53. return t('Accept Organization Invite');
  54. }
  55. handleLogout = async (e: React.MouseEvent) => {
  56. e.preventDefault();
  57. await logout(this.api);
  58. window.location.replace('/auth/login/');
  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. browserHistory.replace(`/${this.state.inviteDetails.orgSlug}/`);
  77. } catch {
  78. this.setState({acceptError: true});
  79. }
  80. this.setState({accepting: false});
  81. };
  82. get existingMemberAlert() {
  83. const user = ConfigStore.get('user');
  84. return (
  85. <Alert type="warning" data-test-id="existing-member">
  86. {tct(
  87. 'Your account ([email]) is already a member of this organization. [switchLink:Switch accounts]?',
  88. {
  89. email: user.email,
  90. switchLink: (
  91. <Link
  92. to=""
  93. data-test-id="existing-member-link"
  94. onClick={this.handleLogout}
  95. />
  96. ),
  97. }
  98. )}
  99. </Alert>
  100. );
  101. }
  102. get authenticationActions() {
  103. const {inviteDetails} = this.state;
  104. return (
  105. <Fragment>
  106. {!inviteDetails.requireSso && (
  107. <p data-test-id="action-info-general">
  108. {t(
  109. `To continue, you must either create a new account, or login to an
  110. existing Sentry account.`
  111. )}
  112. </p>
  113. )}
  114. {inviteDetails.hasAuthProvider && (
  115. <p data-test-id="action-info-sso">
  116. {inviteDetails.requireSso
  117. ? tct(
  118. `Note that [orgSlug] has required Single Sign-On (SSO) using
  119. [authProvider]. You may create an account by authenticating with
  120. the organization's SSO provider.`,
  121. {
  122. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  123. authProvider: inviteDetails.ssoProvider,
  124. }
  125. )
  126. : tct(
  127. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  128. [authProvider]. You may create an account by authenticating with
  129. the organization's SSO provider.`,
  130. {
  131. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  132. authProvider: inviteDetails.ssoProvider,
  133. }
  134. )}
  135. </p>
  136. )}
  137. <Actions>
  138. <ActionsLeft>
  139. {inviteDetails.hasAuthProvider && (
  140. <Button
  141. data-test-id="sso-login"
  142. priority="primary"
  143. href={`/auth/login/${inviteDetails.orgSlug}/`}
  144. >
  145. {t('Join with %s', inviteDetails.ssoProvider)}
  146. </Button>
  147. )}
  148. {!inviteDetails.requireSso && (
  149. <Button
  150. data-test-id="create-account"
  151. priority="primary"
  152. href="/auth/register/"
  153. >
  154. {t('Create a new account')}
  155. </Button>
  156. )}
  157. </ActionsLeft>
  158. {!inviteDetails.requireSso && (
  159. <ExternalLink
  160. href="/auth/login/"
  161. openInNewTab={false}
  162. data-test-id="link-with-existing"
  163. >
  164. {t('Login using an existing account')}
  165. </ExternalLink>
  166. )}
  167. </Actions>
  168. </Fragment>
  169. );
  170. }
  171. get warning2fa() {
  172. const {inviteDetails} = this.state;
  173. return (
  174. <Fragment>
  175. <p data-test-id="2fa-warning">
  176. {tct(
  177. 'To continue, [orgSlug] requires all members to configure two-factor authentication.',
  178. {orgSlug: inviteDetails.orgSlug}
  179. )}
  180. </p>
  181. <Actions>
  182. <Button priority="primary" to="/settings/account/security/">
  183. {t('Configure Two-Factor Auth')}
  184. </Button>
  185. </Actions>
  186. </Fragment>
  187. );
  188. }
  189. get warningEmailVerification() {
  190. const {inviteDetails} = this.state;
  191. return (
  192. <Fragment>
  193. <p data-test-id="email-verification-warning">
  194. {tct(
  195. 'To continue, [orgSlug] requires all members to verify their email address.',
  196. {orgSlug: inviteDetails.orgSlug}
  197. )}
  198. </p>
  199. <Actions>
  200. <Button priority="primary" to="/settings/account/emails/">
  201. {t('Verify Email Address')}
  202. </Button>
  203. </Actions>
  204. </Fragment>
  205. );
  206. }
  207. get acceptActions() {
  208. const {inviteDetails, accepting} = this.state;
  209. return (
  210. <Fragment>
  211. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  212. <p data-test-id="action-info-sso">
  213. {tct(
  214. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  215. [authProvider]. You may join the organization by authenticating with
  216. the organization's SSO provider or via your standard account authentication.`,
  217. {
  218. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  219. authProvider: inviteDetails.ssoProvider,
  220. }
  221. )}
  222. </p>
  223. )}
  224. <Actions>
  225. <ActionsLeft>
  226. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  227. <Button
  228. data-test-id="sso-login"
  229. priority="primary"
  230. href={`/auth/login/${inviteDetails.orgSlug}/`}
  231. >
  232. {t('Join with %s', inviteDetails.ssoProvider)}
  233. </Button>
  234. )}
  235. <Button
  236. data-test-id="join-organization"
  237. priority="primary"
  238. disabled={accepting}
  239. onClick={this.handleAcceptInvite}
  240. >
  241. {t('Join the %s organization', inviteDetails.orgSlug)}
  242. </Button>
  243. </ActionsLeft>
  244. </Actions>
  245. </Fragment>
  246. );
  247. }
  248. renderError() {
  249. return (
  250. <NarrowLayout>
  251. <Alert type="warning">
  252. {t('This organization invite link is no longer valid.')}
  253. </Alert>
  254. </NarrowLayout>
  255. );
  256. }
  257. renderBody() {
  258. const {inviteDetails, acceptError} = this.state;
  259. return (
  260. <NarrowLayout>
  261. <SettingsPageHeader title={t('Accept organization invite')} />
  262. {acceptError && (
  263. <Alert type="error">
  264. {t('Failed to join this organization. Please try again')}
  265. </Alert>
  266. )}
  267. <InviteDescription data-test-id="accept-invite">
  268. {tct('[orgSlug] is using Sentry to track and debug errors.', {
  269. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  270. })}
  271. </InviteDescription>
  272. {inviteDetails.needsAuthentication
  273. ? this.authenticationActions
  274. : inviteDetails.existingMember
  275. ? this.existingMemberAlert
  276. : inviteDetails.needs2fa
  277. ? this.warning2fa
  278. : inviteDetails.needsEmailVerification
  279. ? this.warningEmailVerification
  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;