VerifyCORS.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { Component, ReactNode } from 'react';
  20. import {
  21. SupersetClient,
  22. Method,
  23. makeApi,
  24. SupersetApiError,
  25. t,
  26. } from '@superset-ui/core';
  27. import { Button } from '@superset-ui/core/components';
  28. import ErrorMessage from './ErrorMessage';
  29. export type Props = {
  30. children: ({ payload }: { payload?: object }) => ReactNode;
  31. endpoint?: string;
  32. host: string;
  33. method?: Method;
  34. postPayload?: string;
  35. };
  36. type State = {
  37. didVerify: boolean;
  38. error?: Error | SupersetApiError;
  39. payload?: object;
  40. };
  41. export const renderError = (error: Error) => (
  42. <div>
  43. The following error occurred, make sure you have <br />
  44. 1) configured CORS in Superset to receive requests from this domain. <br />
  45. 2) set the Superset host correctly below. <br />
  46. 3) debug the CORS configuration under the `@superset-ui/connection` stories.
  47. <br />
  48. <br />
  49. <ErrorMessage error={error} />
  50. </div>
  51. );
  52. export default class VerifyCORS extends Component<Props, State> {
  53. constructor(props: Props) {
  54. super(props);
  55. this.state = { didVerify: false };
  56. this.handleVerify = this.handleVerify.bind(this);
  57. }
  58. componentDidUpdate(prevProps: Props) {
  59. const { endpoint, host, postPayload, method } = this.props;
  60. if (
  61. (this.state.didVerify || this.state.error) &&
  62. (prevProps.endpoint !== endpoint ||
  63. prevProps.host !== host ||
  64. prevProps.postPayload !== postPayload ||
  65. prevProps.method !== method)
  66. ) {
  67. // eslint-disable-next-line react/no-did-update-set-state
  68. this.setState({ didVerify: false, error: undefined });
  69. }
  70. }
  71. handleVerify() {
  72. const { endpoint, host, postPayload, method } = this.props;
  73. SupersetClient.reset();
  74. SupersetClient.configure({
  75. credentials: 'include',
  76. host,
  77. mode: 'cors',
  78. })
  79. .init()
  80. .then(() => {
  81. // Test an endpoint if specified
  82. if (endpoint && postPayload) {
  83. return makeApi({
  84. endpoint,
  85. method,
  86. })(postPayload);
  87. }
  88. return { error: 'Must provide valid endpoint and payload.' };
  89. })
  90. .then(result =>
  91. this.setState({ didVerify: true, error: undefined, payload: result }),
  92. )
  93. .catch(error => this.setState({ error }));
  94. }
  95. render() {
  96. const { didVerify, error, payload } = this.state;
  97. const { children } = this.props;
  98. return didVerify ? (
  99. children({ payload })
  100. ) : (
  101. <div className="row">
  102. <div className="col-md-10">
  103. This example requires CORS requests from this domain. <br />
  104. <br />
  105. 1) enable CORS requests in your Superset App from{' '}
  106. {`${window.location.origin}`}
  107. <br />
  108. 2) configure your Superset App host name below <br />
  109. 3) click below to verify authentication. You may debug CORS further
  110. using the `@superset-ui/connection` story. <br />
  111. <br />
  112. <Button type="primary" size="small" onClick={this.handleVerify}>
  113. {t('Verify')}
  114. </Button>
  115. <br />
  116. <br />
  117. </div>
  118. {error && (
  119. <div className="col-md-8">
  120. <ErrorMessage error={error} />
  121. </div>
  122. )}
  123. </div>
  124. );
  125. }
  126. }