Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
fix(Auth): Fix user session can't storage in database
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Aug 15, 2019
1 parent e0fb4f6 commit 30b1049
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions apps/models/form/Auth/UserLoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ private function createUserSession()
$payload['exp'] = $cookieExpire;

// Custom Payload key
$login_ip = app()->request->getClientIp();
if ($this->securelogin === 'yes' || config('security.secure_login') > 1) {
$login_ip = app()->request->getClientIp();
$payload['ip'] = sprintf('%08x', crc32($login_ip)); // Store User Login IP ( in CRC32 format )
}

Expand All @@ -178,6 +178,13 @@ private function createUserSession()
$this->jwt_payload = $payload;
$jwt = JWTHelper::encode($payload);

// Store User Login Session Information in database
app()->pdo->createCommand('INSERT INTO sessions (`uid`, `session`, `login_ip`, `login_at`, `expired`) ' .
'VALUES (:uid, :sid, INET6_ATON(:login_ip), NOW(), :expired)')->bindParams([
'uid' => $this->jwt_payload['aud'], 'sid' => $this->jwt_payload['jti'], 'login_ip' => $login_ip,
'expired' => ($this->logout === 'yes') ? 0 : -1, // -1 -> never expired , 0 -> auto_expire after 15 minutes, 1 -> expired
])->execute();

// Sent JWT content AS Cookie
app()->response->setCookie(Constant::cookie_name, $jwt, $cookieExpire, '/', '', false, true);
}
Expand All @@ -186,13 +193,6 @@ private function updateUserLoginInfo()
{
$ip = app()->request->getClientIp();

// Store User Login Session Information in database
app()->pdo->createCommand('INSERT INTO sessions (`uid`, `session`, `login_ip`, `login_at`, `expired`) ' .
'VALUES (:uid, :sid, INET6_ATON(:login_ip), NOW(), :expired)')->bindParams([
'uid' => $this->jwt_payload['user_id'], 'sid' => $this->jwt_payload['jti'], 'login_ip' => $ip,
'expired' => ($this->logout === 'yes') ? 0 : -1, // -1 -> never expired , 0 -> auto_expire after 15 minutes, 1 -> expired
])->execute();

// Update User Tables
app()->pdo->createCommand('UPDATE `users` SET `last_login_at` = NOW() , `last_login_ip` = INET6_ATON(:ip) WHERE `id` = :id')->bindParams([
'ip' => $ip, 'id' => $this->self['id']
Expand Down

0 comments on commit 30b1049

Please sign in to comment.