/** * Successful login with Facebook. */ public function onLoginWithFacebook() { $newUser = false; try { $accessToken = GraphAPI::instance()->getUserAccessToken(true); $response = GraphAPI::instance()->get('/me', ['id', 'email', 'name'], $accessToken); $facebookUser = $response->getGraphUser(); } catch (SystemException $e) { Flash::error('Facebook login failed.'); return; } $externalUser = UserExternalModel::facebook([$facebookUser->getId()])->first(); if (!$externalUser) { // No user found by Facebook user id $user = UserModel::where('email', '=', $facebookUser->getEmail())->first(); if (!$user) { // No user found by email either, sign up $newUser = true; $password = uniqid(); $signup = ['username' => $facebookUser->getName(), 'name' => $facebookUser->getName(), 'email' => $facebookUser->getEmail(), 'password' => $password, 'password_confirmation' => $password]; try { $user = Auth::register($signup, true); } catch (ModelException $e) { Log::error('Facebook sign up failed: ' . $e->getMessage()); $signup['username'] .= rand(1000, 9999); try { $user = Auth::register($signup, true); } catch (ModelException $e) { Log::error('Facebook sign up failed: ' . $e->getMessage()); Flash::error(Lang::get('klubitus.facebook::lang.register.register_failed')); return; } } } UserExternalModel::create(['user_id' => $user->id, 'provider' => UserExternalModel::PROVIDER_FACEBOOK, 'external_user_id' => $facebookUser->getId(), 'token' => $accessToken->getValue(), 'expires_at' => $accessToken->getExpiresAt()]); } else { $externalUser->token = $accessToken->getValue(); $externalUser->expires_at = $accessToken->getExpiresAt(); $externalUser->save(); $user = $externalUser->user; } Auth::login($user, true); if (post('redirect', true)) { if ($newUser) { return Redirect::to(post('redirect_signup', URL::previous() ?: Request::fullUrl())); } else { return Redirect::to(post('redirect_login', URL::previous() ?: Request::fullUrl())); } } }
/** * Update Event with missing data using GraphAPI. * * @param EventModel $event * @param bool $save * @return string Error message, if any * @throws SystemException */ protected function updateEvent(EventModel $event, $save) { static $accessToken; if (!$accessToken) { try { $externalUser = UserExternalModel::where('user_id', $this->importUserId)->where('provider', UserExternalModel::PROVIDER_FACEBOOK)->firstOrFail(); $accessToken = $externalUser->token; } catch (ModelNotFoundException $e) { $accessToken = GraphAPI::instance()->getAppAccessToken(); } } try { $response = GraphAPI::instance()->get('/' . $event->facebook_id, ['cover', 'description', 'place{id,name,location}', 'ticket_uri'], $accessToken); } catch (SystemException $e) { return $e->getMessage(); } $eventObject = $response->getGraphEvent(); $coverObject = $eventObject->getCover(); $event->info = $eventObject->getDescription(); $event->ticket_url = $eventObject->getTicketUri(); // Don't update flyer if the current is uploaded rather than linked if (!$event->flyer_id) { $event->flyer_url = $event->flyer_front_url = $coverObject->getSource(); } return $this->updateVenue($event, $eventObject->getPlace(), $save); }