Facebook login v navigation
- Raziner
- Člen | 5
Dobrý den, potřeboval bych pomoc s FB loginem. Potřeboval bych dát FB login do navigation, ale hlásí mi to chybu Undefined variable: fbLink. Já ho mám danej zvlášť v jiné stránce, ale nejde mi to dát do navigation.
Takhle vypadá BasePresenter:
<?php
namespace App\Presenters;
use Nette;
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
const CSS_PATH = PUBLIC_HTML_PATH . "/css/all-min.css";
const JS_PATH = PUBLIC_HTML_PATH . "/js/all-min.js";
public function startup()
{
parent::startup();
if($this->getName() != "Unlogged" && !$this->getUser()->isLoggedIn())
{
$this->redirect('Unlogged:login');
}
}
public function actionLogout()
{
$this->getUser()->logOut();
$this->redirect("Unlogged:default");
}
public function beforeRender()
{
$this->template->productionMode = \Tracy\Debugger::$productionMode;
$this->template->jsVersion = filemtime(self::JS_PATH);
$this->template->cssVersion = filemtime(self::CSS_PATH);
}
}
a takhle UnloggedPresenter:
<?php
namespace App\Presenters;
use App\Model\User;
use Facebook\Facebook;
use Nette\Application\UI;
class UnloggedPresenter extends BasePresenter
{
/** @var string[] */
private $config;
/** @var User */
private $user;
/** @var Facebook */
private $fb;
/**
* @param string[] $config
* @param User $user
* @param Facebook $fb
*/
protected function createComponentRezervace()
{
$form = new UI\Form;
$form ->addText("vyhledat", "vyhledat");
return $form;
}
public function __construct($config = null, User $user)
{
$this->config = $config;
$this->user = $user;
$this->fb = new Facebook($config);
//\Tracy\Debugger::barDump($this->user->getUserByEmail("nicola.zemanova25@gmail.com"));
}
public function renderDefault()
{
}
public function actionLogin()
{
if($this->getUser()->isLoggedIn())
{
$this->redirect(":default");
}
$helper = $this->fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
if($accessToken)
{
$response = $this->fb->get('/me?fields=id,name,email', $accessToken);
$user = $response->getGraphUser();
$existing = $this->user->getUserByEmail($user['email']);
if($existing)
{
$existing = $this->user->updateUser($existing['id'], $accessToken);
}
else
{
$existing = $this->user->addUser("facebook", $accessToken, $user['email'], $user['name']);
}
$this->getUser()->login(new \Nette\Security\Identity($existing['id'], null, $existing));
$this->redirect("Profile:default");
}
$permissions = ['email'];
$returnUrl = $this->getHttpRequest()->getUrl()->baseUrl . "prihlaseni";
$this->template->fbLink = $helper->getLoginUrl($returnUrl, $permissions);
}
}
odkaz na ten FB login:
<div class=„modal-body“>
<a href=„{$fbLink}“> Přihlásit se přes Facebook
</a>
Editoval Raziner (5. 4. 2018 17:27)
- David Matějka
- Moderator | 6445
ahoj, komponenta je pro zacatek mozna az moc slozita, nyni bude stacit, aby si to posilal do te sablony na kazde strance – tedy v base presenteru, kde nyni posilas do sablony treba to jsVersion