File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -121,3 +121,34 @@ option or by calling `disableIdentityCheck` from the controller's `beforeFilter(
121121``` php
122122$this->Authentication->disableIdentityCheck();
123123```
124+
125+ ## Redirecting after login
126+
127+ For the common post-login redirect flow, use ` redirectAfterLogin() ` :
128+
129+ ``` php
130+ public function login(): ?\Cake\Http\Response
131+ {
132+ $result = $this->Authentication->getResult();
133+
134+ if ($result && $result->isValid()) {
135+ return $this->Authentication->redirectAfterLogin('/home');
136+ }
137+
138+ return null;
139+ }
140+ ```
141+
142+ This uses the plugin's validated login redirect target from the current
143+ request when available and falls back to the default you provide.
144+
145+ If you need to inspect the validated target before redirecting, use
146+ ` getLoginRedirect() ` instead:
147+
148+ ``` php
149+ $target = $this->Authentication->getLoginRedirect('/home');
150+ return $this->redirect($target);
151+ ```
152+
153+ Avoid reading raw ` redirect ` query string parameters and passing them directly
154+ to the controller's ` redirect() ` method.
Original file line number Diff line number Diff line change @@ -581,8 +581,8 @@ $service->setConfig([
581581]);
582582```
583583
584- Then in your controller's login method you can use ` getLoginRedirect() ` to get
585- the redirect target safely from the query string parameter :
584+ Then in your controller's login method you can use
585+ ` redirectAfterLogin() ` for the common safe post-login redirect flow :
586586
587587``` php
588588public function login(): ?\Cake\Http\Response
@@ -591,19 +591,21 @@ public function login(): ?\Cake\Http\Response
591591
592592 // Regardless of POST or GET, redirect if user is logged in
593593 if ($result->isValid()) {
594- // Use the redirect parameter if present.
595- $target = $this->Authentication->getLoginRedirect();
596- if (!$target) {
597- $target = ['controller' => 'Pages', 'action' => 'display', 'home'];
598- }
599-
600- return $this->redirect($target);
594+ return $this->Authentication->redirectAfterLogin([
595+ 'controller' => 'Pages',
596+ 'action' => 'display',
597+ 'home',
598+ ]);
601599 }
602600
603601 return null;
604602}
605603```
606604
605+ If you need to inspect the validated target before redirecting, you can still
606+ use ` getLoginRedirect() ` directly and handle the response yourself. Avoid
607+ passing raw query string parameters to the controller's ` redirect() ` method.
608+
607609## Having Multiple Authentication Flows
608610
609611In an application that provides both an API and a web interface
Original file line number Diff line number Diff line change @@ -173,9 +173,7 @@ public function login(): ?\Cake\Http\Response
173173 $result = $this->Authentication->getResult();
174174 // If the user is logged in send them away.
175175 if ($result && $result->isValid()) {
176- $target = $this->Authentication->getLoginRedirect() ?? '/home';
177-
178- return $this->redirect($target);
176+ return $this->Authentication->redirectAfterLogin('/home');
179177 }
180178 if ($this->request->is('post')) {
181179 $this->Flash->error('Invalid username or password');
Original file line number Diff line number Diff line change @@ -288,8 +288,8 @@ $service->setConfig([
288288]);
289289```
290290
291- Then in your controller's login method you can use ` getLoginRedirect() ` to get
292- the redirect target safely from the query string parameter :
291+ Then in your controller's login method you can use
292+ ` redirectAfterLogin() ` for the common safe post-login redirect flow :
293293
294294``` php
295295public function login(): ?\Cake\Http\Response
@@ -298,19 +298,20 @@ public function login(): ?\Cake\Http\Response
298298
299299 // Regardless of POST or GET, redirect if user is logged in
300300 if ($result->isValid()) {
301- // Use the redirect parameter if present.
302- $target = $this->Authentication->getLoginRedirect();
303- if (!$target) {
304- $target = ['controller' => 'Pages', 'action' => 'display', 'home'];
305- }
306-
307- return $this->redirect($target);
301+ return $this->Authentication->redirectAfterLogin([
302+ 'controller' => 'Pages',
303+ 'action' => 'display',
304+ 'home',
305+ ]);
308306 }
309307
310308 return null;
311309}
312310```
313311
312+ If you need to inspect the validated target before redirecting, you can still
313+ use ` getLoginRedirect() ` directly and then call ` redirect() ` yourself.
314+
314315## Migrating Hashing Upgrade Logic
315316
316317If your application uses ` AuthComponent ` ’s hash upgrade
You can’t perform that action at this time.
0 commit comments