Overview

Flexible authentication system for Vercube applications

The Auth module provides a powerful, flexible authentication system for Vercube applications. Built around a provider-based architecture, it allows you to implement various authentication strategies such as JWT, sessions, OAuth, or custom solutions.

Installation

$ pnpm add @vercube/auth

Quick Start

Create an Auth Provider

Create a custom authentication provider by extending the AuthProvider class:

src/providers/JWTAuthProvider.ts
import { AuthProvider, type AuthTypes } from '@vercube/auth';

interface User {
  id: number;
  username: string;
  roles: string[];
}

export class JWTAuthProvider extends AuthProvider<User> {
  
  public validate(
    request: Request,
    params?: AuthTypes.MiddlewareOptions,
  ): AuthTypes.ValidationResult<User> {
    const token = request.headers.get('Authorization')?.replace('Bearer ', '');
    
    if (!token) {
      return { error: 'No token provided' };
    }

    try {
      const user = this.verifyToken(token);
      
      // Check roles if specified
      if (params?.roles && params.roles.length > 0) {
        const hasRequiredRole = params.roles.some((role) =>
          user.roles.includes(role),
        );
        if (!hasRequiredRole) {
          return { error: 'Insufficient permissions' };
        }
      }
      
      return { user }; // Authentication successful
    } catch {
      return { error: 'Invalid token' };
    }
  }

  public getCurrentUser(request: Request): User | null {
    const token = request.headers.get('Authorization')?.replace('Bearer ', '');
    
    if (!token) {
      return null;
    }

    try {
      return this.verifyToken(token);
    } catch {
      return null;
    }
  }

  private verifyToken(token: string): User {
    // Your JWT verification logic here
    // This is a simplified example
    return { id: 1, username: 'john', roles: ['user'] };
  }
}

Register Provider in Container

Register your auth provider in the DI container during application setup:

src/setup.ts
import { type App } from '@vercube/core';
import { AuthProvider } from '@vercube/auth';
import { JWTAuthProvider } from './providers/JWTAuthProvider';

export function setup(app: App): void {
  app.container.bind(AuthProvider, JWTAuthProvider);
}

Protect Your Endpoints

Use the @Auth decorator to protect controller methods:

src/controllers/ProfileController.ts
import { Controller, Get } from '@vercube/core';
import { Auth, User } from '@vercube/auth';

interface User {
  id: number;
  username: string;
  roles: string[];
}

@Controller('/profile')
export class ProfileController {
  
  @Get('/')
  @Auth()
  public getProfile(@User() user: User) {
    return { profile: user };
  }
  
  @Get('/admin')
  @Auth({ roles: ['admin'] })
  public getAdminPanel(@User() user: User) {
    return { admin: true, user };
  }
}

Core Concepts

AuthProvider

The AuthProvider is an abstract class that defines the interface for authentication implementations. All authentication providers must extend this class and implement two methods:

  • validate() - Validates incoming requests and returns { user } on success or { error } with a message on failure. An error string, or null on success, is still accepted, but then @User() has to load the user again
  • getCurrentUser() - Returns the authenticated user object or null if not authenticated. @User() skips it only when @Auth() already returned { user } for the request

Decorators

The Auth module provides two decorators for easy integration with controllers:

DecoratorDescription
@Auth()Protects a method, requiring authentication before execution
@User()Injects the current authenticated user as a method parameter

Role-Based Access Control

You can restrict access based on user roles by passing options to the @Auth decorator:

@Auth({ roles: ['admin', 'moderator'] })
public adminOnly(@User() user: User) {
  // Only accessible by admins and moderators
}

The validate() method in your provider receives these options and should check if the user has the required roles.

Authentication Flow

When a request hits a protected endpoint:

  1. The @Auth decorator triggers the authentication middleware
  2. Your AuthProvider.validate() method is called with the request
  3. If validate() returns { user }, authentication succeeds and the user is kept for the rest of the request
  4. If validate() returns { error }, authentication fails with that error message
  5. The @User decorator injects the user validate() returned, so it is loaded only once per request. On a route without @Auth(), or with a different provider, it calls getCurrentUser() instead

The user is kept in RequestContext. With requestContext: false in the config nothing is kept and @User always calls getCurrentUser().

Authentication Flow

Common Patterns

import { AuthProvider, type AuthTypes } from '@vercube/auth';
import jwt from 'jsonwebtoken';

export class JWTAuthProvider extends AuthProvider<User> {
  private secret = process.env.JWT_SECRET!;
  
  public validate(
    request: Request,
    params?: AuthTypes.MiddlewareOptions,
  ): AuthTypes.ValidationResult<User> {
    const token = request.headers.get('Authorization')?.replace('Bearer ', '');
    
    if (!token) {
      return { error: 'Authorization header required' };
    }

    try {
      const decoded = jwt.verify(token, this.secret) as User;
      
      if (params?.roles?.length) {
        if (!params.roles.some(role => decoded.roles.includes(role))) {
          return { error: 'Insufficient permissions' };
        }
      }
      
      return { user: decoded };
    } catch (error) {
      if (error instanceof jwt.TokenExpiredError) {
        return { error: 'Token expired' };
      }
      return { error: 'Invalid token' };
    }
  }

  public getCurrentUser(request: Request): User | null {
    const token = request.headers.get('Authorization')?.replace('Bearer ', '');
    if (!token) return null;
    
    try {
      return jwt.verify(token, this.secret) as User;
    } catch {
      return null;
    }
  }
}