google_login.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/admin/google_login.php
  3. session_start();
  4. require_once '../config/database.php';
  5. require_once '../vendor/autoload.php';
  6. // --- ΡΥΘΜΙΣΕΙΣ ΝΕΟΥ ADMIN GOOGLE API ---
  7. $clientId = '1062187052171-ijgd341h6mdko1k05e78srbl9otmlisl.apps.googleusercontent.com';
  8. $clientSecret = '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';
  9. $redirectUri = 'https://hockeycms.eparmak.eu/admin/google_login.php'; // ⚠️ Πρόσεξε να ταιριάζει ακριβώς με το Redirect URI στο Google Console σου
  10. $client = new Google_Client();
  11. $client->setClientId($clientId);
  12. $client->setClientSecret($clientSecret);
  13. $client->setRedirectUri($redirectUri);
  14. $client->addScope('email');
  15. $client->addScope('profile');
  16. // Αν δεν έχει έρθει το ?code= από την Google, τον στέλνουμε στη σελίδα login της Google
  17. if (!isset($_GET['code'])) {
  18. $authUrl = $client->createAuthUrl();
  19. header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
  20. exit;
  21. }
  22. // Αν επέστρεψε με κωδικό, επεξεργαζόμαστε τα στοιχεία
  23. try {
  24. $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
  25. if (isset($token['error'])) {
  26. throw new Exception("Σφάλμα σύνδεσης: " . $token['error']);
  27. }
  28. $client->setAccessToken($token['access_token']);
  29. // Παίρνουμε τα στοιχεία του χρήστη από την Google
  30. $google_oauth = new Google_Service_Oauth2($client);
  31. $google_account_info = $google_oauth->userinfo->get();
  32. $email = $google_account_info->email;
  33. // Ελέγχουμε αποκλειστικά αν το email υπάρχει στη βάση και είναι Admin (role_id = 1)
  34. $stmt = $pdo->prepare("SELECT id, role_id, first_name, username, is_seller FROM users WHERE email = ? AND role_id = 1");
  35. $stmt->execute([$email]);
  36. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  37. if ($user) {
  38. // Επιτυχής σύνδεση Admin
  39. session_regenerate_id(true);
  40. $_SESSION['user_id'] = $user['id'];
  41. $_SESSION['role_id'] = $user['role_id'];
  42. $_SESSION['first_name'] = $user['first_name'] ?? '';
  43. $_SESSION['username'] = $user['username'];
  44. $_SESSION['is_seller'] = $user['is_seller'] ?? 0;
  45. header("Location: index.php");
  46. exit;
  47. } else {
  48. // Ο χρήστης δεν βρέθηκε ή δεν έχει δικαιώματα διαχειριστή
  49. $_SESSION['login_error'] = "Ο λογαριασμός Google δεν έχει δικαιώματα διαχειριστή (Admin).";
  50. header("Location: login.php");
  51. exit;
  52. }
  53. } catch (Exception $e) {
  54. $_SESSION['login_error'] = "Σφάλμα Google Login: " . $e->getMessage();
  55. header("Location: login.php");
  56. exit;
  57. }