Exemplo n.º 1
0
 $provider = $_GET['provider'];
 fwrite($output, $provider);
 $headerBody = file_get_contents("php://input");
 if (is_string($headerBody)) {
     //fwrite($output, "\n\n".$headerBody);
     $parsedBody = json_decode($headerBody, true);
     if (isset($parsedBody['code']) && !empty($parsedBody['code'])) {
         /* Query the DB, check if user's created in provider's specific table
                If yes: authenticate with that info
                If no: check if user is currently authenticated (Bearer)
                    If yes: add info to that specific provider's table
                    If no: create a user with info from the API and add it to provider's table
            */
         $googleClient = new Google_Client();
         $googleAuth = new GoogleAuth($googleClient);
         $accessToken = json_decode($googleAuth->getAccessToken($parsedBody['code']), true);
         $userData = json_decode(file_get_contents("https://www.googleapis.com/userinfo/v2/me?access_token=" . $accessToken['access_token']), true);
         //fwrite($output, "\nsuccess".print_r($userData, true));
         try {
             $dbh = new PDO($dbn, $database['user'], $database['password']);
         } catch (PDOException $e) {
             fwrite($output, "Connection failed: " . $e->getMessage());
             header("HTTP/1.1 500 Internal Server Error");
         }
         $checkQuery = $dbh->prepare("SELECT users.* FROM google_users, users WHERE google_users.google_id = :googleid AND users.id = google_users.user_id LIMIT 1;");
         $checkQuery->bindParam(":googleid", $userData['id']);
         $checkQuery->execute();
         $checkQueryResults = $checkQuery->fetch(PDO::FETCH_ASSOC);
         fwrite($output, "\n\n" . print_r($checkQueryResults, true) . "\n" . $userData['id']);
         if ($checkQueryResults) {
             // User already exists