-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_callback.php
More file actions
82 lines (70 loc) · 2.8 KB
/
google_callback.php
File metadata and controls
82 lines (70 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
require_once 'config.php';
session_start();
// Verify state parameter
if (!isset($_GET['state']) || $_GET['state'] !== $_SESSION['oauth_state']) {
die('Invalid state parameter');
}
if (isset($_GET['code'])) {
// Exchange code for access token
$token_url = 'https://oauth2.googleapis.com/token';
$token_data = [
'code' => $_GET['code'],
'client_id' => GOOGLE_CLIENT_ID,
'client_secret' => GOOGLE_CLIENT_SECRET,
'redirect_uri' => GOOGLE_REDIRECT_URI,
'grant_type' => 'authorization_code'
];
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_data));
curl_setopt($ch, CURLOPT_POST, true);
$token_response = curl_exec($ch);
curl_close($ch);
$token_info = json_decode($token_response, true);
if (isset($token_info['access_token'])) {
// Get user info
$user_info_url = 'https://www.googleapis.com/oauth2/v2/userinfo';
$ch = curl_init($user_info_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token_info['access_token']
]);
$user_info_response = curl_exec($ch);
curl_close($ch);
$user_info = json_decode($user_info_response, true);
if (isset($user_info['email'])) {
// Connect to database
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if user exists
$stmt = $conn->prepare("SELECT id, name, email FROM users WHERE email = ?");
$stmt->bind_param("s", $user_info['email']);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if (!$user) {
// Create new user
$stmt = $conn->prepare("INSERT INTO users (name, email, google_id) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $user_info['name'], $user_info['email'], $user_info['id']);
$stmt->execute();
$user_id = $conn->insert_id;
} else {
$user_id = $user['id'];
}
// Create session
$_SESSION['user_id'] = $user_id;
$_SESSION['user_name'] = $user_info['name'];
$_SESSION['user_email'] = $user_info['email'];
// Redirect to dashboard
header("Location: dashboard.php");
exit();
}
}
}
// If we get here, something went wrong
header("Location: login.php?error=google_auth_failed");
exit();
?>