-
Notifications
You must be signed in to change notification settings - Fork 0
/
account-kit.php
146 lines (140 loc) · 4.31 KB
/
account-kit.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
// function to verify session status
function is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
// verifying POST data and adding the values to session variables
if(isset($_POST["code"])){
session_start();
$_SESSION["code"] = $_POST["code"];
$_SESSION["csrf_nonce"] = $_POST["csrf_nonce"];
$ch = curl_init();
// Set url elements
$fb_app_id = '746459578850470';
$ak_secret = 'ebfe028d89e2924af4a61b809cef0e9b';
$token = 'AA|'.$fb_app_id.'|'.$ak_secret;
// Get access token
$url = 'https://graph.accountkit.com/v1.0/access_token?grant_type=authorization_code&code='.$_POST["code"].'&access_token='.$token;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
$info = json_decode($result);
// Get account information
$url = 'https://graph.accountkit.com/v1.0/me/?access_token='.$info->access_token;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$final = json_decode($result);
}
?>
<html>
<head>
<title>Login with Account Kit</title>
<link rel="shortcut icon" href="ak-icon.png">
<link rel="stylesheet" href="assets/css/ak.css">
<!--Hotlinked Account Kit SDK-->
<script src="https://sdk.accountkit.com/us_EN/sdk.js"></script>
</head>
<?php include("includes/nav.php"); ?>
<body>
<?php
// verifying if the session exists
if(is_session_started() === FALSE && !isset($_SESSION)){
?>
<h1 class="ac">Login with Account Kit</h1>
<p class="ac">This example shows you how to implement<br>Facebook Account Kit for web using PHP.</p>
<div class="buttons">
<button onclick="phone_btn_onclick();">Login with SMS</button>
<button onclick="email_btn_onclick();">Login with Email</button>
</div>
<form action="" method="POST" id="my_form">
<input type="hidden" name="code" id="code">
<input type="hidden" name="csrf_nonce" id="csrf_nonce">
</form>
<?php
}else{
?>
<h1 class="ac">Login with Account Kit</h1>
<p class="ac">The session with Facebook Account Kit has begun.</p>
<h3 class="ac">Your Information</h3>
<p class="ac">
<!-- show account information -->
<strong>ID:</strong> <?=$final->id?> <br>
<?php
if(isset($final->email)){
?>
<strong>Email:</strong> <?=$final->email->address?>
<?php
}else{
?>
<strong>Country Code:</strong> +<?=$final->phone->country_prefix?> <br>
<strong>Phone Number:</strong> <?=$final->phone->national_number?> <br>
<?php
}
?>
</p>
<!-- END ACCOUNT INFORMATION -->
<div class="buttons">
<button onclick="logout();">Logout</button>
</div>
<?php
}
?>
</body>
<script>
// initialize Account Kit with CSRF protection
AccountKit_OnInteractive = function(){
AccountKit.init(
{
appId:746459578850470,
state:"abcd",
version:"v1.0"
}
//If your Account Kit configuration requires app_secret, you have to include it above
);
};
// login callback
function loginCallback(response) {
console.log(response);
if (response.status === "PARTIALLY_AUTHENTICATED") {
document.getElementById("code").value = response.code;
document.getElementById("csrf_nonce").value = response.state;
document.getElementById("my_form").submit();
}
else if (response.status === "NOT_AUTHENTICATED") {
// handle authentication failure
console.log("Authentication failure");
}
else if (response.status === "BAD_PARAMS") {
// handle bad parameters
console.log("Bad parameters");
}
}
// phone form submission handler
function phone_btn_onclick() {
// you can add countryCode and phoneNumber to set values
AccountKit.login('PHONE', {}, // will use default values if this is not specified
loginCallback);
}
// email form submission handler
function email_btn_onclick() {
// you can add emailAddress to set value
AccountKit.login('EMAIL', {}, loginCallback);
}
// destroying session
function logout() {
document.location = 'logout.php';
}
</script>
</html>