| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- // Αρχείο: /var/www/html/hockey-cms/admin/google_login.php
- session_start();
- require_once '../config/database.php';
- require_once '../vendor/autoload.php';
- // --- ΡΥΘΜΙΣΕΙΣ ΝΕΟΥ ADMIN GOOGLE API ---
- $clientId = '1062187052171-ijgd341h6mdko1k05e78srbl9otmlisl.apps.googleusercontent.com';
- $clientSecret = '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';
- $redirectUri = 'https://hockeycms.eparmak.eu/admin/google_login.php'; // ⚠️ Πρόσεξε να ταιριάζει ακριβώς με το Redirect URI στο Google Console σου
- $client = new Google_Client();
- $client->setClientId($clientId);
- $client->setClientSecret($clientSecret);
- $client->setRedirectUri($redirectUri);
- $client->addScope('email');
- $client->addScope('profile');
- // Αν δεν έχει έρθει το ?code= από την Google, τον στέλνουμε στη σελίδα login της Google
- if (!isset($_GET['code'])) {
- $authUrl = $client->createAuthUrl();
- header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
- exit;
- }
- // Αν επέστρεψε με κωδικό, επεξεργαζόμαστε τα στοιχεία
- try {
- $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
- if (isset($token['error'])) {
- throw new Exception("Σφάλμα σύνδεσης: " . $token['error']);
- }
-
- $client->setAccessToken($token['access_token']);
-
- // Παίρνουμε τα στοιχεία του χρήστη από την Google
- $google_oauth = new Google_Service_Oauth2($client);
- $google_account_info = $google_oauth->userinfo->get();
-
- $email = $google_account_info->email;
- // Ελέγχουμε αποκλειστικά αν το email υπάρχει στη βάση και είναι Admin (role_id = 1)
- $stmt = $pdo->prepare("SELECT id, role_id, first_name, username, is_seller FROM users WHERE email = ? AND role_id = 1");
- $stmt->execute([$email]);
- $user = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($user) {
- // Επιτυχής σύνδεση Admin
- session_regenerate_id(true);
- $_SESSION['user_id'] = $user['id'];
- $_SESSION['role_id'] = $user['role_id'];
- $_SESSION['first_name'] = $user['first_name'] ?? '';
- $_SESSION['username'] = $user['username'];
- $_SESSION['is_seller'] = $user['is_seller'] ?? 0;
-
- header("Location: index.php");
- exit;
- } else {
- // Ο χρήστης δεν βρέθηκε ή δεν έχει δικαιώματα διαχειριστή
- $_SESSION['login_error'] = "Ο λογαριασμός Google δεν έχει δικαιώματα διαχειριστή (Admin).";
- header("Location: login.php");
- exit;
- }
- } catch (Exception $e) {
- $_SESSION['login_error'] = "Σφάλμα Google Login: " . $e->getMessage();
- header("Location: login.php");
- exit;
- }
|