Skip to content

Commit a52e245

Browse files
authored
Merge pull request #3 from middleDuckAi/middleDuck/fix-manager-session-regeneration
Fix manager session id regeneration on login
2 parents eb74354 + f16ce9f commit a52e245

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

src/Services/Users/UserLogin.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function authProcess()
303303

304304
public function writeSession()
305305
{
306-
$currentsessionid = session_regenerate_id();
306+
$currentsessionid = $this->regenerateSessionId();
307307

308308
$_SESSION['usertype'] = 'manager'; // user is a backend user
309309
// get permissions
@@ -337,6 +337,73 @@ public function writeSession()
337337

338338
}
339339

340+
protected function regenerateSessionId(): string
341+
{
342+
$sessionId = Str::random(40);
343+
$sessionData = (isset($_SESSION) && is_array($_SESSION)) ? $_SESSION : [];
344+
345+
if (session_status() === PHP_SESSION_ACTIVE) {
346+
session_unset();
347+
session_destroy();
348+
}
349+
350+
session_id($sessionId);
351+
session_start();
352+
353+
foreach ($sessionData as $key => $value) {
354+
$_SESSION[$key] = $value;
355+
}
356+
357+
$this->syncLaravelSessionId($sessionId);
358+
$this->refreshSessionCookie($sessionId);
359+
360+
return $sessionId;
361+
}
362+
363+
protected function syncLaravelSessionId(string $sessionId): void
364+
{
365+
if (!defined('EVO_SESSION') || !EVO_SESSION || !function_exists('session')) {
366+
return;
367+
}
368+
369+
try {
370+
$store = session()->driver();
371+
if (is_object($store) && method_exists($store, 'setId')) {
372+
$store->setId($sessionId);
373+
}
374+
} catch (\Throwable $exception) {
375+
// Native PHP session remains the source of truth when Laravel sync is unavailable.
376+
}
377+
}
378+
379+
protected function refreshSessionCookie(string $sessionId): void
380+
{
381+
if (headers_sent()) {
382+
return;
383+
}
384+
385+
$name = function_exists('config') ? (string) config('session.cookie', 'evo_session') : session_name();
386+
$lifetime = function_exists('config') ? (int) config('session.lifetime', 120) : 0;
387+
$expireOnClose = function_exists('config') ? (bool) config('session.expire_on_close', false) : true;
388+
389+
$options = [
390+
'expires' => $expireOnClose ? 0 : time() + ($lifetime * 60),
391+
'path' => function_exists('config') ? (string) config('session.path', '/') : '/',
392+
'domain' => function_exists('config') ? (string) config('session.domain', '') : '',
393+
'secure' => function_exists('config') ? (bool) config('session.secure', false) : false,
394+
'httponly' => function_exists('config') ? (bool) config('session.http_only', true) : true,
395+
];
396+
397+
$sameSite = function_exists('config') ? config('session.same_site') : null;
398+
if (!empty($sameSite)) {
399+
$options['samesite'] = (string) $sameSite;
400+
}
401+
402+
setcookie($name, $sessionId, $options);
403+
404+
$_COOKIE[$name] = $sessionId;
405+
}
406+
340407
public function checkRemember()
341408
{
342409

0 commit comments

Comments
 (0)