UNPKG

@wristband/express-auth

Version:

SDK for integrating your ExpressJS application with Wristband. Handles user authentication, session management, and token management.

100 lines (99 loc) 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WristbandAuthImpl = void 0; const auth_service_1 = require("./auth-service"); /** * WristbandAuth is a utility class providing methods for seamless interaction with the Wristband authentication service. * @implements {WristbandAuth} */ class WristbandAuthImpl { /** * Creates an instance of WristbandAuth. * * @param {AuthConfig} authConfig The configuration for Wristband authentication. */ constructor(authConfig) { this.authService = new auth_service_1.AuthService(authConfig); } /** * Internal method to eagerly fetch and cache all auto-configurable values from the * Wristband SDK Configuration Endpoint. This triggers the API call and caches results, * allowing any validation errors to be thrown early (fail-fast). * * @private * @returns {Promise<void>} A Promise that resolves when configuration is preloaded. * @throws {WristbandError} When auto-configuration endpoint is unreachable or returns invalid data. * @throws {TypeError} When required configuration values cannot be resolved. */ async discover() { await this.authService.preloadConfig(); } /** * Static factory method to create a WristbandAuth instance with eager auto-configuration. * * This method immediately fetches and resolves all auto-configuration values from the * Wristband SDK Configuration Endpoint during initialization. Unlike the standard constructor, * this ensures all configuration is loaded and validated upfront, allowing the application to * fail fast if auto-configuration is unavailable. * * @static * @param {AuthConfig} authConfig - Configuration for Wristband authentication. Required fields: * clientId, clientSecret, wristbandApplicationVanityDomain. * @returns {Promise<WristbandAuthImpl>} A Promise that resolves to an instance of WristbandAuthImpl * with all configuration values already resolved and validated. * @throws {WristbandError} When auto-configuration endpoint is unreachable or returns invalid data. * @throws {TypeError} When required configuration values cannot be resolved. * * @example * ```typescript * // Create with eager auto-configuration * const wristbandAuth = await WristbandAuthImpl.createWithDiscovery({ * clientId: "your-client-id", * clientSecret: "your-secret", * wristbandApplicationVanityDomain: "auth.yourapp.io" * }); * // All configuration is now resolved and ready to use * ``` */ static async createWithDiscovery(authConfig) { const auth = new WristbandAuthImpl(authConfig); await auth.discover(); return auth; } /** * @inheritdoc * @see {@link WristbandAuth.login} */ login(req, res, config) { return this.authService.login(req, res, config); } /** * @inheritdoc * @see {@link WristbandAuth.callback} */ callback(req, res) { return this.authService.callback(req, res); } /** * @inheritdoc * @see {@link WristbandAuth.logout} */ logout(req, res, config) { return this.authService.logout(req, res, config); } /** * @inheritdoc * @see {@link WristbandAuth.refreshTokenIfExpired} */ refreshTokenIfExpired(refreshToken, expiresAt) { return this.authService.refreshTokenIfExpired(refreshToken, expiresAt); } /** * @inheritdoc * @see {@link WristbandAuth.createAuthMiddleware} */ createAuthMiddleware(config) { return this.authService.createAuthMiddleware(config); } } exports.WristbandAuthImpl = WristbandAuthImpl;