Advertisement
lowheartrate

discord/connect_discord_oauth.php

Apr 1st, 2019
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2. // get current url
  3. $current_url = $_SERVER['REQUEST_URI'];
  4.  
  5. // if user is currently on registration page
  6. if (strpos($current_url, 'registration') !== false) {
  7.    
  8.     // require proper directory for config.php
  9.     require_once 'models/config.php';
  10.  
  11. // if user is not on registration page...
  12. } else {
  13.    
  14.     // require proper directory for config.php
  15.     require_once '../models/config.php';
  16.  
  17. }
  18.  
  19. // require other necessary files;
  20. require $DBphp_location;
  21. require 'discord_oauth.php';  
  22.  
  23. $redirectURL = $discord_redirect_url; //The URL that the user will be redirect back too
  24. $clientID = 'xxx';                              //The ID of the client
  25. $clientSecret = 'xxx';            //The sensitive and secret key of the client
  26. $scope = 'identify email';
  27.  
  28.  
  29. function getUserId($code) {
  30.     require '../models/config.php';
  31.     $redirectURL = $discord_redirect_url; //The URL that the user will be redirect back too
  32.     $clientID = 'xxx';                              //The ID of the client
  33.     $clientSecret = 'xxx';            //The sensitive and secret key of the client
  34.     $scope = 'identify email';
  35.  
  36.     //Exchange the token
  37.     $auth = discord_oauth_exchange($clientID, $clientSecret, $scope, $redirectURL, $code);
  38.  
  39.     echo '<strong>'.$redirectURL.'</strong><br />';
  40.  
  41.     if ($auth == null || !empty($auth['error'])) {
  42.         printf('Failed: Authorization was bad: %s', $auth['error']);
  43.         return -1;
  44.     }
  45.  
  46.     $user = discord_oauth_get('/users/@me', $auth['access_token']);
  47.  
  48.     if ($user == null || !empty($user['message'])) {
  49.         printf('Failed: /users/@me threw a error: %s', $user['message']);
  50.         return -1;
  51.     }
  52.  
  53.     return intval($user['id']);
  54.    
  55. }
  56.  
  57. // get current URL
  58. $currentURL = $_SERVER['REQUEST_URI'];
  59.  
  60. // if currentURL contains 'discord'
  61. if (strpos($currentURL, 'discord') !== false) {
  62.  
  63.     // If we don't have a code, redirect user
  64.     if (!isset($_GET['code'])) {
  65.         discord_oauth_redirect($clientID, $scope, $redirectURL);
  66.         exit;
  67.     }
  68.    
  69. }
  70. //printf("<pre>\nCode: %s\nUser ID: %d", $_GET['code'], getUserId($_GET['code']));
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement