chrisblakley / Nebula

Nebula is a WordPress theme framework that focuses on enhancing development. The core features of Nebula make it a powerful tool for designing, developing, and analyzing WordPress websites consistently, yet its deliberately uncomplicated code syntax also serves as a learning resource for programmers themselves.
https://nebula.gearside.com
GNU General Public License v2.0
142 stars 36 forks source link

Consider making an example for Google OAuth #873

Closed chrisblakley closed 7 years ago

chrisblakley commented 8 years ago

Would likely clean/optimize this snippet a bit. Maybe consider an AJAX approach? http://loginkit.com/tutorials/google-oauth-login-tutorial.php

<?php
   // callback.php
   session_start();

   include('keys.php');

   // Load error page if something went wrong
   if(isset($_GET['error']))
      header("Location: error.php");

   if(isset($_GET['code'])) {
      $code = $_GET['code'];

   $url = 'https://accounts.google.com/o/oauth2/token';

   $params = 'code' => $code,
           'client_id' => $client_id,
           'client_secret' => $client_secret,
           'redirect_uri' => $redirect_uri,
           'grant_type' => 'authorization_code'
                  ); 

   // use key 'http' even if you send the request to https://...
   $options = array('http' => array(
                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($params)),);

   $context  = stream_context_create($options);
   $result = file_get_contents($url, false, $context);
   $obj = json_decode($result);
   $access_token = $obj->access_token;

   $userDetails = file_get_contents('https://www.googleapis.com/oauth2/' .
    'v1/userinfo?access_token=' . $access_token);

   $userData = json_decode($userDetails);

   $_SESSION['id'] = $userData->id;
   $_SESSION['email'] = $userData->email;
   $_SESSION['name'] = $userData->name;
   $_SESSION['picture'] = $userData->picture;

   header('Location: success.php');
}
?>

Could consider tying this into the contact form similar to Facebook Connect now.

chrisblakley commented 7 years ago

Moved to my repo: https://github.com/chrisblakley/Repo/issues/17