gettoken.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals XMLHttpRequest */
  6. import uid from '@ckeditor/ckeditor5-utils/src/uid';
  7. // WARNING: The URL below should not be used for any other purpose than Easy Image plugin development.
  8. // Images uploaded using the testing token service may be deleted automatically at any moment.
  9. // If you would like to try the Easy Image service, please wait until the official launch of Easy Image and sign up for a free trial.
  10. // Images uploaded during the free trial will not be deleted for the whole trial period and additionally the trial service can be converted
  11. // into a subscription at any moment, allowing you to preserve all uploaded images.
  12. const CLOUD_SERVICES_TOKEN_URL = 'https://j2sns7jmy0.execute-api.eu-central-1.amazonaws.com/prod/token';
  13. export default function getToken() {
  14. return new Promise( ( resolve, reject ) => {
  15. const xhr = new XMLHttpRequest();
  16. const userId = uid();
  17. xhr.open( 'GET', `${ CLOUD_SERVICES_TOKEN_URL }?user.id=${ userId }` );
  18. xhr.onload = () => {
  19. if ( xhr.status >= 200 && xhr.status < 300 ) {
  20. const response = JSON.parse( xhr.responseText );
  21. resolve( response.token );
  22. } else {
  23. reject( new Error( `XHR status: ${ xhr.status }` ) );
  24. }
  25. };
  26. xhr.onerror = err => {
  27. reject( err );
  28. };
  29. xhr.send( null );
  30. } );
  31. }