-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-oauth.php
More file actions
99 lines (99 loc) · 4.58 KB
/
google-oauth.php
File metadata and controls
99 lines (99 loc) · 4.58 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
include 'functions.php';
// Connect to MySQL using the below function
$pdo = pdo_connect_mysql();
// If the captured code param exists and is valid
if (isset($_GET['code']) && !empty($_GET['code'])) {
// Execute cURL request to retrieve the access token
$params = [
'code' => $_GET['code'],
'client_id' => google_oauth_client_id,
'client_secret' => google_oauth_client_secret,
'redirect_uri' => google_oauth_redirect_uri,
'grant_type' => 'authorization_code'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
// Make sure access token is valid
if (isset($response['access_token']) && !empty($response['access_token'])) {
// Execute cURL request to retrieve the user info associated with the Google account
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/oauth2/v3/userinfo');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $response['access_token']]);
$response = curl_exec($ch);
curl_close($ch);
$profile = json_decode($response, true);
// Make sure the profile data exists
if (isset($profile['email'])) {
// Check if account exists in database
$stmt = $pdo->prepare('SELECT * FROM accounts WHERE email = ?');
$stmt->execute([ $profile['email'] ]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
// Get the current date
$date = date('Y-m-d\TH:i:s');
// If the account exists...
if ($account) {
// Account exists! Bind the SQL data
$google_name = $account['full_name'];
$role = $account['role'];
$id = $account['id'];
} else {
// Insert new account
$username = '';
// Determine google name and remove all special characters
$google_name = '';
$google_name .= isset($profile['given_name']) ? preg_replace('/[^a-zA-Z0-9]/s', '', $profile['given_name']) : '';
$google_name .= $google_name ? ' ' : '';
$google_name .= isset($profile['family_name']) ? preg_replace('/[^a-zA-Z0-9]/s', '', $profile['family_name']) : '';
// Default role
$role = 'Member';
// Generate a random password
$password = password_hash(uniqid() . $date, PASSWORD_DEFAULT);
// Account doesn't exist, create it
$stmt = $pdo->prepare('INSERT INTO accounts (full_name, password, email, role) VALUES (?, ?, ?, ?)');
$stmt->execute([ $google_name, $password, $profile['email'], $role ]);
// Account ID
$id = $pdo->lastInsertId();
}
// Authenticate the user
session_regenerate_id();
$_SESSION['account_loggedin'] = TRUE;
$_SESSION['account_id'] = $id;
$_SESSION['account_role'] = $role;
$_SESSION['account_email'] = $profile['email'];
$_SESSION['account_name'] = $google_name;
// Chat system
$_SESSION['chat_widget_account_loggedin'] = TRUE;
$_SESSION['chat_widget_account_id'] = $id;
$_SESSION['chat_widget_account_role'] = $role;
update_info($pdo, $id, $profile['email']);
// Redirect to home page
header('Location: index.php');
exit;
} else {
exit('Could not retrieve profile information! Please try again later!');
}
} else {
exit('Invalid access token! Please try again later!');
}
} else {
// Define params and redirect to Google Authentication page
$params = [
'response_type' => 'code',
'client_id' => google_oauth_client_id,
'redirect_uri' => google_oauth_redirect_uri,
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
'access_type' => 'offline',
'prompt' => 'consent'
];
header('Location: https://accounts.google.com/o/oauth2/auth?' . http_build_query($params));
exit;
}
?>