// token is invalid if the user logged out of Facebook. if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } // Login or logout url will be needed depending on current user state. if ($user) { $logoutUrl = $facebook->getLogoutUrl(); } else { $statusUrl = $facebook->getLoginStatusUrl(); $loginUrl = $facebook->getLoginUrl(); } // This call will always work since we are fetching public data. $naitik = $facebook->api('/naitik'); ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>php-sdk</title> <style> body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; } h1 a { text-decoration: none; color: #3b5998;
/** * Generate and print the rendered login markup to STDOUT. * * @param array $form_options * * @return void */ public function renderLogin($form_options = []) { if (!FACEBOOK_APP_ID) { echo 'Please configure Facebook with your APP_ID.'; return; } if (!FACEBOOK_APP_SECRET) { echo 'Please configure Facebook with your APP_SECRET.'; return; } $facebook = new \Facebook(['appId' => FACEBOOK_APP_ID, 'secret' => FACEBOOK_APP_SECRET]); // User was already logged in. try { $user = $facebook->getUser(); if ($user) { $user_profile = $facebook->api('/me'); $facebooklink = false; } else { $facebooklink = $facebook->getLoginUrl(); } } catch (\Exception $c) { $facebooklink = $facebook->getLoginUrl(); } // $logoutUrl = $facebook->getLogoutUrl(); $tpl = \Core\Templates\Template::Factory('includes/user/facebook_login.tpl'); $tpl->assign('facebooklink', $facebooklink); $tpl->render(); }
public function login() { $facebook = new Facebook(\Config::get('facebook')); $params = array('redirect_uri' => url('/login/fb/callback'), 'scope' => 'email'); return \Redirect::to($facebook->getLoginUrl($params)); }
<?php use App\Classes\User; use App\Libs\Statics\Func; use App\Models\GoogleModel; use Carbon\Carbon; use Facebook\Facebook; return ['config' => ['cache' => path('resources.cache'), 'debug' => true], 'static_functions' => ['Url', 'Session', 'Token'], 'callable_functions' => ['social' => function ($c) { switch ($c) { case 'f': $url = new Facebook(); return $url->getLoginUrl(); case 'g': $client = new Google_Client(); $auth = new GoogleModel($client); return $auth->getAuthUrl(); } }, 'is_loggedin' => function () { $u = new User(); return $u->isLoggedIn(); }, 'time' => function ($time) { $t = new Carbon($time); return $t->toRfc850String(); }, 'readable_time' => function ($time) { $t = new Carbon($time); return $t->diffForHumans(); }, 'strip' => function ($string) { // strip tags to avoid breaking any html $string = strip_tags($string); if (strlen($string) > 500) { // truncate string
public function testGetLoginURLWithScopeParamsAsArray() { $facebook = new Facebook(array('appId' => self::APP_ID, 'secret' => self::SECRET)); // fake the HPHP $_SERVER globals $_SERVER['HTTP_HOST'] = 'www.test.com'; $_SERVER['REQUEST_URI'] = '/unit-tests.php'; $scope_params_as_array = array('email', 'sms', 'read_stream'); $extra_params = array('scope' => $scope_params_as_array, 'nonsense' => 'nonsense'); $login_url = parse_url($facebook->getLoginUrl($extra_params)); $this->assertEquals($login_url['scheme'], 'https'); $this->assertEquals($login_url['host'], 'www.facebook.com'); $this->assertEquals($login_url['path'], '/dialog/oauth'); // expect api to flatten array params to comma separated list // should do the same here before asserting to make sure API is behaving // correctly; $extra_params['scope'] = implode(',', $scope_params_as_array); $expected_login_params = array_merge(array('client_id' => self::APP_ID, 'redirect_uri' => 'http://www.test.com/unit-tests.php'), $extra_params); $query_map = array(); parse_str($login_url['query'], $query_map); $this->assertIsSubset($expected_login_params, $query_map); // we don't know what the state is, but we know it's an md5 and should // be 32 characters long. $this->assertEquals(strlen($query_map['state']), $num_characters = 32); }