Zum Hauptinhalt springen

getBrowserToken

This function returns a GUID (Globally Unique Identifier) that persistently identifies the current browser. The token remains the same even when you close all browser tabs and restart brixxbox. It only changes if the browser's local storage for the domain is cleared.

Syntax

app.getBrowserToken()

Parameters

This function takes no parameters.

Return Value

Returns a String containing a GUID (128-bit identifier in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

How It Works

  • The function generates a random GUID the first time it's called in a browser
  • The GUID is stored in the browser's localStorage
  • Subsequent calls retrieve the stored GUID rather than generating a new one
  • The GUID persists until localStorage is cleared (e.g., by clearing browser data)

Examples

Example 1: Basic usage to get the browser token:

// Get the browser token
const myToken = app.getBrowserToken();
console.log("Browser token: " + myToken);

Example 2: Using the token to maintain a shopping cart across page refreshes:

// Get the browser token
const cartId = app.getBrowserToken();

// Use the token as a key for cart identification
app.sqlRead("GetCartItems", {
browserToken: cartId
});

Example 3: Using the token for cross-tab communication:

// Get the browser token as a unique identifier
const browserToken = app.getBrowserToken();

// Use it to store user preferences across tabs
app.sqlWrite("SaveUserPreferences", {
browserToken: browserToken,
theme: "dark",
fontSize: "large"
});

Use Cases

  • Shopping Carts: Identifying a user's shopping cart across page refreshes
  • Anonymous Session Management: Tracking user sessions without requiring login
  • Cross-Tab State Management: Maintaining consistent state across multiple browser tabs
  • Analytics: Tracking anonymous browser usage patterns
  • Temporary Authentication: Short-term user authentication for public-facing applications

Notes

  • This token is browser-specific, not user-specific (multiple users on the same device using the same browser would share the token)
  • The token does not provide strong security and should not replace proper authentication for sensitive operations
  • The token is specific to the domain (brixxbox instance) where it was generated
  • For session-specific tokens that change with each browser session, use other functions