Facebook Login PHP

config.php

/*
 * FACEBOOK-LOGIN-SETUP
 * https://developers.facebook.com/docs/graph-api/reference/user
 * APP-ID on https://developers.facebook.com/apps/
 * 1. Create a new FB-APP
 * 2. GET the app_id & app_secret
 * 3. ADD Plattform => set Domain for LOGIN
 */
$config = array();
// App-ID
$config['facebook']['app_id'] = '1s59724s57582812';
$config['facebook']['app_secret'] = 'f302eabf302eabf302eabf302eab';
$config['facebook']['callpack_url'] = 'http://login.infochy.de/callback.php';

login.php

require_once 'config.php';
session_start() ;
require_once __DIR__ . '/vendor/php-graph-sdk-5.0.0/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
'app_id' => $config['facebook']['app_id'], 
'app_secret' => $config['facebook']['app_secret'],
'default_graph_version' => 'v2.2',
]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl($config['facebook']['callpack_url'], $permissions);

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';

callback.php

require_once 'config.php';
session_start() ;
require_once __DIR__ . '/vendor/php-graph-sdk-5.0.0/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
    'app_id' => $config['facebook']['app_id'], // Replace {app-id} with your app id
    'app_secret' => $config['facebook']['app_secret'],
    'default_graph_version' => 'v2.2',
]);

$helper = $fb->getRedirectLoginHelper();

