-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoauth.php
110 lines (81 loc) · 2.84 KB
/
goauth.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
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
100
101
102
103
104
105
106
107
108
109
110
<?php
// Includes
require_once 'includes/google-api-php-client/apiClient.php';
require_once 'includes/google-api-php-client/contrib/apiOauth2Service.php';
require_once 'includes/idiorm.php';
require_once 'includes/relativeTime.php';
// Session. Pass your own name if you wish.
session_start();
// Database configuration with the IDIORM library
$host = 'localhost';
$user = 'root';
$pass = 'pranjal123';
$database = 'coderoyale';
ORM::configure("mysql:host=$host;dbname=$database");
ORM::configure('username', $user);
ORM::configure('password', $pass);
// Changing the connection to unicode
ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
// Google API. Obtain these settings from https://code.google.com/apis/console/
$redirect_url = 'http://localhost/coderoyale/goauth.php'; // The url of your web site
$client_id = '52187884579-g8ptkki0n5m91v4lj3j7ph46ottfbbq3.apps.googleusercontent.com';
$client_secret = 'gEH20UjaK_XCxNajQU4UizJ7';
$api_key = 'AIzaSyDis70d2YNa894INaoNY1fV2UJwOq4oagQ';
// Create a new Google API client
$client = new apiClient();
//$client->setApplicationName("Tutorialzine");
// Configure it
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setDeveloperKey($api_key);
$client->setRedirectUri($redirect_url);
$client->setApprovalPrompt(false);
$oauth2 = new apiOauth2Service($client);
if (!isset($_GET['code'])) {
$authUrl = $client->createAuthUrl();
header('Location:'.$authUrl);
}
// The code parameter signifies that this is
// a redirect from google, bearing a temporary code
if (isset($_GET['code'])) {
// This method will obtain the actuall access token from Google,
// so we can request user info
$client->authenticate();
// Get the user data
$info = $oauth2->userinfo->get();
// Find this person in the database
$person = ORM::for_table('glogin_users')->where('email', $info['email'])->find_one();
if(!$person){
// No such person was found. Register!
$person = ORM::for_table('glogin_users')->create();
// Set the properties that are to be inserted in the db
$person->email = $info['email'];
$person->name = $info['name'];
if(isset($info['picture'])){
// If the user has set a public google account photo
$person->photo = $info['picture'];
}
else{
// otherwise use the default
$person->photo = 'assets/img/default_avatar.jpg';
}
// insert the record to the database
$person->save();
}
// Save the user id to the session
$_SESSION['user_id'] = $person->id();
$_SESSION['name'] = $person->name;
// Redirect to the base demo URL
header("Location: home.php ");
exit;
}
// Handle logout
if (isset($_GET['logout'])) {
unset($_SESSION['user_id']);
}
$person = null;
if(isset($_SESSION['user_id'])){
// Fetch the person from the database
$person = ORM::for_table('glogin_users')->find_one($_SESSION['user_id']);
}
?>