@gftdcojp/auth
Version:
Zero-config authentication package for Next.js applications with Firebase integration
195 lines (186 loc) • 6.06 kB
text/typescript
import * as react_jsx_runtime from 'react/jsx-runtime';
import { NextRequest, NextResponse } from 'next/server';
interface UserProfile {
sub: string;
email?: string;
emailVerified?: boolean;
displayName?: string;
photoURL?: string;
tenantId?: string;
[key: string]: unknown;
}
interface AuthConfig {
api: {
baseUrl: string;
loginEndpoint: string;
sessionEndpoint: string;
logoutEndpoint: string;
};
ui: {
signinPath: string;
accountPath: string;
errorPath: string;
};
redirects: {
afterLogin: string;
afterLogout: string;
onAuthError: string;
};
cookieDomain?: string;
}
interface AuthContextType {
user: UserProfile | null;
accessToken: string | null;
isLoading: boolean;
loginWithIdToken: (idToken: string, tenantId?: string) => Promise<void>;
logout: () => Promise<void>;
refreshToken: () => Promise<void>;
getAfterLoginUrl: () => string;
getAfterLogoutUrl: () => string;
getAuthErrorUrl: () => string;
}
interface AuthProviderProps {
children: React.ReactNode;
config: AuthConfig;
authServerUrl?: string;
}
interface ServerAuthConfig extends AuthConfig {
}
interface TenantContext {
tenantId: string;
tenantConfig?: {
name: string;
domain: string;
features: string[];
};
}
interface AuthResult {
success: boolean;
user?: UserProfile;
accessToken?: string;
refreshToken?: string;
error?: string;
tenantId?: string;
}
interface SessionData {
userId: string;
email?: string;
tenantId?: string;
expiresAt: number;
issuedAt: number;
}
interface LoginRequest {
idToken: string;
tenantId?: string;
provider?: string;
}
interface LoginResponse extends AuthResult {
redirectUrl?: string;
}
interface SessionCheckResponse {
authenticated: boolean;
user?: UserProfile;
tenantId?: string;
}
interface MiddlewareConfig {
publicPaths: string[];
loginPath: string;
logoutPath: string;
callbackPath: string;
tenantParam?: string;
}
declare class AuthError extends Error {
code: string;
statusCode: number;
constructor(message: string, code: string, statusCode?: number);
}
declare class TenantNotFoundError extends AuthError {
constructor(tenantId: string);
}
declare class InvalidTokenError extends AuthError {
constructor(message?: string);
}
declare const AuthProvider: ({ children, config, }: AuthProviderProps) => react_jsx_runtime.JSX.Element;
declare const useAuth: () => AuthContextType;
declare const useFirebaseAuth: (_config?: any) => {
signInWithGoogle: () => Promise<never>;
signInWithFacebook: () => Promise<never>;
};
declare function createServerAuthConfig(overrides?: Partial<ServerAuthConfig>): ServerAuthConfig;
declare function createClientAuthConfig(overrides?: Partial<AuthConfig>): AuthConfig;
declare class GFTDAuth {
private config;
constructor(config: ServerAuthConfig);
/**
* Verify Firebase ID token and create session
*/
verifyIdToken(idToken: string, tenantId?: string): Promise<UserProfile>;
/**
* Create session cookie from ID token
*/
createSessionCookie(idToken: string, tenantId?: string, expiresIn?: number): Promise<string>;
/**
* Verify session cookie
*/
verifySessionCookie(sessionCookie: string, tenantId?: string, checkRevoked?: boolean): Promise<UserProfile>;
/**
* Handle login request
*/
handleLogin(request: LoginRequest): Promise<AuthResult>;
/**
* Handle logout
*/
handleLogout(sessionCookie?: string): Promise<{
success: boolean;
}>;
/**
* Get current session
*/
getCurrentSession(sessionCookie: string, tenantId?: string): Promise<AuthResult>;
/**
* Validate tenant access
*/
validateTenant(tenantId?: string): void;
/**
* Get tenant-specific auth instance
*/
getTenantAuth(tenantId: string): any;
/**
* Verify internal service token for microservice communication
*/
verifyInternalServiceToken(token: string, expectedAudience?: string): Promise<UserProfile | null>;
}
declare function createGFTDAuth(config: ServerAuthConfig): GFTDAuth;
declare function getTenantIdFromRequest(request: Request): string | undefined;
interface MicroserviceAuthConfig {
expectedAudience: string;
requiredPermissions?: string[];
publicPaths?: string[];
}
/**
* Create middleware for microservice JWT authentication
*/
declare function createMicroserviceAuthMiddleware(config: MicroserviceAuthConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
/**
* Helper function to get authenticated user from request
*/
declare function getAuthenticatedUser(request: NextRequest): {
userId: string;
permissions: string[];
sessionId: string | undefined;
} | null;
interface AuthMiddlewareConfig {
authConfig: ServerAuthConfig;
publicPaths?: string[];
loginPath?: string;
logoutPath?: string;
callbackPath?: string;
tenantParam?: string;
}
declare function createAuthMiddleware(config: AuthMiddlewareConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
declare const _default: {
AuthProvider: ({ children, config, }: AuthProviderProps) => react_jsx_runtime.JSX.Element;
createGFTDAuth: typeof createGFTDAuth;
createAuthMiddleware: typeof createAuthMiddleware;
};
export { type AuthConfig, type AuthContextType, AuthError, type AuthMiddlewareConfig, AuthProvider, type AuthProviderProps, type AuthResult, GFTDAuth, InvalidTokenError, type LoginRequest, type LoginResponse, type MiddlewareConfig, type ServerAuthConfig, type SessionCheckResponse, type SessionData, type TenantContext, TenantNotFoundError, type UserProfile, createAuthMiddleware, createClientAuthConfig, createGFTDAuth, createMicroserviceAuthMiddleware, createServerAuthConfig, _default as default, getAuthenticatedUser, getTenantIdFromRequest, useAuth, useFirebaseAuth };