indexdb_load static method

Future<IICaller?> indexdb_load()

Loads the user that is saved in the IndexDB if there is one.

The user's session might be expired so check it by the IICaller.legations.is_expired method, and the IICaller.legations.duration_to_expiration method.

Implementation

static Future<IICaller?> indexdb_load() async {
    IICaller? user;
    if (IndexDB.is_support_here()) {

        IndexDB idb = await IndexDB.open(_indexdb_name, [_indexdb_object_store_name]);

        CryptoKey? user_crypto_key_public = await idb.get_object(
            object_store_name: _indexdb_object_store_name,
            key: _indexdb_object_key_cryptokey_public
        ) as CryptoKey?;

        CryptoKey? user_crypto_key_private = await idb.get_object(
            object_store_name: _indexdb_object_store_name,
            key: _indexdb_object_key_cryptokey_private
        ) as CryptoKey?;

        List<Legation>? user_legations = (await idb.get_object(
            object_store_name: _indexdb_object_store_name,
            key: _indexdb_object_key_legations
        ) as List<dynamic>?).nullmap((list_dynamic)=>list_dynamic.cast<JSLegation>().map<Legation>(legation_of_a_jslegation).toList());

        idb.shutdown();

        if (
            user_crypto_key_public != null
            && user_crypto_key_private != null
            && user_legations != null
        ) {
            user = IICaller(
                keys: await SubtleCryptoECDSAP256Keys.of_the_cryptokeys(
                    public_key: user_crypto_key_public,
                    private_key: user_crypto_key_private
                ),
                legations: user_legations
            );
        }
    } else {
        print('IndexDB not supported here.');
    }

    return user;

}