0.1.32 • Published 4 months ago

@ackplus/nest-auth v0.1.32

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

Nest Auth

A comprehensive authentication module for NestJS applications.

Table of Contents

Installation

yarn add @ackplus/nest-auth

Quick Start

import { NestAuthModule } from '@ackplus/nest-auth';

@Module({
  imports: [
    NestAuthModule.forRoot({
      jwt: {
        secret: 'your-secret-key',
      },
      emailAuth: {
        enabled: true,
      },
      // ... other options
    }),
  ],
})
export class AppModule {}

Configuration

Database Configuration

The module supports both PostgreSQL and SQLite databases. Configure your database connection using the DATABASE_URL environment variable:

# For PostgreSQL
DATABASE_URL=postgresql://user:password@localhost:5432/dbname

# For SQLite
DATABASE_URL=:memory:  # For in-memory database
DATABASE_URL=./database.sqlite  # For file-based database

Environment Variables

VariableDescriptionDefault
DATABASE_URLDatabase connection URL:memory:
NODE_ENVApplication environmentdevelopment

Module Options

interface AuthModuleOptions {
  jwt: {
    secret: string;
    expiresIn?: string;
  };
  emailAuth?: {
    enabled: boolean;
    // ... other email auth options
  };
  phoneAuth?: {
    enabled: boolean;
    // ... other phone auth options
  };
  // ... other options
}

Database Setup

PostgreSQL

  1. Create a new database:
CREATE DATABASE your_database;
  1. Run migrations:
yarn typeorm migration:run -d src/typeorm.config.ts

SQLite

No setup required. The database will be created automatically.

Testing

Local Development

  1. For PostgreSQL:
DATABASE_URL=postgresql://user:password@localhost:5432/testdb yarn test
  1. For SQLite:
DATABASE_URL=:memory: yarn test

CI/CD (GitHub Actions)

The tests run automatically in GitHub Actions using SQLite for faster execution. No additional setup required.

Authentication Providers

Email Authentication

@Post('login')
async login(@Body() loginDto: LoginRequestDto) {
  return this.authService.login({
    providerId: 'email',
    credentials: {
      email: loginDto.email,
      password: loginDto.password,
    },
  });
}

Phone Authentication

@Post('login')
async login(@Body() loginDto: LoginRequestDto) {
  return this.authService.login({
    providerId: 'phone',
    credentials: {
      phone: loginDto.phone,
      password: loginDto.password,
    },
  });
}

API Documentation

Authentication Endpoints

EndpointMethodDescription
/auth/loginPOSTLogin with credentials
/auth/registerPOSTRegister new user
/auth/refreshPOSTRefresh access token
/auth/logoutPOSTLogout user

Request/Response Examples

Login Request

{
  "providerId": "email",
  "credentials": {
    "email": "user@example.com",
    "password": "password123"
  }
}

Login Response

{
  "accessToken": "eyJhbGciOiJIUzI1NiIs...",
  "refreshToken": "eyJhbGciOiJIUzI1NiIs...",
  "isRequiresMfa": false
}

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

  1. Clone the repository
  2. Install dependencies:
yarn install
  1. Set up the database:
# For PostgreSQL
DATABASE_URL=postgresql://user:password@localhost:5432/testdb

# For SQLite
DATABASE_URL=:memory:
  1. Run tests:
yarn test

Testing Guidelines

  • Write tests for all new features
  • Ensure tests pass in both PostgreSQL and SQLite
  • Follow the existing test patterns
  • Use appropriate test fixtures

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.1.32

4 months ago

0.1.31

4 months ago

0.1.30

4 months ago

0.1.29

4 months ago

0.1.28

4 months ago

0.1.27

4 months ago

0.1.26

4 months ago

0.1.25

4 months ago

0.1.24

4 months ago

0.1.23

4 months ago

0.1.22

4 months ago

0.1.21

4 months ago

0.1.20

4 months ago

0.1.19

4 months ago

0.1.18

4 months ago

0.1.17

5 months ago

0.1.16

5 months ago

0.1.15

5 months ago

0.1.14

5 months ago

0.1.13

5 months ago

0.1.12

5 months ago

0.1.10

5 months ago

0.1.9

5 months ago

0.1.8

5 months ago

0.1.7

5 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.4

5 months ago

0.1.3

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago

0.0.51

5 months ago

0.0.50

5 months ago

0.0.49

5 months ago

0.0.48

5 months ago

0.0.47

5 months ago

0.0.46

5 months ago

0.0.45

5 months ago

0.0.43

5 months ago

0.0.42

5 months ago

0.0.41

5 months ago

0.0.40

5 months ago

0.0.39

5 months ago

0.0.38

5 months ago

0.0.37

5 months ago

0.0.36

5 months ago

0.0.35

5 months ago

0.0.34

5 months ago

0.0.33

5 months ago

0.0.32

5 months ago

0.0.30

5 months ago

0.0.29

5 months ago

0.0.28

5 months ago

0.0.27

5 months ago

0.0.26

5 months ago

0.0.25

5 months ago

0.0.24

5 months ago

0.0.23

5 months ago