Hi, i had to do this few weeks ago and I used this:
https://github.com/thephpleague/oauth2-facebook
total time to make it work ( without facebook app registration crap ) : 20 mins
here are my functions:
Lets create a static function in our Utils class that will generate the return URL.
public static function getFacebookLoginUrl(){
$provider = \Phalcon\Di::getDefault()->get('facebook');
$authUrl = $provider->getAuthorizationUrl([
'scope' => ['email', 'public_profile']
]);
\Phalcon\Di::getDefault()->get('session')->set('oauth2state', $provider->getState());
return $authUrl;
}
Lets register the service that will contain our app data: ( in your boostrap.php or so )
$this->di->setShared('facebook', function() {
return new \League\OAuth2\Client\Provider\Facebook([
'clientId' => 'xxxxxx',
'clientSecret' => 'xxxxxx',
'redirectUri' => 'https://xxxxxxt/users/facebook/auth',
'graphApiVersion' => 'v2.5',
]);
});
and this is my contoller that recieves the ruturn URL... ( basiccaly after the user enters his creadentials in facebook and agree with terms etc...
class FacebookController extends Controller {
public function authAction(){
if (isset($_GET["error_code"]) && $_GET["error_code"] != ""){
$this->view->pick('login');
} else {
$provider = $this->di->get('facebook');
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$user = $provider->getResourceOwner($token);
$provider->getLongLivedAccessToken($token);
$this->di->get('auth')->authenticateOrCreateFacebookUser($user->toArray());
}
}
}
and finally lets put the button:
<a href="<?php echo \CommonClasses\Utils\Main::getFacebookLoginUrl(); ?>" class="fa fa-3x fa-facebook-square"></a>
//this calls the first function that i wrote