Storing and Accessing User Inputs

If your extension needs to store user inputs (such as API keys, access tokens, or settings), you can use the UserDefault class to implement this functionality.

Encryption

TeamSQL SDK encrypts your data using the teamsql.secret you defined in the package.json file. TeamSQL uses AES-256 encryption.

Usage

Import the UserDefault class:

import { UserDefaults } from "@teamsql-extensions/ts-extension-sdk/dist/source/user-defaults/UserDefaults"

Storing Data

When adding data, the first parameter you provide is the key and the second parameter is the value.

UserDefaults.add("apikey", apiKey);

You can only store string values. If you'd like to store other data types, such as objects or JSON snippets, you can stringify your data.

let settings = {
    apiKey: "442sAxz-22sD5HGONQ6BSXP0Y2-anhsgq1",
    environment: "development",
    userId: 19836351
}
let settingsJsonValue = JSON.stringify(settings)
UserDefaults.add("user_prefs", settingsJsonValue);

Retrieving Data

The get function of UserDefaults expects only one parameter, which is the key. It returns the string value associated with the key if the given key exists. Otherwise, the function returns null:

var accessKey = UserDefaults.get("access_key");

results matching ""

    No results matching ""