-
Notifications
You must be signed in to change notification settings - Fork 10
/
clef.php
61 lines (49 loc) · 1.72 KB
/
clef.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
<?php
require_once('config.php');
require_once('vendor/autoload.php');
function validate_state($state) {
$is_valid = isset($_SESSION['state']) && strlen($_SESSION['state']) > 0 && $_SESSION['state'] == $state;
if (!$is_valid) {
header('HTTP/1.0 403 Forbidden');
echo "The state parameter didn't match what was passed in to the Clef button.";
exit;
} else {
unset($_SESSION['state']);
}
return $is_valid;
}
if (!session_id()) {
session_start();
}
if (isset($_GET["code"]) && $_GET["code"] != "") {
validate_state($_GET["state"]);
\Clef\Clef::initialize(APP_ID, APP_SECRET);
try {
$response = \Clef\Clef::get_login_information($_GET["code"]);
$result = $response->info;
// reset the user's session
if (isset($result->id) && ($result->id != '')) {
//remove all the variables in the session
session_unset();
// destroy the session
session_destroy();
if (!session_id())
session_start();
$clef_id = $result->id;
$_SESSION['name'] = $result->first_name .' '. $result->last_name;
$_SESSION['email'] = $result->email;
$_SESSION['user_id'] = $clef_id;
$_SESSION['logged_in_at'] = time(); // timestamp in unix time
require_once('mysql.php');
$user = get_user($clef_id, $mysql);
if (!$user) {
insert_user($clef_id, $result->first_name, $mysql);
}
// send them to the member's area!
header("Location: members_area.php");
}
} catch (Exception $e) {
echo "Login with Clef failed: " . $e->getMessage();
}
}
?>