-1

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

CDO
  • 131
  • 1
  • 8
  • Controller's responsibility is to, based on user input, alter the state of model layer. So ... how exactly you manage to come to conclusion, that *"put all this code in a FacebookAuth function in the Controller"* would be appropriate? – tereško Oct 06 '14 at 08:45
  • I know is not appropriate, that's why I asked for help – CDO Oct 06 '14 at 09:13
  • You might find [this](http://stackoverflow.com/a/9685039/727208) useful. The way you authenticate a user should not be affected by the authentication form. – tereško Oct 06 '14 at 10:20

1 Answers1

-1

Your question is about just one specific case, but the real problem is you don't understand MVC well enough. I would suggest you to read an article explaining MVC. You also should use an existing MVC framework if you don't have any good reasons not to. Those frameworks pevent you from reinventing the wheel.

MVC in short: (M)odel is responsible for you data, (C)ontroller is responsible for handling that data (e.g. transforming, checking, ..), (V)iew is just a view, your controller should provide your view with data.

In your case that means the logic for checking a token and retrieving the loginUrl from the Facebook SDK is your controllers' responibility. Displaying the login url has to be done by your view.

Boyd
  • 723
  • 4
  • 15
  • 32
  • It also just so happens that **you** do not understand MVC. – tereško Oct 06 '14 at 08:58
  • Are you just going to say 'No' or provide some 'why' with that? – Boyd Oct 06 '14 at 09:06
  • "In your case that means the logic for checking a token and retrieving the loginUrl from the Facebook SDK is your controllers' responibility. Displaying the login url has to be done by your view." I exactly said that in my question, but i think it is the wrong way – CDO Oct 06 '14 at 09:10
  • 1
    @Boyd because, if you had actually read any materials that are not bundled with favorite-framework-of-month, you would know that model contains all of the business logic (not "data"), controllers are responsible for altering module based on user input (not "handling data" nor "providing data to view") and views are responsible presentation logic based on models state. The authentication of user would probably happen within a dedicated service, which would be part of model layer. And definitely **NOT** on the controller, which is part of presentation layer. – tereško Oct 06 '14 at 10:17