acceptOrganizationInvite.tsx 8.0 KB

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