nestjs-auth-kit v1.4.6
š”ļø NestJS Auth Kit - NOT READY
A modular authentication kit for NestJS providing JWT authentication, OAuth2 social login (Google, Facebook, etc.), OTP verification, and password reset functionality.
š Features
- ā JWT-based authentication (Access & Refresh tokens)
- ā OAuth2 social login (Google, Facebook, etc.)
- ā OTP-based authentication (Email or SMS-based)
- ā Password reset via OTP
- ā Role-based access control (RBAC)
- ā Modular and scalable architecture
- ā Custom decorators for roles and authentication
- ā Integration with NestJS Guards & Interceptors
- ā Customizable authentication strategies
- ā Configurable environment variables
š¦ Installation
npm install nestjs-auth-kit
or with PNPM:
pnpm install nestjs-auth-kit
or with Yarn:
yarn add nestjs-auth-kit
š ļø Setup & Usage
1ļøā£ Import the AuthModule
in app.module.ts
@Module({
imports: [
AuthModule.register({
jwtSecret: process.env.JWT_SECRET,
jwtExpiration: process.env.JWT_EXPIRATION || '1h',
socialAuth: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
},
facebook: {
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
},
},
}),
],
})
export class AppModule {}
2ļøā£ Configure .env
Variables
Make sure your environment variables are correctly set:
JWT_SECRET=your_jwt_secret
JWT_EXPIRATION=1h
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
FACEBOOK_CLIENT_ID=your_facebook_client_id
FACEBOOK_CLIENT_SECRET=your_facebook_client_secret
OTP_EXPIRATION=300 # OTP expiry time in seconds
3ļøā£ Available Authentication Methods
š¹ JWT Authentication
Login and get a JWT token:
import { AuthService } from 'nestjs-auth-kit';
constructor(private authService: AuthService) {}
async login() {
return this.authService.login({ email: 'user@example.com', password: 'password' });
}
š¹ OAuth2 Social Login
Authenticate using Google:
import { SocialAuthService } from 'nestjs-auth-kit';
constructor(private socialAuthService: SocialAuthService) {}
async googleLogin(token: string) {
return this.socialAuthService.validateGoogleUser(token);
}
š¹ OTP-based Authentication
Generate an OTP:
import { OtpService } from 'nestjs-auth-kit';
constructor(private otpService: OtpService) {}
async sendOtp(email: string) {
return this.otpService.generateOtp(email);
}
Verify OTP:
async verifyOtp(email: string, otp: string) {
return this.otpService.verifyOtp(email, otp);
}
š¹ Password Reset via OTP
import { ForgotPasswordService } from 'nestjs-auth-kit';
constructor(private forgotPasswordService: ForgotPasswordService) {}
async resetPassword(email: string, otp: string, newPassword: string) {
return this.forgotPasswordService.resetPassword(email, otp, newPassword);
}
š Role-Based Access Control (RBAC)
Use the @Roles()
decorator to protect routes based on roles.
import { Controller, Get } from '@nestjs/common';
import { Roles } from 'nestjs-auth-kit';
@Controller('admin')
export class AdminController {
@Get()
@Roles('admin')
getAdminData() {
return { message: 'Admin data' };
}
}
š API Endpoints
Endpoint | Method | Description |
---|---|---|
/auth/login | POST | User login |
/auth/register | POST | User registration |
/auth/google | GET | Google OAuth login |
/auth/facebook | GET | Facebook OAuth login |
/auth/otp | POST | OTP generation |
/auth/otp/verify | POST | OTP verification |
/auth/password-reset | POST | Reset password via OTP |
/auth/me | GET | Get authenticated user info |
āļø Configuration Options
You can configure authentication options using AuthModule.register()
.
AuthModule.register({
jwtSecret: process.env.JWT_SECRET,
jwtExpiration: '1h',
socialAuth: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
},
facebook: {
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
},
},
});
šļø Folder Structure
nestjs-auth-kit/
āāā src/
ā āāā auth.module.ts
ā āāā auth.service.ts
ā āāā auth.controller.ts
ā āāā strategies/
ā ā āāā jwt.strategy.ts
ā ā āāā google.strategy.ts
ā ā āāā facebook.strategy.ts
ā āāā guards/
ā ā āāā jwt-auth.guard.ts
ā āāā decorators/
ā ā āāā roles.decorator.ts
ā āāā dto/
ā ā āāā login.dto.ts
ā ā āāā register.dto.ts
ā āāā interfaces/
ā ā āāā auth-options.interface.ts
āāā package.json
āāā index.ts
š License
MIT License Ā© 2025 Galatex Solutions
š¤ Contribution Guidelines
- Fork the repository.
- Create a feature branch:
git checkout -b feature-branch
- Commit your changes:
git commit -m "Added new feature"
- Push to the branch:
git push origin feature-branch
- Open a pull request.
š¬ Contact & Support
For issues, questions, or suggestions, feel free to open an issue on GitHub.
š NestJS Auth Kit is designed to simplify authentication in NestJS applications. Get started today! šÆ
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago