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 @@ -550,6 +550,20 @@ private function checkRememberMeToken(string $remember)
550
550
*/
551
551
public function startLogin (User $ user ): void
552
552
{
553
+ /** @var int|string|null $userId */
554
+ $ userId = $ this ->getSessionKey ('id ' );
555
+
556
+ // Check if already logged in.
557
+ if ($ userId !== null ) {
558
+ throw new LogicException (
559
+ 'The user has User Info in Session, so already logged in or in pending login state. '
560
+ . ' If a logged in user logs in again with other account, the session data of the previous '
561
+ . ' user will be used as the new user. '
562
+ . ' Fix your code to prevent users from logging in without logging out or delete the session data. '
563
+ . ' user_id: ' . $ userId
564
+ );
565
+ }
566
+
553
567
$ this ->user = $ user ;
554
568
555
569
// Regenerate the session ID to help protect against session fixation
Original file line number Diff line number Diff line change 7
7
use CodeIgniter \Shield \Authentication \Authenticators \Session ;
8
8
use CodeIgniter \Shield \Config \Auth ;
9
9
use CodeIgniter \Shield \Entities \User ;
10
+ use CodeIgniter \Shield \Exceptions \LogicException ;
10
11
use CodeIgniter \Shield \Models \RememberModel ;
11
12
use CodeIgniter \Shield \Models \UserModel ;
12
13
use CodeIgniter \Shield \Result ;
@@ -340,6 +341,26 @@ public function testAttemptSuccess(): void
340
341
]);
341
342
}
342
343
344
+ public function testAttemptUserHavingSessionDataAttemptsAgain (): void
345
+ {
346
+ $ _SESSION ['user ' ]['id ' ] = '999 ' ;
347
+
348
+ $ this ->expectException (LogicException::class);
349
+ $ this ->expectExceptionMessage (
350
+ 'The user has User Info in Session, so already logged in or in pending login state. '
351
+ );
352
+
353
+ $ this ->user ->createEmailIdentity ([
354
+ 'email ' => 'foo@example.com ' ,
355
+ 'password ' => 'secret123 ' ,
356
+ ]);
357
+
358
+ $ this ->auth ->attempt ([
359
+ 'email ' => $ this ->user ->email ,
360
+ 'password ' => 'secret123 ' ,
361
+ ]);
362
+ }
363
+
343
364
public function testAttemptCaseInsensitive (): void
344
365
{
345
366
$ this ->user ->createEmailIdentity ([
You can’t perform that action at this time.
0 commit comments