acceptOrganizationInvite.tsx 7.9 KB

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