@@ -25,25 +25,27 @@ class SessionControlKill
25
25
*/
26
26
public function handle (Request $ request , Closure $ next ): Response
27
27
{
28
- $ user = Auth::user ();
29
- $ userSession = $ request ->session ()->get ('user_session ' );
30
-
31
- if ($ userSession ) {
32
- $ configIP = Setting::configByKey (self ::IP_RESTRICTION_KEY );
33
- $ configDevice = Setting::configByKey (self ::DEVICE_RESTRICTION_KEY );
34
-
35
- $ session = $ this ->getActiveSession ($ user , $ userSession );
36
-
37
- if ($ session ) {
38
- // Checks if the session has expired based on the IP address
39
- $ isSessionExpiredByIP = $ configIP === '2 ' && $ this ->isSessionExpiredByIP ($ session , $ request );
40
- // Checks if the session has expired based on the device
41
- $ isSessionExpiredByDevice = $ configDevice === '2 ' && $ this ->isSessionExpiredByDevice ($ session );
42
- // Checks if the session has expired except the one within the active device
43
- $ isAnyRestrictionEnabled = $ configIP === '1 ' || $ configDevice === '1 ' ;
44
-
45
- if ($ isSessionExpiredByIP || $ isSessionExpiredByDevice || $ isAnyRestrictionEnabled ) {
46
- return $ this ->killSessionAndRedirect ($ session );
28
+ if (Auth::check ()) {
29
+ $ user = Auth::user ();
30
+ $ userSession = $ request ->session ()->get ('user_session ' );
31
+
32
+ if ($ userSession ) {
33
+ $ configIP = Setting::configByKey (self ::IP_RESTRICTION_KEY );
34
+ $ configDevice = Setting::configByKey (self ::DEVICE_RESTRICTION_KEY );
35
+
36
+ $ session = $ this ->getActiveSession ($ user , $ userSession );
37
+
38
+ if ($ session ) {
39
+ // Checks if the session has expired based on the IP address
40
+ $ isSessionExpiredByIP = $ configIP === '2 ' && $ this ->isSessionExpiredByIP ($ session , $ request );
41
+ // Checks if the session has expired based on the device
42
+ $ isSessionExpiredByDevice = $ configDevice === '2 ' && $ this ->isSessionExpiredByDevice ($ session );
43
+ // Checks if the session has expired except the one within the active device
44
+ $ isAnyRestrictionEnabled = $ configIP === '1 ' || $ configDevice === '1 ' ;
45
+
46
+ if ($ isSessionExpiredByIP || $ isSessionExpiredByDevice || $ isAnyRestrictionEnabled ) {
47
+ return $ this ->killSessionAndRedirect ($ session );
48
+ }
47
49
}
48
50
}
49
51
}
@@ -57,8 +59,8 @@ private function getActiveSession(User $user, string $userSession): ?UserSession
57
59
->where ([
58
60
['is_active ' , true ],
59
61
['token ' , $ userSession ],
60
- ['expired_date ' , '!= ' , null ],
61
62
])
63
+ ->whereNotNull ('expired_date ' )
62
64
->first ();
63
65
}
64
66
0 commit comments