| 1 | efrain | 1 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 2 | //
 | 
        
           |  |  | 3 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 4 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 5 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 6 | // (at your option) any later version.
 | 
        
           |  |  | 7 | //
 | 
        
           |  |  | 8 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 11 | // GNU General Public License for more details.
 | 
        
           |  |  | 12 | //
 | 
        
           |  |  | 13 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 14 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | /**
 | 
        
           |  |  | 17 |  * A module to handle the OAuth2 callback for MoodleNet.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     core/moodlenet/oauth2callback
 | 
        
           |  |  | 20 |  * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
 | 
        
           |  |  | 21 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 22 |  * @since      4.2
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | import Prefetch from "core/prefetch";
 | 
        
           |  |  | 26 | import {alert} from 'core/notification';
 | 
        
           |  |  | 27 | import {getString} from 'core/str';
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * Handle the OAuth2 callback for MoodleNet.
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @param {String} error Error
 | 
        
           |  |  | 33 |  * @param {String} errorDescription Error description
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | const handleCallback = (error, errorDescription) => {
 | 
        
           |  |  | 36 |     if (window.opener) {
 | 
        
           |  |  | 37 |         // Call the MoodleNet Authorization again in the opener window.
 | 
        
           |  |  | 38 |         window.opener.moodleNetAuthorize(error, errorDescription);
 | 
        
           |  |  | 39 |         // Close the authorization popup.
 | 
        
           |  |  | 40 |         // We need to use setTimeout here because the Behat 'I press "x" and switch to main window' step expects the popup to still
 | 
        
           |  |  | 41 |         // be visible after clicking the button. Otherwise, it will throw a webdriver error.
 | 
        
           |  |  | 42 |         setTimeout(() => {
 | 
        
           |  |  | 43 |             // Close the authorization popup.
 | 
        
           |  |  | 44 |             window.close();
 | 
        
           |  |  | 45 |         }, 300);
 | 
        
           |  |  | 46 |     } else {
 | 
        
           |  |  | 47 |         alert(getString('error', 'moodle'), getString('moodlenet:sharefailtitle', 'moodle'));
 | 
        
           |  |  | 48 |     }
 | 
        
           |  |  | 49 | };
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | /**
 | 
        
           |  |  | 52 |  * Initialize.
 | 
        
           |  |  | 53 |  *
 | 
        
           |  |  | 54 |  * @param {String} error Error
 | 
        
           |  |  | 55 |  * @param {String} errorDescription Error description
 | 
        
           |  |  | 56 |  */
 | 
        
           |  |  | 57 | export const init = (error, errorDescription) => {
 | 
        
           |  |  | 58 |     Prefetch.prefetchStrings('moodle', ['moodlenet:sharefailtitle', 'error']);
 | 
        
           |  |  | 59 |     handleCallback(error, errorDescription);
 | 
        
           |  |  | 60 | };
 |