ssoForm.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {Component} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {Client} from 'sentry/api';
  4. import Form from 'sentry/components/deprecatedforms/form';
  5. import TextField from 'sentry/components/deprecatedforms/textField';
  6. import {t, tct} from 'sentry/locale';
  7. import {AuthConfig} from 'sentry/types';
  8. type Props = {
  9. api: Client;
  10. authConfig: AuthConfig;
  11. };
  12. type State = {
  13. errorMessage: string | null;
  14. };
  15. class SsoForm extends Component<Props, State> {
  16. state: State = {
  17. errorMessage: null,
  18. };
  19. handleSubmit: Form['props']['onSubmit'] = async (data, onSuccess, onError) => {
  20. const {api} = this.props;
  21. try {
  22. const response = await api.requestPromise('/auth/sso-locate/', {
  23. method: 'POST',
  24. data,
  25. });
  26. onSuccess(data);
  27. browserHistory.push({pathname: response.nextUri});
  28. } catch (e) {
  29. if (!e.responseJSON) {
  30. onError(e);
  31. return;
  32. }
  33. const message = e.responseJSON.detail;
  34. this.setState({errorMessage: message});
  35. onError(e);
  36. }
  37. };
  38. render() {
  39. const {serverHostname} = this.props.authConfig;
  40. const {errorMessage} = this.state;
  41. return (
  42. <Form
  43. className="form-stacked"
  44. submitLabel={t('Continue')}
  45. onSubmit={this.handleSubmit}
  46. footerClass="auth-footer"
  47. errorMessage={errorMessage}
  48. >
  49. <TextField
  50. name="organization"
  51. placeholder="acme"
  52. label={t('Organization ID')}
  53. required
  54. help={tct('Your ID is the slug after the hostname. e.g. [example] is [slug].', {
  55. slug: <strong>acme</strong>,
  56. example: <SlugExample slug="acme" hostname={serverHostname} />,
  57. })}
  58. />
  59. </Form>
  60. );
  61. }
  62. }
  63. type SlugExampleProps = {
  64. hostname: string;
  65. slug: string;
  66. };
  67. const SlugExample = ({hostname, slug}: SlugExampleProps) => (
  68. <code>
  69. {hostname}/<strong>{slug}</strong>
  70. </code>
  71. );
  72. export default SsoForm;