acceptOrganizationInvite.tsx 9.4 KB

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