UNPKG

@aca-1/a2-composer

Version:
71 lines (70 loc) 3.21 kB
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Authentication Success</title> </head> <body> <script> var run_on_load = function() { var origin = window.location.origin; if(window != window.top && window != window.parent ) { console.log('Not main window/tab.'); var message = 'error'; if (window.location.hash.indexOf('access_token') !== -1) { message = '{"token":"' + window.location.hash.replace(/^.*access_token=([^&]+).*$/, '$1') + '","expires_in":' + parseInt(window.location.hash.replace(/^.*expires_in=([^&]+).*$/, '$1')) + '}'; } else if (window.location.search.indexOf('code') !== -1) { message = '{"code":"' + window.location.search.replace(/^.*code=([^&]+).*$/, '$1') + '"}'; } try { // Standards based browsers parent.postMessage(message, origin); } catch(e) { console.error(e); } } else { console.log('Main Window/Tab'); var data = window.location.hash ? window.location.hash : window.location.search; var loc = localStorage.getItem('oauth_redirect'); console.log(loc); if(loc){ console.log('Redirecting to ' + loc); if(loc.indexOf('?') >= 0) loc = loc.split('?')[0]; console.log(data); var oauth_data = []; if(data.indexOf('#') >= 0) { // Remove hash if data is a hash data = '?' + data.replace(new RegExp('#', 'g'), ''); oauth_data = data.split('?')[1].split('&'); } else if(data.indexOf('?') >= 0){ // Remove question mark if data is query. data = '?' + data.replace(new RegExp('\\?', 'g'), ''); oauth_data = data.split('?')[1].split('&'); } var oauth_resp = {}; console.log(oauth_data); if(oauth_data){ for(var i = 0; i < oauth_data.length; i++) { let parts = oauth_data[i].split('='); if(parts.length > 1){ oauth_resp[parts[0]] = parts[1]; } } if(sessionStorage) { sessionStorage.setItem('OAUTH.params', JSON.stringify(oauth_resp)); } } window.location.href = loc + data; } else window.location.href = origin + data; } }; if (window.addEventListener) { window.addEventListener("DOMContentLoaded", run_on_load); } else { run_on_load(); // IE 8 } </script> </body> </html>