diff --git a/server/controllers/user/login.php b/server/controllers/user/login.php index 54357d80..5e1fb8a7 100644 --- a/server/controllers/user/login.php +++ b/server/controllers/user/login.php @@ -5,7 +5,8 @@ class LoginController extends Controller { private $userInstance; private $session; - + private $rememberToken; + public function validations() { return [ 'permission' => 'any', @@ -19,8 +20,9 @@ class LoginController extends Controller { return; } - if ($this->areCredentialsValid()) { + if ($this->areCredentialsValid() || $this->isRememberTokenValid()) { $this->createUserSession(); + $this->createSessionCookie(); Response::respondSuccess($this->getUserData()); } else { @@ -36,6 +38,20 @@ class LoginController extends Controller { return ($this->getUserByInputCredentials() !== null); } + private function isRememberTokenValid() { + $rememberToken = Controller::request('rememberToken'); + + if ($rememberToken) { + $sessionCookie = SessionCookie::getDataStore($rememberToken, 'token'); + $userid = Controller::request('userId'); + + if ($sessionCookie !== null && $userid === $sessionCookie->user->id) { + $this->userInstance = $sessionCookie->user; + return true; + } + } + } + private function createUserSession() { $this->getSession()->createSession($this->userInstance->id); } @@ -46,7 +62,8 @@ class LoginController extends Controller { return array( 'userId' => $userInstance->id, 'userEmail' => $userInstance->email, - 'token' => $this->getSession()->getToken() + 'token' => $this->getSession()->getToken(), + 'rememberToken' => $this->rememberToken ); } @@ -68,4 +85,19 @@ class LoginController extends Controller { return $this->session; } + private function createSessionCookie(){ + $remember = Controller::request('remember'); + if ($remember) { + $this->rememberToken = Hashing::generateRandomToken(); + + $sessionCookie = new SessionCookie(); + $sessionCookie->setProperties(array( + 'user' => $this->userInstance->getBeanInstance(), + 'token' => $this->rememberToken, + 'ip' => $_SERVER['REMOTE_ADDR'], + 'creationDate' => date('d-m-Y (H:i:s)') + )); + $sessionCookie->store(); + } + } } diff --git a/server/libs/Hashing.php b/server/libs/Hashing.php index 01ef7ff4..7b296357 100644 --- a/server/libs/Hashing.php +++ b/server/libs/Hashing.php @@ -7,4 +7,7 @@ class Hashing { public static function verifyPassword($password, $hash) { return password_verify($password, $hash); } + public static function generateRandomToken() { + return md5(uniqid(rand())); + } } \ No newline at end of file diff --git a/server/models/Session.php b/server/models/Session.php index 3c9d4f5e..3d543607 100644 --- a/server/models/Session.php +++ b/server/models/Session.php @@ -65,6 +65,6 @@ class Session { } private function generateToken() { - return md5(uniqid(rand())); + return Hashing::generateRandomToken();; } } \ No newline at end of file diff --git a/server/models/SessionCookie.php b/server/models/SessionCookie.php new file mode 100644 index 00000000..e66adc5e --- /dev/null +++ b/server/models/SessionCookie.php @@ -0,0 +1,19 @@ +