1.4.6 • Published 5 months ago

nestjs-auth-kit v1.4.6

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

šŸ›”ļø 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

EndpointMethodDescription
/auth/loginPOSTUser login
/auth/registerPOSTUser registration
/auth/googleGETGoogle OAuth login
/auth/facebookGETFacebook OAuth login
/auth/otpPOSTOTP generation
/auth/otp/verifyPOSTOTP verification
/auth/password-resetPOSTReset password via OTP
/auth/meGETGet 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

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature-branch
  3. Commit your changes: git commit -m "Added new feature"
  4. Push to the branch: git push origin feature-branch
  5. 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! šŸŽÆ

1.4.6

5 months ago

1.4.5

5 months ago

1.4.4

5 months ago

1.4.3

5 months ago

1.4.1

5 months ago

1.4.0

5 months ago

1.3.9

5 months ago

1.3.8

5 months ago

1.3.7

5 months ago

1.3.6

5 months ago

1.3.5

5 months ago

1.3.4

5 months ago

1.3.3

5 months ago

1.3.2

5 months ago

1.3.1

5 months ago

1.3.0

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.0

5 months ago