Jump to content

facebook login integration


Frank Vèssia
 Share

Recommended Posts

Hi guys, I'm trying to integrate the facebook sdk to register/login users but I'm getting a strange error when I try to create a new user after I get the facebook token.
 


Fatal error: Class 'User' not found in /var/www/vhosts/httpdocs/site/templates/social_fb_callback.php on line 41

it's possible the facebook sdk interferes with pw classes?

this is the complete code. I'm on PW 3.0.10
 

<?php
   require_once('../Facebook/autoload.php');
 
   $fb = new Facebook\Facebook([
     'app_id' => 'xxxxx',
     'app_secret' => 'xxxxx',
     'default_graph_version' => 'v2.5',
   ]);

    $helper = $fb->getRedirectLoginHelper();
    try {
        $accessToken = $helper->getAccessToken();
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
 
    if (isset($accessToken)) {
        // Logged in!
        $session->facebook_access_token = (string) $accessToken;
        // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($session->facebook_access_token);

        $token_fb  = $sanitizer->selectorValue($session->facebook_access_token);
        $checkUser = $users->get("token_fb=$token_fb");
        if($checkUser instanceof NullPage){
            // new user
            try {
               
                $fb->setDefaultAccessToken($session->facebook_access_token);
                $res      = $fb->get('/me?fields=first_name,last_name,email');
                $userNode = $res->getGraphObject();
                // get profile picture
                $res     = $fb->get( '/me/picture?type=large&redirect=false');
                $picture = $res->getGraphObject();
                // get email 
                if($userNode->getProperty( 'email' ) == ''){
                    $uemail = "tocomplete";
                }else{
                    $uemail = "";
                }
                $nome    = $userNode->getProperty( 'first_name' );
                $cognome = $userNode->getProperty( 'last_name' );
                $pass    = "vogliadi".rand(100,10000);
                // creo username dal nome e cognome e se esiste aggiungo un numero alla fine.
                $username = strtolower($nome).strtolower($cognome);
                $checkuser = $users->get("name=$username,token_fb!=$session->facebook_access_token");
                if($checkuser->id){
                    $username = $username.rand(10,100);
                }
                //registro utente
                $u  = new User();
                $u->name  = $username;
                $u->pass  = $pass;
                $u->passclear  = $pass;
                $u->email = $uemail;
                $u->nome = $nome;
                $u->cognome = $cognome;
                $u->token_fb    = $session->facebook_access_token;
                $u->privacy_newsletter = 1;
                $u->addRole("guest");
                $u->addRole("member");
                if($u->save()){
                    $u->profilepic = $picture['url'];
                    $u->save();
                    $session->login($u->name,$u->pass);
                }
                $session->redirect("/",false);
 
            } catch(Facebook\Exceptions\FacebookResponseException $e) {
              // When Graph returns an error
              echo 'Graph returned an error: ' . $e->getMessage();
              exit;
            } catch(Facebook\Exceptions\FacebookSDKException $e) {
              // When validation fails or other local issues
              echo 'Facebook SDK returned an error: ' . $e->getMessage();
              exit;
            }
        }else{
            // login
            $session->login($checkUser->name,$checkUser->password);
            $session->redirect("/",false);
        }
       
    }

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...