Open popout login

Response Data

Press the button above to send a request
The code

Open the popout and print response data (JavaScript)

function login () {
    window.open("https://api.cloudianos.com/v2/auth?key=YOUR_API_KEY&back=URL_TO_POPOUT_HANDLER", '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');
}

window.addEventListener("message", function (event) {
    console.log(event);
    var origins = ["https://www.cloudianos.com", "https://api.cloudianos.com"]; //add your domain here
    if (origins.indexOf(event.origin) > -1 && event.data != "") responseData.innerHTML = JSON.stringify(JSON.parse(event.data), null, 2);
}, false);

Popout handler (PHP)

if (isset($_POST['cloudianos_data'])) {
    function fetch_api($url, $post = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        if(!empty($post)) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        } 
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    $rid = $_POST['cloudianos_data'];
    $cloudianos_data = json_decode(fetch_api("https://api.cloudianos.com/v2/auth?retrieve&rid=$rid&private_key=YOUR_PRIVATE_API_KEY"));

    if ($cloudianos_data->error == NULL) {
        //create/authenticate user session here e.g. $_SESSION['user']
        echo 'Click <a href="javascript:window.close()">here</a>.<script>window.opener.postMessage(`{"success":true,"cloudianos_data":' . json_encode($cloudianos_data) . '}`, "*");window.close();</script>';
    }
    else echo '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"><div class="container"><div class="card bg-danger mt-2"><div class="card-body"><h5>Error</h5><p>' . $cloudianos_data->error .'</p></div><code><pre class="bg-light border rounded">'. $rid .'</pre></code></div></div>';
}