lib/amd/src/moodlenet/oauth2callback.js

  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. * A module to handle the OAuth2 callback for MoodleNet.
  17. *
  18. * @module core/moodlenet/oauth2callback
  19. * @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @since 4.2
  22. */
  23. import Prefetch from "core/prefetch";
  24. import {alert} from 'core/notification';
  25. import {getString} from 'core/str';
  26. /**
  27. * Handle the OAuth2 callback for MoodleNet.
  28. *
  29. * @param {String} error Error
  30. * @param {String} errorDescription Error description
  31. */
  32. const handleCallback = (error, errorDescription) => {
  33. if (window.opener) {
  34. // Call the MoodleNet Authorization again in the opener window.
  35. window.opener.moodleNetAuthorize(error, errorDescription);
  36. // Close the authorization popup.
  37. // We need to use setTimeout here because the Behat 'I press "x" and switch to main window' step expects the popup to still
  38. // be visible after clicking the button. Otherwise, it will throw a webdriver error.
  39. setTimeout(() => {
  40. // Close the authorization popup.
  41. window.close();
  42. }, 300);
  43. } else {
  44. alert(getString('error', 'moodle'), getString('moodlenet:sharefailtitle', 'moodle'));
  45. }
  46. };
  47. /**
  48. * Initialize.
  49. *
  50. * @param {String} error Error
  51. * @param {String} errorDescription Error description
  52. */
  53. export const init = (error, errorDescription) => {
  54. Prefetch.prefetchStrings('moodle', ['moodlenet:sharefailtitle', 'error']);
  55. handleCallback(error, errorDescription);
  56. };