[data-reveal]{opacity:1!important;transform:none!important}
Auth

Token Handling

The Interface Auth provides functions for generating and validating authentication tokens. These functions serve as the foundation for the authentication system.

Token generation

SignRaw

The SignRaw function signs arbitrary data and returns a token string.

import { SignRaw } from "@antelopejs/interface-auth";

const token = await SignRaw(
  { userId: 123, role: "admin" },
  { expiresIn: "1h" },
);

console.log(token); // Signed token string

The function accepts two arguments:

ArgumentTypeDescription
datastring | Buffer | objectThe data to sign
optionsSignOptions (optional)Signing configuration options

SignRaw returns a Promise<string> that resolves to the signed token.

SignOptions

The SignOptions interface configures token generation.

PropertyTypeDescription
expiresInstring | numberToken expiration time as seconds or a timespan string (e.g., "1h", "2d")
notBeforestring | numberDuration before which the token is not valid

SignServerResponse

The SignServerResponse function signs data and attaches the resulting token as a cookie on the HTTP response.

import { SignServerResponse } from "@antelopejs/interface-auth";
import type { ServerResponse } from "node:http";

async function login(res: ServerResponse) {
  const userData = { userId: 123, role: "user" };

  await SignServerResponse(
    res,
    userData,
    { expiresIn: "1h" },
    { httpOnly: true, secure: true, path: "/" },
  );
}

The function accepts four arguments:

ArgumentTypeDescription
resServerResponseThe HTTP response object
datastring | Buffer | objectThe data to sign
signOptionsSignOptions (optional)Signing configuration options
cookieOptionsCookieOptions (optional)Cookie configuration options

The cookie is set with the name ANTELOPEJS_AUTH and the signed token as its value. The function returns a Promise<ServerResponse> that resolves to the same response object once the cookie header has been set.

CookieOptions

The CookieOptions interface configures the authentication cookie.

PropertyTypeDescription
maxAgenumberMaximum age in milliseconds
signedbooleanWhether the cookie should be signed
expiresDateSpecific date when the cookie expires
httpOnlybooleanPrevents client-side JavaScript from accessing the cookie
pathstringURL path for which the cookie is valid
domainstringDomain for which the cookie is valid
securebooleanOnly sends the cookie over HTTPS

Token validation

ValidateRaw

The ValidateRaw function verifies a token and returns the data contained within it.

import { ValidateRaw } from "@antelopejs/interface-auth";

try {
  const userData = await ValidateRaw<{ userId: number; role: string }>(token);
  console.log(userData); // { userId: 123, role: "admin" }
} catch (error) {
  console.error("Invalid token:", error.message);
}

The function accepts two arguments:

ArgumentTypeDescription
tokenstring (optional)The signed token to verify
optionsVerifyOptions (optional)Verification configuration

VerifyOptions

The VerifyOptions interface configures token validation.

PropertyTypeDescription
ignoreExpirationbooleanIf true, expired tokens are still considered valid
ignoreNotBeforebooleanIf true, tokens not yet valid are accepted
maxAgestring | numberMaximum allowed age of the token

Example with verify options

import { ValidateRaw } from "@antelopejs/interface-auth";

// Accept tokens up to 30 minutes old, ignoring the not-before field
const userData = await ValidateRaw(token, {
  maxAge: "30m",
  ignoreNotBefore: true,
});