2323use Grav \Common \User \Interfaces \UserInterface ;
2424use Grav \Common \Utils ;
2525use Grav \Common \Uri ;
26+ use Grav \Events \BeforeSessionStartEvent ;
2627use Grav \Events \PluginsLoadedEvent ;
2728use Grav \Events \SessionStartEvent ;
2829use Grav \Framework \Flex \Interfaces \FlexCollectionInterface ;
@@ -62,6 +63,9 @@ class LoginPlugin extends Plugin
6263 /** @var Invitation|null */
6364 protected $ invitation ;
6465
66+ protected $ temp_redirect ;
67+ protected $ temp_messages ;
68+
6569 /**
6670 * @return array
6771 */
@@ -70,6 +74,7 @@ public static function getSubscribedEvents(): array
7074 return [
7175 PluginsLoadedEvent::class => [['onPluginsLoaded ' , 10 ]],
7276 SessionStartEvent::class => ['onSessionStart ' , 0 ],
77+ BeforeSessionStartEvent::class => ['onBeforeSessionStart ' , 0 ],
7378 PageAuthorizeEvent::class => ['onPageAuthorizeEvent ' , -10000 ],
7479 'onPluginsInitialized ' => [['initializeSession ' , 10000 ], ['initializeLogin ' , 1000 ]],
7580 'onTask.login.login ' => ['loginController ' , 0 ],
@@ -124,6 +129,18 @@ public function onPluginsLoaded(): void
124129 };
125130 }
126131
132+ /**
133+ * @param BeforeSessionStartEvent $event
134+ * @return void
135+ */
136+ public function onBeforeSessionStart (BeforeSessionStartEvent $ event ): void
137+ {
138+ $ session = $ event ->session ;
139+ $ this ->temp_redirect = $ session ->redirect_after_login ?? null ;
140+ $ this ->temp_messages = $ session ->messages ;
141+ }
142+
143+
127144 /**
128145 * @param SessionStartEvent $event
129146 * @return void
@@ -132,6 +149,15 @@ public function onSessionStart(SessionStartEvent $event): void
132149 {
133150 $ session = $ event ->session ;
134151
152+ if (isset ($ this ->temp_redirect )) {
153+ $ session ->redirect_after_login = $ this ->temp_redirect ;
154+ unset($ this ->temp_redirect );
155+ }
156+ if (isset ($ this ->temp_messages )) {
157+ $ session ->messages = $ this ->temp_messages ;
158+ unset($ this ->temp_messages );
159+ }
160+
135161 $ user = $ session ->user ?? null ;
136162 if ($ user && $ user ->exists () && ($ this ->config ()['session_user_sync ' ] ?? false )) {
137163 // User is stored into the filesystem.
0 commit comments