Premenna v signali komponenty
- Stic
- Člen | 28
Zdravim,
snazim sa rozchodit prihlasovanie pomocou Google v mojej aplikacii. Pokial som prihlasovanie riesil priamo v presenteri pomocou dvoch action metod, tak vsetko fungovalo spolahlivo.
Presuvam to vsak do komponent, aby som s tym v buducnosti mohol lahsie narabat. V komponente som zmenil action na handle a vyzera to teda takto:
<?php
/**
* Handle Google Login
*/
public function handleGoogle()
{
$url = $this->google->getLoginUrl(array(
'scope' => $this->presenter->context->parameters['google']['scope'],
'redirect_uri' => $this->link('//googleLogin'),
));
$this->presenter->redirectUrl($url);
}
/**
* Perform login with Google+
*/
public function handleGoogleLogin($code, $error = NULL)
{
if($error) {
$this->flashMessage('Please allow this application to access your Google account in order to log in.');
$this->presenter->redirect('Homepage:');
}
$g = $this->google;
$token = $g->getToken($code, $this->link('//googleLogin'));
$profile = $g->getInfo($token);
try {
$this->presenter->getUser()->setExpiration('+20 minutes', FALSE);
$this->presenter->getUser()->login('google',$profile); // Authentication successful, login in!
$this->presenter->flashMessage('Successfully Logged in with your Google account.', 'alert-success');
$this->presenter->redirect('Signup:');
} catch ( Nette\Security\AuthenticationException $e) { // Authentication Error
$this->presenter->flashMessage($e->getMessage(),'alert-error');
$this->presenter->redirect('Homepage:');
}
}
?>
Presmerovanie na Google stranky prebehne v poriadku, google presmeruje spat na moj signal GoogleLogin, no napriek tomu ze v Debugger bare vidim v requeste premennu $code normalnu ako mi ju vygeneroval google api, tak v metode handleGoogleLogin uz tuto premennu vidim iba ako NULL.
Viete ma nasmerovat kde mozem robit chybu?
Pouzivam Nette 2.1 dev
Dakujem
- David Matějka
- Moderator | 6445
aby se do komponenty, ktera se jmenuje napriklad „foo“, dostal parameter
„code“, musi byt v requestu foo-code=blabla
, pokud tedy google
vzdycky vraci ten parametr „code“ bez moznosti nastaveni, budes si to muset
rucne vytahnout z http requestu
- s4muel
- Člen | 92
@Stic: trochu kratsie, ale princip rovnaky.
$this->getParameter('code');
ostatne veci sa daju tiez trochu skratit
$this->presenter->redirect('Signup:');
$this->presenter->getUser()...;
$this->presenter->flashMessage($e->getMessage(),'alert-error');
...
//na
$this->redirect('Signup:');
$this->getUser()...;
$this->flashMessage($e->getMessage(),'alert-error');
...
- Stic
- Člen | 28
s4muel napsal(a):
@Stic: trochu kratsie, ale princip rovnaky.
$this->getParameter('code');
Toto nefunguje, funguje len ked to zmenim na:
<?php
$code = $this->presenter->getParameter('code');
?>
ostatne veci sa daju tiez trochu skratit
$this->presenter->redirect('Signup:'); $this->presenter->getUser()...; $this->presenter->flashMessage($e->getMessage(),'alert-error'); ... //na $this->redirect('Signup:'); $this->getUser()...; $this->flashMessage($e->getMessage(),'alert-error'); ...
skratenie s vynechanim „$this->presenter“ ako si napisal, rovnako v komponente nefunguje…ked tam dam aj „presenter“ tak to normalne ide.
Pouzivam dev.2.1, mam factory na komponentu v configu, v presenteri injectujem tu factory a pouzijem takto:
<?php
public function createComponentSocialLogin()
{
return $this->socialLoginControlFactory->create();
}
?>
Je to tak predsa spravne ci? Snazil som sa vychadzat z navodu v sekcii „planette“