public function generateAutologinTokenAndStoreHash($lifetime = null)
    {
        if ($lifetime !== null) {
            Deprecation::notice(
                '5.0',
                'Passing a $lifetime to Member::generateAutologinTokenAndStoreHash() is deprecated,
                    use the Member.auto_login_token_lifetime config setting instead',
                Deprecation::SCOPE_GLOBAL
            );
            $lifetime = (86400 * $lifetime); // Method argument is days, convert to seconds
        } else {
            $lifetime = $this->config()->auto_login_token_lifetime;
        }

        do {
            $generator = new RandomGenerator();
            $token = $generator->randomToken();
            $hash = $this->encryptWithUserSettings($token);
        } while (DataObject::get_one(Member::class, array(
            '"Member"."AutoLoginHash"' => $hash
        )));

        $this->AutoLoginHash = $hash;
        $this->AutoLoginExpired = date('Y-m-d H:i:s', time() + $lifetime);

        $this->write();

        return $token;
    }