暫無描述

callback.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. require_once 'config.php';
  3. // IF A SESSION ALREADY EXISTS, USE EXISTING TOKEN
  4. // ELSE IF THERE'S NO SESSION BUT $_GET HAS USER AUTHCODE, OBTAIN NEW TOKEN
  5. // ELSE REDIRECT TO ERROR PAGE
  6. if(isset($_SESSION['token'])) {
  7. $client->setAccessToken($_SESSION['token']);
  8. }
  9. else if(isset($_GET['code'])) {
  10. $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
  11. // IF 'code' IS NOT AUTHENTIC, TOKEN IS AN ARRAY WITH 'error' SET TO SOMETHING (i.e. 'invalid_grant')
  12. if(isset($token['error'])) {
  13. header('Location: ./logout.php');
  14. die();
  15. }
  16. $_SESSION['token'] = $token;
  17. }
  18. else {
  19. header('Location: ../home.php');
  20. exit();
  21. }
  22. // var_dump($_GET);
  23. // var_dump($_SESSION);
  24. $oAuth = new Google_Service_Oauth2($client);
  25. $userData = $oAuth->userinfo_v2_me->get();
  26. /*
  27. Algunos keys de los datos que se pueden extraer:
  28. (USADOS)
  29. -SCOPE 'profile': name, picture
  30. -SCOPE 'email': email
  31. (NO USADOS)
  32. *-SCOPE 'profile': gender, familyName, givenName, hd, id, link*, locale, verifiedEmail
  33. *-SCOPE 'profile': internal_gapi_mappings, modelData, processed
  34. Lista completa de scopes: https://developers.google.com/identity/protocols/googlescopes
  35. */
  36. require_once 'dbh.inc.php';
  37. $query = 'SELECT * FROM researcher WHERE email = "' . $userData['email'] . '";';
  38. $result = mysqli_query($connection, $query);
  39. $dbUserData = mysqli_fetch_assoc($result);
  40. // IF DB CAN'T FETCH USER DATA, IT MUST BE BECAUSE USER IS UNAUTHORIZED
  41. if($dbUserData === NULL) {
  42. $_SESSION['error'] = 'unauthorized';
  43. header('Location: ../unauthorized.php');
  44. exit();
  45. }
  46. // FIRST ASSUME USER IS NOT NEW
  47. $_SESSION['newUser'] = false;
  48. // IF USER IS NEW, UPDATE DB WITH USER INFO FROM GOOGLE
  49. // AND SET 'newUser' SESSION VARIABLE TO TRUE
  50. if($dbUserData['picture'] == NULL) { //$dbUserData['name'] == NULL &&
  51. $query = 'UPDATE researcher SET name = "' . $userData['name'] . '", picture = "' . $userData['picture'] . '" WHERE email = "' . $userData['email'] . '";';
  52. mysqli_query($connection, $query);
  53. $_SESSION['newUser'] = true;
  54. }
  55. // REFETCH INFO FROM DATABASE AND STORE IN SESSION
  56. $result = mysqli_query($connection, $query);
  57. $dbUserData = mysqli_fetch_assoc($result);
  58. $_SESSION['dbUserData'] = $dbUserData;
  59. //### FOR DEBUGGING ###
  60. // $_SESSION['result'] = $result;
  61. $_SESSION['error_set'] = isset($_SESSION['error']);
  62. //### FOR DEBUGGING ###
  63. header('Location: ../home.php');
  64. exit();
  65. /*
  66. Additional links that helped:
  67. Google Developers Tutorial (Javascript): https://developers.google.com/identity/sign-in/web/sign-in
  68. Sample Code: https://github.com/GoogleChromeLabs/google-sign-in
  69. Branding Guidelines: https://developers.google.com/identity/branding-guidelines
  70. Developer's Console: https://console.developers.google.com
  71. Tutorials:
  72. -Login With Google Account Using PHP & Client API: https://www.youtube.com/watch?v=hazMyK_cnzk
  73. -Login with Google Account using PHP: https://www.codexworld.com/login-with-google-api-using-php/
  74. */