JavaScript

The JavaScript library to interact with the FluxAuth API for authentication and retrieving secure variables.

Installation

Download the flux.js file and place it in your project directory.

Import

import { Flux } from './flux.js';

Initialize

Create a new instance of the Flux class with your application ID:

const flux = new Flux('your-application-id');

Authenticate

To authenticate a user, call the authenticate method with the user’s license key and an optional hardware ID:

const license = 'your-license-key';
const hwid = 'your-hardware-id';

try {
  await flux.authenticate(license, hwid);
} catch (error) {
  console.error('Authentication failed:', error.message);
}

Get Variable

Retrieve a secure variable by calling the getVariable method with the variable’s name and its expected type:

const variableName = 'your-variable-name';

try {
  const variableValue = await flux.getVariable(variableName, String);
  console.log('Variable value:', variableValue);
} catch (error) {
  console.error('Failed to get variable:', error.message);
}

Get Field

Access a field from the authentication response by calling the getField method with the field’s name and expected type:

const fieldName = 'your-field-name';

try {
  const fieldValue = flux.getField(fieldName, Number);
  console.log('Field value:', fieldValue);
} catch (error) {
  console.error('Failed to get field:', error.message);
}

Download Variable

Download a secure variable as a file by calling the downloadVariable method with the variable’s name:

import fs from 'fs/promises';

const variableName = 'your-variable-name';

try {
  const data = await flux.downloadVariable(variableName);
  await fs.writeFile('output-file-name', data);
} catch (error) {
  console.error('Failed to download variable:', error.message);
}

Example Usage

This example demonstrates the process of initializing, authenticating, and retrieving variables using the Flux SDK:

import { Flux } from './flux.js';
import fs from 'fs/promises';
import readline from 'readline';

const flux = new Flux('your-application-id');

// Get an unauthenticated variable
console.log(await flux.getVariable('welcome-str', String));

const read = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const license = await new Promise((resolve) => {
  read.question('Enter license:', resolve);
});

try {
const auth = await flux.authenticate(license, 'test');
} catch (error) {
  console.error('Authentication failed:', error.message);
  process.exit(1);
}

console.log('Expires:', flux.getField('expiresAt', Number));
console.log('Authenticated Variable:', await flux.getVariable('authenticated', String));
const data = await flux.downloadVariable('file');
await fs.writeFile('file.bin', data);

read.close();