UNPKG

@mridang/nestjs-auth

Version:

A comprehensive Auth.js integration for NestJS applications with TypeScript support, framework-agnostic HTTP adapters, and role-based access control

104 lines (103 loc) 3.21 kB
export declare const IS_PUBLIC_KEY: unique symbol; export declare const REQUIRED_ROLES_KEY: unique symbol; /** * Marks a route as publicly accessible, bypassing authentication requirements. * * Routes decorated with `@Public()` will not require a valid session and will * be accessible to unauthenticated users. This decorator should be used sparingly * and only for endpoints that genuinely need to be public (e.g., login, health checks). * * @example * ```ts * @Controller('auth') * export class AuthController { * @Get('login') * @Public() * getLoginPage() { * return { message: 'Login page' }; * } * } * ``` * * @returns A method decorator that sets the 'isPublic' metadata to true */ export declare const Public: () => ClassDecorator & MethodDecorator; /** * Restricts route access to users with specific roles. * * Users must have at least one of the specified roles to access the decorated route. * The roles are checked against the user's roles array in their session. If the user * has no roles or none of the required roles, access will be denied. * * @param roles - One or more role names that are allowed to access this route * * @example * ```ts * @Controller('admin') * export class AdminController { * @Get('users') * @RequireRoles('admin', 'moderator') * getUsers() { * return { users: [] }; * } * * @Delete('user/:id') * @RequireRoles('admin') * deleteUser(@Param('id') id: string) { * // Only admin role can delete users * } * } * ``` * * @returns A method decorator that sets the 'requiredRoles' metadata */ export declare const RequireRoles: (...roles: readonly string[]) => MethodDecorator; /** * Injects the current Auth.js session into the route handler parameter. * * This decorator extracts the session object from the request and injects it * as a parameter into your route handler. The session contains user information, * expiration data, and other Auth.js session properties. * * Returns `null` if no valid session exists (user is not authenticated). * Access the user data via `session.user` and session metadata via other properties. * * @param data - Optional data parameter (not used in this implementation) * @param ctx - The execution context provided by NestJS * * @example * ```ts * @Controller('profile') * export class ProfileController { * @Get('me') * getCurrentUser(@AuthSession() session: Session | null) { * if (!session) { * throw new UnauthorizedException('Not authenticated'); * } * * return { * user: session.user, * expires: session.expires, * // Access other session properties as needed * }; * } * * @Get('dashboard') * getDashboard(@AuthSession() session: Session | null) { * const user = session?.user; * if (!user) { * throw new UnauthorizedException(); * } * * return { * welcome: `Hello, ${user.name}!`, * email: user.email, * roles: user.roles || [] * }; * } * } * ``` * * @returns The current session object or null if no session exists */ export declare const AuthSession: (...dataOrPipes: unknown[]) => ParameterDecorator;