protected function updateAuthToken(Entity\Users $userEntity)
    {
        $userName = $userEntity->getUsername();
        $cookieLifetime = (int) $this->cookieOptions['lifetime'];
        $repo = $this->getRepositoryAuthtoken();
        try {
            $tokenEntity = $repo->getUserToken($userEntity->getId(), $this->getClientIp(), $this->getClientUserAgent());
        } catch (DriverException $e) {
            /** @deprecated workaround until v4 */
            if (strpos('no such column: user_id', $e->getMessage()) !== false) {
                throw $e;
            }
            $tokenEntity = false;
        }

        if ($tokenEntity) {
            $token = $tokenEntity->getToken();
        } else {
            $salt = $this->randomGenerator->generateString(32);
            $token = $this->getAuthToken($userEntity->getId(), $salt);

            $tokenEntity = new Entity\Authtoken();
            $tokenEntity->setUserId($userEntity->getId());
            $tokenEntity->setToken($token);
            $tokenEntity->setSalt($salt);
        }

        $tokenEntity->setValidity(Carbon::create()->addSeconds($cookieLifetime));
        $tokenEntity->setIp($this->getClientIp());
        $tokenEntity->setLastseen(Carbon::now());
        $tokenEntity->setUseragent($this->getClientUserAgent());

        try {
            $this->getRepositoryAuthtoken()->save($tokenEntity);
        } catch (DriverException $e) {
            /** @deprecated workaround until v4 */
            if (strpos('no such column: user_id', $e->getMessage()) !== false) {
                throw $e;
            }
        }

        $this->systemLogger->debug("Saving new login token '$token' for user ID '$userName'", ['event' => 'authentication']);

        return $tokenEntity;
    }