public function updateToken(User $user)
    {
        if (!$user->has('id')) {
            throw new FatalErrorException(__d('user', 'UsersTable::updateToken(), no ID was found for the given entity.'));
        }

        $token = md5(uniqid($user->id, true));
        $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
        while ($count > 0) {
            $token = str_shuffle(md5(uniqid($user->id, true) . rand(1, 9999)));
            $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
        }

        $user->set('token', $token);
        $user->set('token_expiration', time() + USER_TOKEN_EXPIRATION);
        $this->updateAll([
            'token' => $user->get('token'),
            'token_expiration' => $user->get('token_expiration'),
        ], ['id' => $user->id]);

        return $user;
    }