-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticate.php
64 lines (54 loc) · 2.01 KB
/
authenticate.php
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
<?php
session_start();
## Require connection file
require_once './conn.php';
## Establish the connection to the database
$conn = connectMainDB();
if(isset($_GET['code'])){
$googleClient->authenticate($_GET['code']);
$_SESSION['token'] = $googleClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
############ Set Google access token ############
if (isset($_SESSION['token'])) {
$googleClient->setAccessToken($_SESSION['token']);
}
if ($googleClient->getAccessToken()) {
############ Fetch data from graph api ############
try {
$gpUserProfile = $google_oauthV2->userinfo->get();
}
catch (\Exception $e) {
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
header("Location: ./");
exit;
}
############ Store data in database ############
$oauthpro = "Google";
$authenticated = 1;
$oauthid = $gpUserProfile['id'] ?? '';
$f_name = $gpUserProfile['given_name'] ?? '';
$email_id = $gpUserProfile['email'] ?? '';
$picture = $gpUserProfile['picture'] ?? '';
$sql = "SELECT * FROM users WHERE email='".$email_id."'";
$result = $conn->query($sql);
if ($result->num_rows === 1) {
## Update the existing user with only the defined variables
$conn->query("UPDATE users SET oauth_pro='".$oauthpro."', oauth_id='".$oauthid."', oauth_fname='".$f_name."', oauth_picture='".$picture."', is_verified='".$authenticated."' WHERE email='".$email_id."'");
} else {
## Insert new user with only the defined variables
$conn->query("INSERT INTO users (oauth_pro, oauth_id, oauth_fname, email, oauth_picture, is_verified) VALUES ('".$oauthpro."', '".$oauthid."', '".$f_name."', '".$email_id."', '".$picture."', '".$authenticated."')");
}
$res = $conn->query($sql);
$userData = $res->fetch_assoc();
$_SESSION['userData'] = $userData;
### Set session that will be used to verify users ###
$_SESSION['authenticated'] = true;
$_SESSION['email'] = $email_id;
$_SESSION['username'] = $f_name;
header("Location: ./index.php");
} else {
header("Location: ./");
}
?>