try {
    $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

if (! isset($accessToken)) {
    if ($helper->getError()) {
        header('HTTP/1.0 401 Unauthorized');
        echo "Error: " . $helper->getError() . "\n";
        echo "Error Code: " . $helper->getErrorCode() . "\n";
        echo "Error Reason: " . $helper->getErrorReason() . "\n";
        echo "Error Description: " . $helper->getErrorDescription() . "\n";
    } else {
        header('HTTP/1.0 400 Bad Request');
        echo 'Bad request';
    }
    exit;
}

// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);

// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId($config['facebook']['app_id']); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();

if (! $accessToken->isLongLived()) {
    // Exchanges a short-lived access token for a long-lived one
    try {
        $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
        exit;
    }

    echo '<h3>Long-lived</h3>';
    var_dump($accessToken->getValue());
}

$_SESSION['fb_access_token'] = (string) $accessToken;

try {
    // Returns a `Facebook\FacebookResponse` object
    // EXAMPLE: /me?fields=id,name
    $response = $fb->get('/me?fields=id,name,email', (string) $accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

$user = $response->getGraphUser();

echo 'Name: ' . $user['name'];
echo 'email: ' .$user['email'];

Extbase FeUser Example Extension

GIT: https://github.com/wallpageNET/infochy_feuserexample
TER: https://typo3.org/extensions/repository/view/infochy_feuserexample

Model

    /**
     * user
     *
     * @var \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
     */
    protected $user;
    
    /**
     * Returns the user
     *
     * @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $user
     */
    public function getUser() {
        return $this->user;
    }

    /**
     * Sets the user
     *
     * @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $user
     * @return void
     */
    public function setUser($user) {
        $this->user = $user;
    }

Controller

    /**
     * action create
     *
     * @param \Infochy\InfochyFeuserexample\Domain\Model\MyData $newMyData
     * @return void
     */
    public function createAction(\Infochy\InfochyFeuserexample\Domain\Model\MyData $newMyData)
    {
        $this->addFlashMessage('createAction ADD FrontendUser', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);       

		
	$newMyData->getUser()->addUsergroup($this->getFrontendUserGroup());          
        $this->myDataRepository->add($newMyData);
        $this->redirect('list');
    }
    
    private function getFrontendUserGroup(){
    	$frontendUserGroups = $this->frontendUserGroupRepository->findByTitle('InfochyFeuserexample');
		$frontendUserGroup = $frontendUserGroups->getFirst();
		if(!is_null($frontendUserGroup)){
			return $frontendUserGroup;
		}else{
			$frontendUserGroup = $this->objectManager->get('TYPO3\CMS\Extbase\Domain\Model\FrontendUserGroup');
			$frontendUserGroup->setTitle('InfochyFeuserexample');
			$frontendUserGroup->setDescription('Title display only on Sys-Pages');
			$this->frontendUserGroupRepository->add($frontendUserGroup);
			$persistenceManager =  $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
			$persistenceManager->persistAll();	
	    	$frontendUserGroups = $this->frontendUserGroupRepository->findByTitle('InfochyFeuserexample');
			return $frontendUserGroups->getFirst();					
		}
    }

Controller -> frontendUserGroupRepository

    /**
     * frontendUserGroupRepository
     *
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository
     * @inject
     */
    protected $frontendUserGroupRepository = NULL;

TCA

		'user' => array(
			'exclude' => 1,
			'label' => 'Fe User',
			'config' => array(
				'type' => 'inline',
				'foreign_table' => 'fe_users',
				'minitems' => 0,
				'maxitems' => 1,
				'appearance' => array(
					'collapseAll' => 0,
					'levelLinksPosition' => 'top',
					'showSynchronizationLink' => 1,
					'showPossibleLocalizationRecords' => 1,
					'showAllLocalizationLink' => 1
				),
			),
		),	

Fluid-HTML

<label for="user.username">
	FeUser->username
</label><br />
	<f:form.textfield property="user.username" /><br />
	
<label for="user.password">
	FeUser->password
</label><br />
	<f:form.textfield property="user.password" /><br />
	
<label for="user.name">
	FeUser->name
</label><br />
	<f:form.textfield property="user.name" /><br />
	
<label for="user.email">
	FeUser->email
</label><br />
	<f:form.textfield property="user.email" /><br />

Extbase Ajax Example Extension

GIT: https://github.com/wallpageNET/infochy_ajaxexample
TER: https://typo3.org/extensions/repository/view/infochy_ajaxexample

Typoscript

page.includeJSFooter {
  infochy_ajaxexample = EXT:infochy_ajaxexample/Resources/Public/Javascript/ajax.js
}
ajaxCall = PAGE
ajaxCall {
   typeNum = 22222
   config {
    disableAllHeaderCode = 1
    xhtml_cleaning = 0
    admPanel = 0
    additionalHeaders = Content-type: text/plain
    no_cache = 1
    debug = 0
   }
   10 = USER
   10 {
	    userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
	    extensionName = InfochyAjaxexample
	    pluginName = Plugin
	    vendorName = Infochy
	    controller = Ajaxexample
	    action = ajax
	    view < plugin.tx_infochyajaxexample_plugin.view
	    persistence < plugin.tx_infochyajaxexample_plugin.persistence
	    settings < plugin.tx_infochyajaxexample_plugin.settings
   }
}

JavaScript

$("form.newAjaxexample").submit(function (event) {
    event.preventDefault();
    var form = $(this);
    var action = form.attr("action"),
          method = form.attr("method"),
          data = form.serialize();

    $.ajax({
        url: action,
        type: method,
        data: data
        }).done(function (data) {
            $( "div.tx-infochy-ajaxexample" ).replaceWith( data );
        }).fail(function () {
			( "div.tx-infochy-ajaxexample" ).replaceWith("errorMessage");  
        }).always(function () {
        
        });
});

HTML

<f:form action="ajax" 
    controller="Ajaxexample" 
    extensionName="InfochyAjaxexample" 
    pluginName="Plugin"
    name="newAjaxexample"
    method="POST"  
    pageType="22222"
    class="newAjaxexample"  object="{newAjaxexample}">    
<f:render partial="Ajaxexample/FormFields" />
	<f:form.submit value="Ajax new" />
</f:form>

PHP log Debug

unlink("logger.log");
error_log(print_r($mixedArray,true), 3, "logger.log");