authrix
Version:
Lightweight, flexible authentication library for Node.js and TypeScript.
132 lines (128 loc) • 4.19 kB
TypeScript
import { S as SessionUser } from './session-C-ESV9uK.js';
export { L as LogoutResult } from './session-C-ESV9uK.js';
export { a as authConfig, g as getAuthrixStatus, i as initAuth, b as isAuthrixInitialized } from './index-2c4aN9k6.js';
export { A as AuthDbAdapter, a as AuthUser } from './db-BIgxMgj8.js';
export { g as generateTwoFactorCode, b as getUserTwoFactorCodes, i as initiateEmailVerification, a as initiateSMSVerification, v as verifyTwoFactorCode } from './twoFactor-Dnjt2e0v.js';
interface PublicUser {
id: string;
email: string;
username?: string;
firstName?: string;
lastName?: string;
fullName?: string;
profilePicture?: string;
emailVerified?: boolean;
createdAt?: Date;
lastLoginAt?: Date;
}
interface CookieOptions {
httpOnly?: boolean;
secure?: boolean;
maxAge?: number;
sameSite?: "strict" | "lax" | "none";
path?: string;
expires?: Date;
}
interface AuthSuccessResult {
success: true;
user: PublicUser;
token: string;
cookieOptions: CookieOptions;
isNewUser?: boolean;
requiresEmailVerification?: boolean;
}
interface AuthErrorResult {
success: false;
error: {
message: string;
};
}
type AuthResponse = AuthSuccessResult | AuthErrorResult;
interface UniversalLogoutResult {
success: boolean;
message: string;
cookiesToClear: Array<{
name: string;
options: {
httpOnly: boolean;
secure: boolean;
sameSite: "lax" | "strict" | "none";
path: string;
expires: Date;
domain?: string;
};
}>;
redirectUrl?: string;
}
/**
* Framework-agnostic signup function
* Returns user data, token, and cookie options for manual handling
*/
declare function signupUniversal(email: string, password: string): Promise<AuthResponse>;
/**
* Framework-agnostic signin function
* Returns user data, token, and cookie options for manual handling
*/
declare function signinUniversal(email: string, password: string): Promise<AuthResponse>;
/**
* Framework-agnostic logout function
* Returns cookie information for manual clearing
*/
declare function logoutUniversal(): UniversalLogoutResult;
/**
* Get current user from token (framework-agnostic)
*/
declare function getCurrentUserUniversal(token: string | null): Promise<SessionUser | null>;
/**
* Check if token is valid (framework-agnostic)
*/
declare function isTokenValidUniversal(token: string | null): Promise<boolean>;
/**
* Utility to create a standard cookie string
*/
declare function createCookieString(name: string, value: string, options: CookieOptions): string;
/**
* Utility to parse cookies from a cookie header string
*/
declare function parseCookies(cookieHeader: string): Record<string, string>;
/**
* Get auth token from parsed cookies
*/
declare function getAuthTokenFromCookies(cookies: Record<string, string>): string | null;
/**
* Validate authentication for any framework
* Returns both validation result and user data
*/
declare function validateAuth(token: string | null): Promise<{
isValid: boolean;
user: null;
error: string;
} | {
isValid: boolean;
user: SessionUser;
error: null;
}>;
/**
* Create authentication headers for API requests
*/
declare function createAuthHeaders(token: string): Record<string, string>;
/**
* Utility for framework-specific implementations to handle errors consistently
*/
declare function createAuthError(message: string, status?: number): {
success: boolean;
error: {
message: string;
};
status: number;
};
/**
* Utility for framework-specific implementations to handle success responses consistently
*/
declare function createAuthSuccess<T>(data: T, status?: number): {
success: boolean;
data: T;
status: number;
};
type AuthResult = AuthResponse;
export { type AuthResponse, type AuthResult, type CookieOptions, type UniversalLogoutResult, createAuthError, createAuthHeaders, createAuthSuccess, createCookieString, getAuthTokenFromCookies, getCurrentUserUniversal, isTokenValidUniversal, logoutUniversal, parseCookies, signinUniversal, signupUniversal, validateAuth };