| 21 | |
| 22 | |
| 23 | export function GoogleCredentials(props) { |
| 24 | const [cloudDBCredInstance, setCloudDBCredInstance] = React.useState(); |
| 25 | |
| 26 | let _eventBus = React.useContext(CloudWizardEventsContext); |
| 27 | let child = null; |
| 28 | React.useMemo(() => { |
| 29 | const googleCredSchema = new GoogleCredSchema({ |
| 30 | authenticateGoogle:(client_secret_file) => { |
| 31 | let loading_icon_url = url_for( |
| 32 | 'static', { 'filename': 'img/loading.gif'} |
| 33 | ); |
| 34 | const axiosApi = getApiInstance(); |
| 35 | _eventBus.fireEvent('SET_ERROR_MESSAGE_FOR_CLOUD_WIZARD', [MESSAGE_TYPE.INFO, 'Google authentication process is in progress. <img src="' + loading_icon_url + '" alt="' + gettext('Loading...') + '">']); |
| 36 | let _url = url_for('google.verify_credentials'); |
| 37 | const post_data = { |
| 38 | cloud: 'google', |
| 39 | secret: {'client_secret_file':client_secret_file} |
| 40 | }; |
| 41 | return new Promise((resolve, reject)=>{axiosApi.post(_url, post_data) |
| 42 | .then((res) => { |
| 43 | if (res.data && res.data.success == 1 ) { |
| 44 | let params = 'scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no, width=550,height=650,left=600,top=150'; |
| 45 | child = window.open(res.data.data.auth_url, 'google_authentication', params); |
| 46 | resolve(true); |
| 47 | } |
| 48 | else if (res.data && res.data.success == 0) { |
| 49 | _eventBus.fireEvent('SET_ERROR_MESSAGE_FOR_CLOUD_WIZARD',[MESSAGE_TYPE.ERROR, res.data.errormsg]); |
| 50 | _eventBus.fireEvent('SET_CRED_VERIFICATION_INITIATED',false); |
| 51 | resolve(false); |
| 52 | } |
| 53 | }) |
| 54 | .catch((error) => { |
| 55 | _eventBus.fireEvent('SET_ERROR_MESSAGE_FOR_CLOUD_WIZARD',[MESSAGE_TYPE.ERROR, gettext(`Error during authentication: ${error}`)]); |
| 56 | reject(new Error(gettext(`Error during authentication: ${error}`))); |
| 57 | }); |
| 58 | }); |
| 59 | }, |
| 60 | verification_ack:()=>{ |
| 61 | let auth_url = url_for('google.verification_ack'); |
| 62 | let countdown = 90; |
| 63 | const axiosApi = getApiInstance(); |
| 64 | return new Promise((resolve, reject)=>{ |
| 65 | const interval = setInterval(()=>{ |
| 66 | axiosApi.get(auth_url) |
| 67 | .then((res)=>{ |
| 68 | if (res.data.success && res.data.success == 1 ){ |
| 69 | _eventBus.fireEvent('SET_CRED_VERIFICATION_INITIATED',true); |
| 70 | _eventBus.fireEvent('SET_ERROR_MESSAGE_FOR_CLOUD_WIZARD',[MESSAGE_TYPE.SUCCESS, gettext('Authentication completed successfully. Click the Next button to proceed.')]); |
| 71 | clearInterval(interval); |
| 72 | if(child){ |
| 73 | // close authentication window |
| 74 | child.close(); |
| 75 | } |
| 76 | resolve(); |
| 77 | } else if (res.data && res.data.success == 0 && res.data.errormsg ){ |
| 78 | _eventBus.fireEvent('SET_ERROR_MESSAGE_FOR_CLOUD_WIZARD',[MESSAGE_TYPE.ERROR, res.data.errormsg]); |
| 79 | _eventBus.fireEvent('SET_CRED_VERIFICATION_INITIATED',false); |
| 80 | clearInterval(interval); |