@ssktechnologies/awsforge
Version:
Enterprise-grade AWS Cognito authentication toolkit for seamless user management, registration, login, and password recovery with JWT token handling
109 lines (108 loc) • 4.43 kB
JavaScript
// src/index.ts
// Main entry point with global import solution
import { CognitoService, CognitoConfigs } from './services/cognito.js';
import { PackageConfig } from './config/packageConfig.js';
import { extractTokens } from "./utils/tokenManager.js";
// Re-export all types
export * from './types/index.js';
// Traditional exports (keep for backward compatibility)
export { CognitoService, CognitoConfigs, PackageConfig, extractTokens };
// NEW: Global factory function for simplified usage
export default function createCognito(config) {
// Create a single, reusable service instance
const service = new CognitoService(config);
return {
// Service instance
service,
// Configuration presets
configs: CognitoConfigs,
// Utility functions
utils: {
extractTokens,
},
// Quick access methods (delegates to service)
async register(data) {
return service.registerUser(data);
},
async login(data) {
return service.loginUser(data);
},
async confirmRegistration(data) {
return service.confirmUserRegistration(data);
},
async forgotPassword(data) {
return service.initiateForgotPassword(data);
},
async verifyToken(token) {
return service.verifyToken(token);
},
async verifyAccessToken(token) {
return service.verifyAccessToken(token);
},
async verifyIdToken(token) {
return service.verifyIdToken(token);
},
async getUserFromToken(accessToken) {
return service.getUserFromToken(accessToken);
},
async refreshTokens(refreshToken) {
return service.refreshTokens(refreshToken);
},
async revokeToken(token) {
return service.revokeToken(token);
},
async adminCreateUser(data) {
return service.adminCreateUser(data);
},
async adminDeleteUser(params) {
return service.adminDeleteUser(params);
},
async respondToNewPasswordChallenge(data) {
return service.respondToNewPasswordChallenge(data);
},
};
}
// Alternative: Class-based global API
export class AWSForge {
cognitoService;
constructor(config) {
this.cognitoService = new CognitoService(config);
}
// Cognito methods
get cognito() {
return {
register: this.cognitoService.registerUser.bind(this.cognitoService),
login: this.cognitoService.loginUser.bind(this.cognitoService),
confirmRegistration: this.cognitoService.confirmUserRegistration.bind(this.cognitoService),
forgotPassword: this.cognitoService.initiateForgotPassword.bind(this.cognitoService),
confirmForgotPassword: this.cognitoService.confirmForgotPassword.bind(this.cognitoService),
changePassword: this.cognitoService.changePassword.bind(this.cognitoService),
deleteUser: this.cognitoService.deleteUser.bind(this.cognitoService),
resendConfirmationCode: this.cognitoService.resendConfirmationCode.bind(this.cognitoService),
adminCreateUser: this.cognitoService.adminCreateUser.bind(this.cognitoService),
adminDeleteUser: this.cognitoService.adminDeleteUser.bind(this.cognitoService),
respondToNewPasswordChallenge: this.cognitoService.respondToNewPasswordChallenge.bind(this.cognitoService),
// Token methods
verifyToken: this.cognitoService.verifyToken.bind(this.cognitoService),
verifyAccessToken: this.cognitoService.verifyAccessToken.bind(this.cognitoService),
verifyIdToken: this.cognitoService.verifyIdToken.bind(this.cognitoService),
getUserFromToken: this.cognitoService.getUserFromToken.bind(this.cognitoService),
refreshTokens: this.cognitoService.refreshTokens.bind(this.cognitoService),
revokeToken: this.cognitoService.revokeToken.bind(this.cognitoService),
};
}
// Utils
get utils() {
return {
extractTokens,
};
}
// Configs
static get configs() {
return CognitoConfigs;
}
}
// Export as named export too
export { createCognito };
// Re-export the service for those who prefer the original approach
export { CognitoService as Cognito };