-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle.php
56 lines (45 loc) · 1.78 KB
/
google.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
<?php
// Include file gpconfig
include_once 'gpconfig.php';
if(isset($_GET['code'])){
$gclient->authenticate($_GET['code']);
$_SESSION['token'] = $gclient->getAccessToken();
header('Location: ' . filter_var($redirect_url, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$gclient->setAccessToken($_SESSION['token']);
}
if ($gclient->getAccessToken())
{
include 'db.php';
// Get user profile data from google
$gpuserprofile = $google_oauthv2->userinfo->get();
$nama = $gpuserprofile['given_name']." ".$gpuserprofile['family_name']; // Ambil nama dari Akun Google
$email = $gpuserprofile['email']; // Ambil email Akun Google nya
$sql = mysqli_query($con, "SELECT id_user, username, nama_lengkap FROM user WHERE email='".$email."'");
$user = mysqli_fetch_array($sql); // Ambil datanya dari hasil query tadi
if(empty($user))
{
$ex = explode('@', $email);
$username = $ex[0];
mysqli_query($con, "INSERT INTO user(username, nama_lengkap, email) VALUES('".$username."', '".$nama."', '".$email."')");
$id = mysqli_insert_id($con); // Ambil id user yang baru saja di insert
}
else
{
$id = $user['id_user']; // Ambil id pada tabel user
$username = $user['username']; // Ambil username pada tabel user
$nama = $user['nama']; // Ambil username pada tabel user
}
$_SESSION['id_us'] = $id;
$_SESSION['username'] = $username;
$_SESSION['nama'] = $nama;
$_SESSION['email'] = $email;
header("location:$url");
}
else
{
$authUrl = $gclient->createAuthUrl();
header("location: ".$authUrl);
}
?>