SecureStorage

external class SecureStorage

This class provides access to the secure local storage, which is persistent and tied to the user's Telegram account. Data stored in secure storage is accessible only to the Web App that saved it.

Use this storage to save sensitive data like access tokens or user preferences that should persist across sessions. Keep in mind that storage size is limited to 2 kilobytes.

For non-sensitive data, consider using DeviceStorage.

See also

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
fun clear(callback: (error: SecureStorageErrorMessage?, isSuccessful: Boolean) -> Unit)

Clears all key-value pairs from the secure storage.

Link copied to clipboard
suspend fun SecureStorage.clear(): Boolean

Clears all key-value pairs from secure storage. This suspending function handles the result directly and throws an exception if an error occurs.

Link copied to clipboard

Clears all key-value pairs from the secure storage using a CompletableDeferred and returns a Result.

Link copied to clipboard
fun getItem(key: String, callback: (error: SecureStorageErrorMessage?, value: String?, canBeRestored: Boolean?) -> Unit)

Retrieves the value associated with a key from secure storage.

Link copied to clipboard
suspend fun SecureStorage.getItem(key: String): Either<String?, Boolean>

Retrieves the value associated with a key from secure storage. This function uses a callback-based approach for handling the asynchronous result of the operation.

Link copied to clipboard
suspend fun SecureStorage.getWithResult(key: String): Result<Either<String?, Boolean>>

Retrieves the value associated with a key from secure storage using a CompletableDeferred and returns a Result. This suspending function encapsulates the asynchronous operation of retrieving a value and provides a structured way to handle both successful retrieval and potential errors. It uses a CompletableDeferred to manage the asynchronous result.

Link copied to clipboard
fun removeItem(key: String, callback: (error: SecureStorageErrorMessage?, isSuccessful: Boolean) -> Unit)

Removes the key-value pair associated with a key from secure storage.

Link copied to clipboard

Removes the key-value pair associated with a key from secure storage. This suspending function handles the result directly and throws an exception if an error occurs.

Link copied to clipboard

Removes the key-value pair associated with a key from secure storage using a CompletableDeferred and returns a Result.

Link copied to clipboard
fun restoreItem(key: String, callback: (error: SecureStorageErrorMessage?, value: String?) -> Unit)

Restores the value associated with a key in secure storage. This is useful if the value was previously removed using removeItem and needs to be retrieved again. Note that restoring a value is only possible if it hasn't been overwritten by a new value for the same key.

Link copied to clipboard

Restores the value associated with a key in secure storage. This suspending function handles the result directly and throws an exception if an error occurs.

Link copied to clipboard

Restores the value associated with a key in secure storage using a CompletableDeferred and returns a Result.

Link copied to clipboard
fun setItem(key: String, value: String, callback: (error: SecureStorageErrorMessage?, isSuccessful: Boolean) -> Unit)

Stores a key-value pair in secure storage.

Link copied to clipboard
suspend fun SecureStorage.setItem(key: String, value: String): Boolean

Stores a key-value pair in secure storage. This suspending function handles the result directly and throws an exception if an error occurs.

Link copied to clipboard

Stores a key-value pair in secure storage using a CompletableDeferred and returns a Result.