@domusjs/auth
v0.2.2
Published
The `@domusjs/auth` module provides a modular, extensible foundation for handling user authentication in modern backend applications.
Downloads
27
Readme
@domusjs/auth
The @domusjs/auth module provides a modular, extensible foundation for handling user authentication in modern backend applications.
📘 Documentation: @domusjs/auth Docs
Features
- 🧩 Pluggable strategies for authentication (e.g., credentials, Google, GitHub, etc.)
- 🔐 Unified JWT generation and validation
- ✨ Built with extensibility and type-safety in mind
Installation
npm install @domusjs/authUsage
1. Define your own AuthResult
export interface UserAuthResult {
userId: string;
email: string;
}2. Implement a custom strategy
// password-auth.strategy.ts
import { AuthStrategy } from '@domusjs/auth';
interface PasswordAuthPayload {
email: string;
password: string;
}
interface PasswordAuthResult {
userId: string;
email: string;
}
export class PasswordAuthStrategy implements AuthStrategy<PasswordAuthPayload, PasswordAuthResult> {
async login(payload: PasswordAuthPayload): Promise<PasswordAuthResult> {
// Validate user, check password, etc.
return { userId: 'abc123', email: payload.email };
}
}3. Register Auth Module with custom strategies
import { registerAuthModule, AuthService } from '@domusjs/auth';
import { PasswordAuthStrategy } from './password-auth.strategy';
const jwtOptions = {
secret: 'my_jwt_secret',
expiresIn: '1h',
};
registerAuthModule(
[
{
strategy: PasswordAuthStrategy,
instance: new PasswordAuthStrategy(),
},
],
jwtOptions
);4. Use AuthService
import { container } from 'tsyringe';
import { AuthService } from '@domusjs/auth';
import { PasswordAuthStrategy } from './password-auth.strategy';
const authService = container.resolve<AuthService>('AuthService');
const authResult = await authService.loginWith(PasswordAuthStrategy, {
email: '[email protected]',
password: '1234',
});🔗 Learn More
For advanced patterns, dependency injection, and more, check out the full documentation:
