example of using onLoggedIn

jdan1131x
Member | 41
+
0
-

the documentation says onLoggedIn is called as soon
as login occurs

https://doc.nette.org/en/glossary#…

but my code below does not work at all after login:

public function renderWelcome() {

$test = ‘default’;
$this->template->user->onLoggedIn[] = function ($test) {
redirect(‘Homepage:’ . $test);
 };

}
i tried to pass $this as var but Tracy no like :-)
not sure what I am missing. Any assistance much appreciated.
I've tried 10 diff ways to make it work. It does not redirect
but also does not throw an error. the user is redirected to the Welcome page
but not redirected Home. so …???

in the welcome page I can access $user and print identity, role, name info etc… so i know $user exists…
thanks!
James

Keeehi
Member | 36
+
0
-

Setting onLoggedIn callback in render<View> method is too late. Try it in startup method. Or it can be in action<Action>. The login form is handled in handle<Signal> so anything after that is too late.
Also, it should be without template. Just

$this->user->onLoggedIn[] = ...

Last edited by Keeehi (2021-04-06 23:22)

jdan1131x
Member | 41
+
0
-

thanks, i tried startup handlesignal, beforerender, action, but no luck
apache log: shows no redirect to Homepage:default under any method use…

192.168.0.251 – – [06/Apr/2021:14:35:22 –0700] “GET /sign/out HTTP/1.1” 302 105
192.168.0.251 – – [06/Apr/2021:14:35:22 –0700] “GET /?_fid=45fj HTTP/1.1” 200 30360
192.168.0.251 – – [06/Apr/2021:14:35:22 –0700] “GET /?_fid=45fj&_tracy_bar=js&v=2.8.3&XDEBUG_SESSION_STOP=1 HTTP/1.1” 200 13499
192.168.0.251 – – [06/Apr/2021:14:35:24 –0700] “GET /login/in HTTP/1.1” 200 19845
192.168.0.251 – – [06/Apr/2021:14:35:43 –0700] “POST /login/in HTTP/1.1” 303 118
192.168.0.251 – – [06/Apr/2021:14:35:43 –0700] “GET /login/welcome?_fid=vnsg HTTP/1.1” 200 33742
192.168.0.251 – – [06/Apr/2021:14:35:43 –0700] “GET /login/welcome?_fid=vnsg&_tracy_bar=js&v=2.8.3&XDEBUG_SESSION_STOP=1 HTTP/1.1” 200 13499

i think i will go walk the dog :-)
thanks again,
James

dakur
Member | 493
+
0
-

@jdan1131x Did you also remove the ->template from $this->template->user->onLoggedIn[] as Keeehi wrote? It is defined on Nette\Security\User, not on Nette\Bridges\ApplicationLatte\Template.

Last edited by dakur (2021-04-07 07:33)

dkorpar
Member | 135
+
0
-

@DavidGrudl I noticed that a lot of people are actually having issues with this…
Any chance to add some logic for those events to throw exception if they're already executed?
Ofcours can't be for all events, but those system ones for form submit and etc that you're sure are happening only once could be added… It would solve a lot of issues for juniors IMHO…

David Grudl
Nette Core | 8218
+
0
-

@dkorpar I do not understand what you mean. Can you explain more?

dkorpar
Member | 135
+
0
-

@DavidGrudl if someone tries to attach something to onSubmit event for example but after form was submitted to actually throw an exception that event is no longer valid. I've noticed that almost every developer I was teaching to nette (5+ junior devs) had this problem and wasn't sure what was going on (attaching to event that is already finished and then not realizing why this code isn't executed). If I wouldn't solve that they would be stuck for hours…

David Grudl
Nette Core | 8218
+
+1
-

@dkorpar I tried to add something like this to the forms https://github.com/…7a0af93af84b

@jdan1131x I don't know exactly what you want to achieve, but onLoggedIn doesn't seem like a good way. Use this way instead https://github.com/…resenter.php#…

jdan1131x
Member | 41
+
0
-

dakur wrote:

@jdan1131x Did you also remove the ->template from $this->template->user->onLoggedIn[] as Keeehi wrote? It is defined on Nette\Security\User, not on Nette\Bridges\ApplicationLatte\Template.

yes

jdan1131x
Member | 41
+
0
-

David Grudl wrote:

@dkorpar I tried to add something like this to the forms https://github.com/…7a0af93af84b

@jdan1131x I don't know exactly what you want to achieve, but onLoggedIn doesn't seem like a good way. Use this way instead https://github.com/…resenter.php#…

the 1st objective was to get the automatic call provided by onLoggedOn working and understand how to implement it
in a good way. the 2nd objective is to be able to understand the trivial redirect feature

Its a very useful feature just as custom filter is, for example.
Thanks for response, i do have the form handler working fine and use try/catch, etc.

What the third objective is most important. as i read documentation and explore API,
then I keep adding features and classes until have an extremely robust registration presenter, registration classes and Latte templates sitting on top of the framework. The work to do Login will nearly be trivial…

this accomplishes two more objectives, increased fluency with Nette et all and a Robust Forms system that can
be leveraged for use with all future forms…

since i have time then this first effort takes weeks to write and will be a Robust solution. the ROI on invested time is fantastic. Forms are the communications system/fulcrum point between users and persistent data and barbarians too :-)

regards,
James