function authenticate
authenticate(
baseUrl: string,
options: AuthOptions
): Promise<AuthResult | null>

Authenticates with a TP-Link router and returns session context.

Performs the following steps:

  1. Fetches router info from the login page
  2. Retrieves RSA public key for encryption
  3. Creates encryption instance with AES + RSA
  4. Checks if already logged in
  5. Performs login to get session ID
  6. Retrieves security token for API calls

Examples

Basic authentication

import { authenticate } from "./authenticate.ts";

const auth = await authenticate("http://192.168.1.1", {
  password: "admin",
});

if (auth) {
  // Use auth.encryption, auth.sessionId, auth.tokenId for API calls
}

Authentication with custom username

import { authenticate } from "./authenticate.ts";

const auth = await authenticate("http://192.168.1.1", {
  username: "user",
  password: "secret",
  forceLogin: false,
});

Authentication against a JSON-dialect router

import { authenticate } from "./authenticate.ts";
import { gdprJson } from "./dialect/mod.ts";

const auth = await authenticate("http://192.168.254.1", {
  password: "secret",
  dialect: gdprJson,
});

Parameters

baseUrl: string

Base URL of the router (e.g., "http://192.168.1.1")

options: AuthOptions

Authentication options including password

Return Type

Promise<AuthResult | null>

Authentication result with encryption context, or null if login failed