What i want ? I want to make a proper Facebook login in MVC
My directory tree (simplified) :
index.php
/controllers
/users.class.php
/core
/libs
/facebook-sdk (contains Facebook SDK files)
/models
/user.class.php
/views
/login.php
/user
/index.php
My problem : I know how to make a one-file Facebook Login, i know how to make a Form Login in MVC, but i'm new to Facebook PHP SDK Framework.
My one page code (simplified) :
// Autoload the required files
require_once(ROOT.'core/libs/facebook-sdk/autoload.php');
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
...
// Initialize the SDK
FacebookSession::setDefaultApplication('APP_ID', 'APP_SECRET');
// Create the login helper
$helper = new FacebookRedirectLoginHelper('REDIRECT_URL');
// Check if existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// Create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// Validate the access_token to make sure it's still valid
try {
if ( ! $session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// Catch any exceptions
$session = null;
}
} else {
// No session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
}
// Check if a session exists
if ( isset( $session ) ) {
// Save the session
$_SESSION['fb_token'] = $session->getToken();
// Create session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
} else {
// No session
// Get login URL
$loginUrl = $helper->getLoginUrl();
echo '<a href="' . $loginUrl . '">Log in</a>';
}
If i refer to my MVC understanding i have to put all this code in a FacebookAuth function in the Controller (except the login button?) ? Do i need to include the Facebook SDK in all my application? or just for the login?
Thanks you in advance