Php-google-OAuth


https://console.cloud.google.com/
https://console.cloud.google.com/apis/credentials
+create credentials
oauth client id
application type : web application
Authorized JavaScript origins,The HTTP origins that host your web application. (masukkan alamat host mis https://biston.web.id )
catat Client ID, misalnya xxxxxxxx-5kg812lr6q2pd6da2ec4ekodqs9olci7.apps.googleusercontent.com

login.php


<style>
button {
  background-color: #4285F4; /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
</style>


<html lang="en">
  <head>
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="xxxxxxxxxxxxxx-eam7m1pkldsgluq33rvchp1ojfo51ocj.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
  </head>
  <body><center><hr>
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>


    <button onclick="signOut()">SignOut</button></center>


    <script>
      function onSignIn(googleUser) {
        // Useful data for your client-side scripts:
        var profile = googleUser.getBasicProfile();
        var id_token = googleUser.getAuthResponse().id_token;
        var email = profile.getEmail();
        var name = profile.getName();
        var img = profile.getImageUrl();
        var id = profile.getId();
        document.getElementById("data").innerHTML = "<hr>"+name+"<br>"+email+"<br>"+id+"<br>"+id_token;
        ////ini adalah proses olah datanya dengan php
        ////id_token dan email diolah dengan metode GET
        ////data ini diperiksa apakah emailnya sudah member atau bukan (jadi bikin memang data member sebelumnya)
        ////ada pesan yg berbeda kalau emailnya member atau bukan
        ////semua yang login, member ataupun bukan harus di catat
        ////berikut ini adalah contoh GET datanya ke confirm.php
        //window.location.replace("https://biston.web.id/confirm.php?id="+id_token+"&email="+email);
        //window.location.replace("https://biston.web.id/login/confirm.php?id="+id_token+"&email="+email);

      }


  function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function () {
      ///location.reload(); 
      window.location.replace("https://biston.web.id/login.php");
      console.log('User signed out.');
    });
  }
</script>



<p id="data"></p>


  </body>
</html>



confirm.php



<style>
button {
  background-color: #4285F4; /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
</style>


<html lang="en">
  <head>
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="xxxxxxxxxxxxxxxx-eam7m1pkldsgluq33rvchp1ojfo51ocj.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
  </head>



 <hr><button onclick='signOut()'>SignOut</button></center><hr>


<script>
  function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function () {
      ///location.reload(); 
      window.location.replace("https://biston.web.id/login.php");
      console.log('User signed out.');
    });
  }
</script>

<?php
session_start();
$id_token = $_GET['id'];
if(!empty($id_token)) {
$he1 = array("User-Agent:okhttp/3.12.1");
$url = "https://oauth2.googleapis.com/tokeninfo?id_token=".$id_token;
$res = curl_get($url,$he1) ;
$dt = json_decode($res, true);
$email = $dt['email'];

if ($email=="[email protected]"){
	echo "<h1>Status : Anda adalah Member</h1>";
}else{
	echo "<h1>status : Maaf anda Bukan Member</h1>";
}

print_r($res);

}/////////////////////////////////
else{
	echo "<h1>status : Gagal Login !!!</h1>";
}



function curl_get($url, $headers = []) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    if (!empty($headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); }

    curl_close($ch);
    return $response;
}



?>