index.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import {Fragment} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {urlEncode} from '@sentry/utils';
  5. import {logout} from 'sentry/actionCreators/account';
  6. import {Alert} from 'sentry/components/alert';
  7. import {Button} from 'sentry/components/button';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import Link from 'sentry/components/links/link';
  10. import NarrowLayout from 'sentry/components/narrowLayout';
  11. import {t, tct} from 'sentry/locale';
  12. import ConfigStore from 'sentry/stores/configStore';
  13. import {space} from 'sentry/styles/space';
  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} = window.__initialData;
  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. makeNextUrl(path: string) {
  56. return `${path}?${urlEncode({next: window.location.pathname})}`;
  57. }
  58. handleLogout = async (e: React.MouseEvent) => {
  59. e.preventDefault();
  60. await logout(this.api);
  61. window.location.replace(this.makeNextUrl('/auth/login/'));
  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. <Button
  144. data-test-id="sso-login"
  145. priority="primary"
  146. href={this.makeNextUrl(`/auth/login/${inviteDetails.orgSlug}/`)}
  147. >
  148. {t('Join with %s', inviteDetails.ssoProvider)}
  149. </Button>
  150. )}
  151. {!inviteDetails.requireSso && (
  152. <Button
  153. data-test-id="create-account"
  154. priority="primary"
  155. href={this.makeNextUrl('/auth/register/')}
  156. >
  157. {t('Create a new account')}
  158. </Button>
  159. )}
  160. </ActionsLeft>
  161. {!inviteDetails.requireSso && (
  162. <ExternalLink
  163. href={this.makeNextUrl('/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. <Button priority="primary" to="/settings/account/security/">
  186. {t('Configure Two-Factor Auth')}
  187. </Button>
  188. </Actions>
  189. </Fragment>
  190. );
  191. }
  192. get warningEmailVerification() {
  193. const {inviteDetails} = this.state;
  194. return (
  195. <Fragment>
  196. <p data-test-id="email-verification-warning">
  197. {tct(
  198. 'To continue, [orgSlug] requires all members to verify their email address.',
  199. {orgSlug: inviteDetails.orgSlug}
  200. )}
  201. </p>
  202. <Actions>
  203. <Button priority="primary" to="/settings/account/emails/">
  204. {t('Verify Email Address')}
  205. </Button>
  206. </Actions>
  207. </Fragment>
  208. );
  209. }
  210. get acceptActions() {
  211. const {inviteDetails, accepting} = this.state;
  212. return (
  213. <Fragment>
  214. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  215. <p data-test-id="action-info-sso">
  216. {tct(
  217. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  218. [authProvider]. You may join the organization by authenticating with
  219. the organization's SSO provider or via your standard account authentication.`,
  220. {
  221. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  222. authProvider: inviteDetails.ssoProvider,
  223. }
  224. )}
  225. </p>
  226. )}
  227. <Actions>
  228. <ActionsLeft>
  229. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  230. <Button
  231. data-test-id="sso-login"
  232. priority="primary"
  233. href={this.makeNextUrl(`/auth/login/${inviteDetails.orgSlug}/`)}
  234. >
  235. {t('Join with %s', inviteDetails.ssoProvider)}
  236. </Button>
  237. )}
  238. <Button
  239. data-test-id="join-organization"
  240. priority="primary"
  241. disabled={accepting}
  242. onClick={this.handleAcceptInvite}
  243. >
  244. {t('Join the %s organization', inviteDetails.orgSlug)}
  245. </Button>
  246. </ActionsLeft>
  247. </Actions>
  248. </Fragment>
  249. );
  250. }
  251. renderError() {
  252. return (
  253. <NarrowLayout>
  254. <Alert type="warning">
  255. {t('This organization invite link is no longer valid.')}
  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.needsEmailVerification
  282. ? this.warningEmailVerification
  283. : inviteDetails.requireSso
  284. ? this.authenticationActions
  285. : this.acceptActions}
  286. </NarrowLayout>
  287. );
  288. }
  289. }
  290. const Actions = styled('div')`
  291. display: flex;
  292. align-items: center;
  293. justify-content: space-between;
  294. margin-bottom: ${space(3)};
  295. `;
  296. const ActionsLeft = styled('span')`
  297. > a {
  298. margin-right: ${space(1)};
  299. }
  300. `;
  301. const InviteDescription = styled('p')`
  302. font-size: 1.2em;
  303. `;
  304. export default AcceptOrganizationInvite;