Career Edge Backend Auth Library
raw JSON → 2.0.3 verified Sat Apr 25 auth: no javascript
A modular authentication library for Node.js backends integrating AWS Cognito JWT validation and MongoDB connectivity. Version 2.0.3 provides a registerable module with synchronous configuration. Features token verification, database connection management, and TypeScript-first design. Differentiates through opinionated module registration pattern and direct Cognito integration.
Common errors
error Cannot find module 'career-edge-backend-auth-library' ↓
cause Package not installed or import path incorrect.
fix
Run npm install career-edge-backend-auth-library and ensure the import statement is correct.
error TypeError: ValidateTokenModule.register is not a function ↓
cause Likely using a default import instead of named import, or the module has changed its API.
fix
Use named import: import { ValidateTokenModule } from 'career-edge-backend-auth-library'. If problem persists, check version >=2.0.0.
error MongooseError: Mongoose connection error: missing database name ↓
cause DATABASE_CONNECTION_STRING does not contain the database name, and DATABASE_NAME is empty.
fix
Provide a valid MongoDB URI that includes the database name (e.g., mongodb://localhost:27017/mydb).
error JsonWebTokenError: invalid signature ↓
cause JWT token does not match the JWK from Cognito; possibly wrong JWK URL or token expired.
fix
Verify AWS_COGNITO_JWK_URL is correct and the token is still valid.
Warnings
breaking Configuration object changed between versions: removed optional fields and added DATABASE_CONNECTION_STRING and DATABASE_NAME. Existing code using older config will fail. ↓
fix Update the register() call to include all required fields as shown in the README.
breaking The library is ESM-only since v2. CommonJS require() statements will throw an error. ↓
fix Use import statements instead of require. Ensure your project supports ESM (e.g., type: module in package.json).
gotcha AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are required even if using IAM roles; passing empty strings may cause runtime errors. ↓
fix Provide valid AWS credentials or use AWS SDK credential chain on EC2/ECS.
gotcha DATABASE_CONNECTION_STRING must include the database name in the URI (e.g., mongodb://host/dbname) because DATABASE_NAME option is additional but may be ignored if present in the URI. ↓
fix Ensure the MongoDB URI includes the database name. The separate DATABASE_NAME is optional and may override.
deprecated AwsSdk v2 is used internally. AWS SDK v3 is recommended; future versions will migrate. ↓
fix No immediate action needed, but plan for migration when library updates.
Install
npm install test-backend-schema-library yarn add test-backend-schema-library pnpm add test-backend-schema-library Imports
- ValidateTokenModule wrong
import ValidateTokenModule from 'career-edge-backend-auth-library'correctimport { ValidateTokenModule } from 'career-edge-backend-auth-library' - TokenService wrong
const { TokenService } = require('career-edge-backend-auth-library')correctimport { TokenService } from 'career-edge-backend-auth-library' - TokenGuard wrong
import { TokenGuard } from 'career-edge-backend-auth-library/guards'correctimport { TokenGuard } from 'career-edge-backend-auth-library'
Quickstart
import { Module } from '@nestjs/common';
import { ValidateTokenModule } from 'career-edge-backend-auth-library';
@Module({
imports: [
ValidateTokenModule.register({
AWS_COGNITO_JWK_URL: process.env.COGNITO_JWK_URL ?? '',
AWS_API_VERSION: process.env.API_VERSION ?? '2016-04-18',
AWS_REGION: process.env.AWS_REGION ?? '',
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID ?? '',
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY ?? '',
DATABASE_CONNECTION_STRING: process.env.MONGO_URI ?? '',
DATABASE_NAME: process.env.DATABASE_NAME ?? '',
}),
],
providers: [],
controllers: []
})
export class AppModule {}
// In a service:
import { Injectable } from '@nestjs/common';
import { TokenService } from 'career-edge-backend-auth-library';
@Injectable()
export class ExampleService {
constructor(private readonly tokenService: TokenService) {}
async validate(token: string) {
return this.tokenService.validateToken(token);
}
}