login.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Client} from 'sentry/api';
  4. import LoadingError from 'sentry/components/loadingError';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import NavTabs from 'sentry/components/navTabs';
  7. import {t} from 'sentry/locale';
  8. import space from 'sentry/styles/space';
  9. import {AuthConfig} from 'sentry/types';
  10. import withApi from 'sentry/utils/withApi';
  11. import LoginForm from './loginForm';
  12. import RegisterForm from './registerForm';
  13. import SsoForm from './ssoForm';
  14. const FORM_COMPONENTS = {
  15. login: LoginForm,
  16. register: RegisterForm,
  17. sso: SsoForm,
  18. } as const;
  19. type ActiveTab = keyof typeof FORM_COMPONENTS;
  20. type TabConfig = [key: ActiveTab, label: string, disabled?: boolean];
  21. type Props = {
  22. api: Client;
  23. };
  24. type State = {
  25. activeTab: ActiveTab;
  26. authConfig: null | AuthConfig;
  27. error: null | boolean;
  28. loading: boolean;
  29. };
  30. class Login extends Component<Props, State> {
  31. state: State = {
  32. loading: true,
  33. error: null,
  34. activeTab: 'login',
  35. authConfig: null,
  36. };
  37. componentDidMount() {
  38. this.fetchData();
  39. }
  40. handleSetTab = (activeTab: ActiveTab, event: React.MouseEvent) => {
  41. this.setState({activeTab});
  42. event.preventDefault();
  43. };
  44. fetchData = async () => {
  45. const {api} = this.props;
  46. try {
  47. const response = await api.requestPromise('/auth/config/');
  48. const {vsts_login_link, github_login_link, google_login_link, ...config} = response;
  49. const authConfig = {
  50. ...config,
  51. vstsLoginLink: vsts_login_link,
  52. githubLoginLink: github_login_link,
  53. googleLoginLink: google_login_link,
  54. };
  55. this.setState({authConfig});
  56. } catch (e) {
  57. this.setState({error: true});
  58. }
  59. this.setState({loading: false});
  60. };
  61. get hasAuthProviders() {
  62. if (this.state.authConfig === null) {
  63. return false;
  64. }
  65. const {githubLoginLink, googleLoginLink, vstsLoginLink} = this.state.authConfig;
  66. return !!(githubLoginLink || vstsLoginLink || googleLoginLink);
  67. }
  68. render() {
  69. const {api} = this.props;
  70. const {loading, error, activeTab, authConfig} = this.state;
  71. const FormComponent = FORM_COMPONENTS[activeTab];
  72. const tabs: TabConfig[] = [
  73. ['login', t('Login')],
  74. ['sso', t('Single Sign-On')],
  75. ['register', t('Register'), !authConfig?.canRegister],
  76. ];
  77. const renderTab = ([key, label, disabled]: TabConfig) =>
  78. !disabled && (
  79. <li key={key} className={activeTab === key ? 'active' : ''}>
  80. <a href="#" onClick={e => this.handleSetTab(key, e)}>
  81. {label}
  82. </a>
  83. </li>
  84. );
  85. return (
  86. <Fragment>
  87. <Header>
  88. <Heading>{t('Sign in to continue')}</Heading>
  89. <AuthNavTabs>{tabs.map(renderTab)}</AuthNavTabs>
  90. </Header>
  91. {loading && <LoadingIndicator />}
  92. {error && (
  93. <StyledLoadingError
  94. message={t('Unable to load authentication configuration')}
  95. onRetry={this.fetchData}
  96. />
  97. )}
  98. {!loading && authConfig !== null && !error && (
  99. <FormWrapper hasAuthProviders={this.hasAuthProviders}>
  100. <FormComponent {...{api, authConfig}} />
  101. </FormWrapper>
  102. )}
  103. </Fragment>
  104. );
  105. }
  106. }
  107. const StyledLoadingError = styled(LoadingError)`
  108. margin: ${space(2)};
  109. `;
  110. const Header = styled('div')`
  111. border-bottom: 1px solid ${p => p.theme.border};
  112. padding: 20px 40px 0;
  113. `;
  114. const Heading = styled('h3')`
  115. font-size: 24px;
  116. margin: 0 0 20px 0;
  117. `;
  118. const AuthNavTabs = styled(NavTabs)`
  119. margin: 0;
  120. `;
  121. const FormWrapper = styled('div')<{hasAuthProviders: boolean}>`
  122. padding: 35px;
  123. width: ${p => (p.hasAuthProviders ? '600px' : '490px')};
  124. `;
  125. const formFooterClass = `
  126. display: grid;
  127. grid-template-columns: max-content 1fr;
  128. gap: ${space(1)};
  129. align-items: center;
  130. justify-items: end;
  131. border-top: none;
  132. margin-bottom: 0;
  133. padding: 0;
  134. `;
  135. export {formFooterClass};
  136. export default withApi(Login);