
var url = document.location.href;

if (url.indexOf('admin_consent=True') != -1) {
    var sid = "";
    var error = "";
    var error_description = "";

    for (var p of url.split('?')[1].split('&')) {
        if (p.indexOf('error=') == 0)
            error = p.substr('error='.length);
        if (p.indexOf('error_description=') == 0)
            error_description = p.substr('error_description='.length);
        if (p.indexOf('state=') == 0)
            sid = p.substr('state='.length);
    }

    if (sid != "") {

        sid = sid.replace('#', '');

        var url = [location.protocol, '//', location.host, location.pathname.split('?')[0]].join('');

        if (error == "") {
            document.location.href = url + "?SubId=" + sid.split('x')[0] + "&a=" + sid + "&t=Ok";
        }
        else {
            document.location.href = url + "?SubId=" + sid.split('x')[0] + "&a=" + sid + "&t=error" + error + ". " + error_description;
        }
    }

}


if (document.location.hash && document.location.hash.length > 1) {

    var access_token = "";
    var sid = "";
    var error = "";
    var error_description = "";

    for (var p of document.location.hash.substr(1).split('&')) {
        if (p.indexOf('error=') == 0)
            access_token = p.substr('error='.length);
        if (p.indexOf('error_description=') == 0)
            access_token = p.substr('error_description='.length);
        if (p.indexOf('access_token=') == 0)
            access_token = p.substr('access_token='.length);
        if (p.indexOf('state=') == 0)
            sid = p.substr('state='.length);
    }

    if (sid != "") {

        var url = [location.protocol, '//', location.host, location.pathname.split('#')[0]].join('');

        if (access_token != "") {
            //console.log(url + "?SubId=" + sid.split('x')[0] + "&a=" + sid + "&t=" + access_token.substr(0, 100) + Math.random().toString(36).substr(2, 10) + access_token.substr(100));
            document.location.href = url + "?SubId=" + sid.split('x')[0] + "&a=" + sid + "&t=" + access_token.substr(0, 100) + Math.random().toString(36).substr(2, 10) + access_token.substr(100);
        }
        else {
            //console.log(url + "?SubId=" + sid.split('x')[0] + "&a=" + sid + "&t=error" + error + ". " + error_description);
            document.location.href = url + "?SubId=" + sid.split('x')[0] + "&a=" + sid + "&t=error" + error + ". " + error_description;
        }
    }

}