File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
src/Authentication/Authenticators
tests/Authentication/Authenticators Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -527,6 +527,20 @@ private function checkRememberMeToken(string $remember)
527527 */
528528 public function startLogin (User $ user ): void
529529 {
530+ /** @var int|string|null $userId */
531+ $ userId = $ this ->getSessionKey ('id ' );
532+
533+ // Check if already logged in.
534+ if ($ userId !== null ) {
535+ throw new LogicException (
536+ 'The user has User Info in Session, so already logged in or in pending login state. '
537+ . ' If a logged in user logs in again with other account, the session data of the previous '
538+ . ' user will be used as the new user. '
539+ . ' Fix your code to prevent users from logging in without logging out or delete the session data. '
540+ . ' user_id: ' . $ userId
541+ );
542+ }
543+
530544 $ this ->user = $ user ;
531545
532546 // Regenerate the session ID to help protect against session fixation
Original file line number Diff line number Diff line change 77use CodeIgniter \Shield \Authentication \Authenticators \Session ;
88use CodeIgniter \Shield \Config \Auth ;
99use CodeIgniter \Shield \Entities \User ;
10+ use CodeIgniter \Shield \Exceptions \LogicException ;
1011use CodeIgniter \Shield \Models \RememberModel ;
1112use CodeIgniter \Shield \Models \UserModel ;
1213use CodeIgniter \Shield \Result ;
@@ -292,6 +293,26 @@ public function testAttemptSuccess(): void
292293 ]);
293294 }
294295
296+ public function testAttemptUserHavingSessionDataAttemptsAgain (): void
297+ {
298+ $ _SESSION ['user ' ]['id ' ] = '999 ' ;
299+
300+ $ this ->expectException (LogicException::class);
301+ $ this ->expectExceptionMessage (
302+ 'The user has User Info in Session, so already logged in or in pending login state. '
303+ );
304+
305+ $ this ->user ->createEmailIdentity ([
306+ 307+ 'password ' => 'secret123 ' ,
308+ ]);
309+
310+ $ result = $ this ->auth ->attempt ([
311+ 'email ' => $ this ->user ->email ,
312+ 'password ' => 'secret123 ' ,
313+ ]);
314+ }
315+
295316 public function testAttemptCaseInsensitive (): void
296317 {
297318 $ this ->user ->createEmailIdentity ([
You can’t perform that action at this time.
0 commit comments