index.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import {Fragment} from 'react';
  2. import {browserHistory, 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 DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  14. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  15. type InviteDetails = {
  16. existingMember: boolean;
  17. hasAuthProvider: boolean;
  18. needs2fa: boolean;
  19. needsAuthentication: boolean;
  20. needsEmailVerification: 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} = window.__initialData;
  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 = async (e: React.MouseEvent) => {
  55. e.preventDefault();
  56. await logout(this.api);
  57. window.location.replace('/auth/login/');
  58. };
  59. handleAcceptInvite = async () => {
  60. const {memberId, token} = this.props.params;
  61. this.setState({accepting: true});
  62. try {
  63. if (this.orgSlug) {
  64. await this.api.requestPromise(
  65. `/accept-invite/${this.orgSlug}/${memberId}/${token}/`,
  66. {
  67. method: 'POST',
  68. }
  69. );
  70. } else {
  71. await this.api.requestPromise(`/accept-invite/${memberId}/${token}/`, {
  72. method: 'POST',
  73. });
  74. }
  75. browserHistory.replace(`/${this.state.inviteDetails.orgSlug}/`);
  76. } catch {
  77. this.setState({acceptError: true});
  78. }
  79. this.setState({accepting: false});
  80. };
  81. get existingMemberAlert() {
  82. const user = ConfigStore.get('user');
  83. return (
  84. <Alert type="warning" data-test-id="existing-member">
  85. {tct(
  86. 'Your account ([email]) is already a member of this organization. [switchLink:Switch accounts]?',
  87. {
  88. email: user.email,
  89. switchLink: (
  90. <Link
  91. to=""
  92. data-test-id="existing-member-link"
  93. onClick={this.handleLogout}
  94. />
  95. ),
  96. }
  97. )}
  98. </Alert>
  99. );
  100. }
  101. get authenticationActions() {
  102. const {inviteDetails} = this.state;
  103. return (
  104. <Fragment>
  105. {!inviteDetails.requireSso && (
  106. <p data-test-id="action-info-general">
  107. {t(
  108. `To continue, you must either create a new account, or login to an
  109. existing Sentry account.`
  110. )}
  111. </p>
  112. )}
  113. {inviteDetails.hasAuthProvider && (
  114. <p data-test-id="action-info-sso">
  115. {inviteDetails.requireSso
  116. ? tct(
  117. `Note that [orgSlug] has required Single Sign-On (SSO) using
  118. [authProvider]. You may create an account by authenticating with
  119. the organization's SSO provider.`,
  120. {
  121. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  122. authProvider: inviteDetails.ssoProvider,
  123. }
  124. )
  125. : tct(
  126. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  127. [authProvider]. You may create an account by authenticating with
  128. the organization's SSO provider.`,
  129. {
  130. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  131. authProvider: inviteDetails.ssoProvider,
  132. }
  133. )}
  134. </p>
  135. )}
  136. <Actions>
  137. <ActionsLeft>
  138. {inviteDetails.hasAuthProvider && (
  139. <Button
  140. data-test-id="sso-login"
  141. priority="primary"
  142. href={`/auth/login/${inviteDetails.orgSlug}/`}
  143. >
  144. {t('Join with %s', inviteDetails.ssoProvider)}
  145. </Button>
  146. )}
  147. {!inviteDetails.requireSso && (
  148. <Button
  149. data-test-id="create-account"
  150. priority="primary"
  151. href="/auth/register/"
  152. >
  153. {t('Create a new account')}
  154. </Button>
  155. )}
  156. </ActionsLeft>
  157. {!inviteDetails.requireSso && (
  158. <ExternalLink
  159. href="/auth/login/"
  160. openInNewTab={false}
  161. data-test-id="link-with-existing"
  162. >
  163. {t('Login using an existing account')}
  164. </ExternalLink>
  165. )}
  166. </Actions>
  167. </Fragment>
  168. );
  169. }
  170. get warning2fa() {
  171. const {inviteDetails} = this.state;
  172. return (
  173. <Fragment>
  174. <p data-test-id="2fa-warning">
  175. {tct(
  176. 'To continue, [orgSlug] requires all members to configure two-factor authentication.',
  177. {orgSlug: inviteDetails.orgSlug}
  178. )}
  179. </p>
  180. <Actions>
  181. <Button priority="primary" to="/settings/account/security/">
  182. {t('Configure Two-Factor Auth')}
  183. </Button>
  184. </Actions>
  185. </Fragment>
  186. );
  187. }
  188. get warningEmailVerification() {
  189. const {inviteDetails} = this.state;
  190. return (
  191. <Fragment>
  192. <p data-test-id="email-verification-warning">
  193. {tct(
  194. 'To continue, [orgSlug] requires all members to verify their email address.',
  195. {orgSlug: inviteDetails.orgSlug}
  196. )}
  197. </p>
  198. <Actions>
  199. <Button priority="primary" to="/settings/account/emails/">
  200. {t('Verify Email Address')}
  201. </Button>
  202. </Actions>
  203. </Fragment>
  204. );
  205. }
  206. get acceptActions() {
  207. const {inviteDetails, accepting} = this.state;
  208. return (
  209. <Fragment>
  210. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  211. <p data-test-id="action-info-sso">
  212. {tct(
  213. `Note that [orgSlug] has enabled Single Sign-On (SSO) using
  214. [authProvider]. You may join the organization by authenticating with
  215. the organization's SSO provider or via your standard account authentication.`,
  216. {
  217. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  218. authProvider: inviteDetails.ssoProvider,
  219. }
  220. )}
  221. </p>
  222. )}
  223. <Actions>
  224. <ActionsLeft>
  225. {inviteDetails.hasAuthProvider && !inviteDetails.requireSso && (
  226. <Button
  227. data-test-id="sso-login"
  228. priority="primary"
  229. href={`/auth/login/${inviteDetails.orgSlug}/`}
  230. >
  231. {t('Join with %s', inviteDetails.ssoProvider)}
  232. </Button>
  233. )}
  234. <Button
  235. data-test-id="join-organization"
  236. priority="primary"
  237. disabled={accepting}
  238. onClick={this.handleAcceptInvite}
  239. >
  240. {t('Join the %s organization', inviteDetails.orgSlug)}
  241. </Button>
  242. </ActionsLeft>
  243. </Actions>
  244. </Fragment>
  245. );
  246. }
  247. renderError() {
  248. return (
  249. <NarrowLayout>
  250. <Alert type="warning">
  251. {t('This organization invite link is no longer valid.')}
  252. </Alert>
  253. </NarrowLayout>
  254. );
  255. }
  256. renderBody() {
  257. const {inviteDetails, acceptError} = this.state;
  258. return (
  259. <NarrowLayout>
  260. <SettingsPageHeader title={t('Accept organization invite')} />
  261. {acceptError && (
  262. <Alert type="error">
  263. {t('Failed to join this organization. Please try again')}
  264. </Alert>
  265. )}
  266. <InviteDescription data-test-id="accept-invite">
  267. {tct('[orgSlug] is using Sentry to track and debug errors.', {
  268. orgSlug: <strong>{inviteDetails.orgSlug}</strong>,
  269. })}
  270. </InviteDescription>
  271. {inviteDetails.needsAuthentication
  272. ? this.authenticationActions
  273. : inviteDetails.existingMember
  274. ? this.existingMemberAlert
  275. : inviteDetails.needs2fa
  276. ? this.warning2fa
  277. : inviteDetails.needsEmailVerification
  278. ? this.warningEmailVerification
  279. : inviteDetails.requireSso
  280. ? this.authenticationActions
  281. : this.acceptActions}
  282. </NarrowLayout>
  283. );
  284. }
  285. }
  286. const Actions = styled('div')`
  287. display: flex;
  288. align-items: center;
  289. justify-content: space-between;
  290. margin-bottom: ${space(3)};
  291. `;
  292. const ActionsLeft = styled('span')`
  293. > a {
  294. margin-right: ${space(1)};
  295. }
  296. `;
  297. const InviteDescription = styled('p')`
  298. font-size: 1.2em;
  299. `;
  300. export default AcceptOrganizationInvite;