Bagosep

Login Google

Jun 9th, 2021 (edited)
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. include('config.php');
  4.  
  5. $login_button = '';
  6.  
  7. if (isset($_GET["code"])) {
  8.     $token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
  9.     if (!isset($token['error'])) {
  10.         $google_client->setAccessToken($token['access_token']);
  11.         $_SESSION['access_token'] = $token['access_token'];
  12.         $google_service = new Google_Service_Oauth2($google_client);
  13.         $data = $google_service->userinfo->get();
  14.  
  15.         if (!empty($data['given_name'])) {
  16.             $_SESSION['user_first_name'] = $data['given_name'];
  17.         }
  18.  
  19.         if (!empty($data['family_name'])) {
  20.             $_SESSION['user_last_name'] = $data['family_name'];
  21.         }
  22.  
  23.         if (!empty($data['email'])) {
  24.             $_SESSION['user_email_address'] = $data['email'];
  25.         }
  26.  
  27.         if (!empty($data['gender'])) {
  28.             $_SESSION['user_gender'] = $data['gender'];
  29.         }
  30.  
  31.         if (!empty($data['picture'])) {
  32.             $_SESSION['user_image'] = $data['picture'];
  33.         }
  34.  
  35.     }
  36.  
  37. }
  38. ?>
  39. <html>
  40.  
  41. <head>
  42.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  43.     <title>Login Melalui Akun Google</title>
  44.     <meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport' />
  45.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  46.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  47.     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  48.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
  49. </head>
  50.  
  51. <body>
  52.     <div class="container">
  53.         <br />
  54.         <h2 align="center">Login Melalui Akun Google</h2>
  55.         <br />
  56.         <div class="panel panel-default">
  57.             <div align="center" style="padding: 30px">
  58.                 <a href="<?= $google_client->createAuthUrl() ?>" class="btn btn-primary" style="padding: 10px"><i class="fab fa-google"></i> Login With Google</a>
  59.             </div>
  60.         </div>
  61.     </div>
  62. </body>
  63.  
  64. </html>
RAW Paste Data