Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. $signedRequest = common::getSignedRequest(FB_SECRET_KEY);
  2. $isUser = false; // Default: non-user
  3.  
  4. // FB permissions granted, we have "user_id"
  5. if ($signedRequest['user_id']) {
  6.     // Create psuedo user via reference to FB ID
  7.     $reponse = RESTClient::post(API_BASE . 'logins', "{\"fb_id\":\"{$signedRequest['user_id']}\"}");
  8.  
  9.     if ($reponse['code'] === 200) {
  10.         $reponse = json_decode($reponse['body']);
  11.  
  12.         // Make request to customer records with response user ID, allows us to retrieve stored FB ID
  13.         $fbUserCheck = RESTClient::get(API_BASE . 'customers/' . $reponse->id);
  14.  
  15.         if ($fbUserCheck['code'] === 200) {
  16.             $fbUserCheck = json_decode($fbUserCheck['body']);
  17.  
  18.             // Check the parity between stored ID and signed request ID
  19.             if ($fbUserCheck->fb_id != $signedRequest['user_id']) {
  20.                 unset($_SESSION['user']); // Destroy session key if there's a conflict
  21.             }
  22.  
  23.             // FB ID's are fine, current user is the same user as stored backend
  24.             else {
  25.                 $isUser = true; // Flag
  26.                 $_SESSION['user']['id'] = $reponse->id; // Set user
  27.  
  28.                 // If cart exists, make user the owner of existing cart
  29.                 if (isset($_SESSION['cart']['id'])) {
  30.                     RESTClient::put(API_BASE . 'carts/' . $_SESSION['cart']['id'], json_encode(array('customer_id' => $_SESSION['user']['id'])));
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     else unset($_SESSION['user']);
  37. }
  38.  
  39. // No signed request "user_id", destroy SESSION if exists
  40. elseif (isset($_SESSION['user']['id'])) {
  41.     unset($_SESSION['user']);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement