Skip to content

Commit 524f9fc

Browse files
committed
Add ability to reload page without any problem
1 parent 5ba9331 commit 524f9fc

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

sample/index.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
// More information about it here: https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes
1717
$oauth2->startRedirection(['identify', 'connections']);
1818
} else {
19-
try {
19+
// If preload the token to see if everything happen without error
20+
$ok = $oauth2->loadToken();
21+
if ($ok !== true) {
22+
// A common error can be to reload the page because the code returned by Discord would still be present in the URL
23+
// If this happen, isRedirected will return true and we will come here with an invalid code
24+
// So if there is a problem, we redirect the user to Discord authentification
25+
$oauth2->startRedirection(['identify', 'connections']);
26+
} else {
2027
// ---------- USER INFORMATION
2128
$answer = $oauth2->getUserInformation(); // Same as $oauth2->getCustomInformation('users/@me')
2229
if (array_key_exists("code", $answer)) {
@@ -35,8 +42,6 @@
3542
echo $a["type"] . ': ' . $a["name"] . '<br/>';
3643
}
3744
}
38-
} catch (Exception $e) {
39-
exit("An error occured: " . $e->getMessage());
4045
}
4146
}
4247
?>

src/OAuth2.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,28 @@ public function isRedirected() {
1717
return isset($_GET['code']);
1818
}
1919

20-
public function getCustomInformation($endpoint, $forceRefresh = false) {
21-
return $this->getInformation($forceRefresh, $endpoint);
20+
public function getCustomInformation($endpoint) {
21+
return $this->getInformation($endpoint);
2222
}
2323

24-
public function getUserInformation($forceRefresh = false) {
25-
return $this->getInformation($forceRefresh, 'users/@me');
24+
public function getUserInformation() {
25+
return $this->getInformation('users/@me');
2626
}
2727

28-
public function getConnectionsInformation($forceRefresh = false) {
29-
return $this->getInformation($forceRefresh, 'users/@me/connections');
28+
public function getConnectionsInformation() {
29+
return $this->getInformation('users/@me/connections');
3030
}
3131

32-
public function getGuildsInformation($forceRefresh = false) {
33-
return $this->getInformation($forceRefresh, 'users/@me/guilds');
32+
public function getGuildsInformation() {
33+
return $this->getInformation('users/@me/guilds');
3434
}
3535

36-
private function getInformation($forceRefresh, $endpoint) {
37-
if ($forceRefresh === true || $this->_accessToken === null) {
38-
$this->loadToken();
36+
private function getInformation($endpoint) {
37+
if ($this->_accessToken === null) {
38+
$response = $this->loadToken();
39+
if ($response !== true) {
40+
return ["code" => 0, "message" => $response];
41+
}
3942
}
4043
$curl = curl_init('https://discordapp.com/api/v6/' . $endpoint);
4144
curl_setopt($curl, CURLOPT_RETURNTRANSFER, "false");
@@ -47,10 +50,10 @@ private function getInformation($forceRefresh, $endpoint) {
4750
return $response;
4851
}
4952

50-
private function loadToken() {
53+
public function loadToken() {
5154
if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
5255
unset($_SESSION['oauth2state']);
53-
throw new Exception('Invalid state');
56+
return 'Invalid state';
5457
}
5558
$curl = curl_init();
5659
curl_setopt_array($curl, array(
@@ -59,10 +62,13 @@ private function loadToken() {
5962
CURLOPT_POSTFIELDS => "client_id=" . $this->_clientId . "&client_secret=" . $this->_secret . "&grant_type=authorization_code&code=" . $_GET['code'] . "&redirect_uri=" . urlencode($this->_redirectUrl),
6063
CURLOPT_RETURNTRANSFER => "false"
6164
));
62-
$response = curl_exec($curl);
63-
$this->_accessToken = json_decode($response, true)['access_token'];
65+
$response = json_decode(curl_exec($curl), true);
66+
if (array_key_exists('error_description', $response)) {
67+
return $response['error_description'];
68+
}
69+
$this->_accessToken = $response['access_token'];
6470
curl_close($curl);
65-
return $response;
71+
return true;
6672
}
6773

6874
private static function generateToken() {

0 commit comments

Comments
 (0)