public function callback() { $code = Input::get('code'); if (strlen($code) == 0) { return \Redirect::to('/'); } $facebook = new Facebook(\Config::get('facebook')); $uid = $facebook->getUser(); if ($uid == 0) { return \Redirect::to('/'); } $me = $facebook->api('/me'); $profile = User::where('user_id', $uid)->first(); if (empty($profile)) { $user = new User(); $user->username = $me['name']; $user->photo = 'https://graph.facebook.com/' . $me['id'] . '/picture?type=large'; $user->user_id = $uid; $user->save(); $x = new leaderboard(); $x->user_id = $uid; $x->user_name = $me['name']; $x->round_id = 1; $x->save(); } $user = User::where('user_id', $uid)->select('user_id', 'username', 'photo')->first(); session()->put(['user_id' => $uid, 'name' => $user['username']]); return redirect('/'); }
* * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ use Facebook\Facebook; use Facebook\FacebookApiException; require '../src/Facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array('appId' => '344617158898614', 'secret' => '6dc8ac871858b34798bc2488200e503d')); // Get User ID $user = $facebook->getUser(); // We may or may not have this data based on whether the user is logged in. // // If we have a $user id here, it means we know the user is logged into // Facebook, but we don't know if the access token is valid. An access // 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.
/** * 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(); }