@wikiccu/nest-auth
Version:
A comprehensive authentication package for NestJS applications with Prisma and PostgreSQL
436 lines • 19.3 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const auth_service_1 = require("../services/auth.service");
const jwt_auth_guard_1 = require("../guards/jwt-auth.guard");
const roles_guard_1 = require("../guards/roles.guard");
const public_decorator_1 = require("../decorators/public.decorator");
const roles_decorator_1 = require("../decorators/roles.decorator");
const current_user_decorator_1 = require("../decorators/current-user.decorator");
const auth_dto_1 = require("../dto/auth.dto");
const responses_dto_1 = require("../dto/responses.dto");
let AuthController = class AuthController {
authService;
constructor(authService) {
this.authService = authService;
}
async register(registerDto) {
return this.authService.register(registerDto);
}
async login(loginDto, req) {
const sessionInfo = {
ipAddress: req.ip,
userAgent: req.headers['user-agent'],
};
return this.authService.login(loginDto, sessionInfo);
}
async refreshToken(refreshTokenDto) {
return this.authService.refreshToken(refreshTokenDto);
}
async logout(logoutDto) {
return this.authService.logout(logoutDto);
}
async forgotPassword(forgotPasswordDto) {
return this.authService.forgotPassword(forgotPasswordDto);
}
async resetPassword(resetPasswordDto) {
return this.authService.resetPassword(resetPasswordDto);
}
async verifyEmail(verifyEmailDto) {
return this.authService.verifyEmail(verifyEmailDto);
}
async resendVerification(resendVerificationDto) {
return this.authService.resendVerification(resendVerificationDto);
}
async getProfile(user) {
return this.authService.getProfile(user.id);
}
async updateProfile(user, updateUserDto) {
return this.authService.updateProfile(user.id, updateUserDto);
}
async changePassword(user, changePasswordDto) {
return this.authService.changePassword(user.id, changePasswordDto);
}
async getUsers(page = 1, limit = 10) {
return this.authService.getUsers(Number(page), Number(limit));
}
async createUser(createUserDto) {
return this.authService.createUser(createUserDto);
}
async updateUser(id, updateUserDto) {
return this.authService.updateUser(id, updateUserDto);
}
async deleteUser(id) {
return this.authService.deleteUser(id);
}
};
exports.AuthController = AuthController;
__decorate([
(0, common_1.Post)('register'),
(0, public_decorator_1.Public)(),
(0, swagger_1.ApiOperation)({
summary: 'Register a new user',
description: 'Creates a new user account and sends email verification',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.RegisterDto }),
(0, swagger_1.ApiResponse)({
status: 201,
description: 'User registered successfully',
type: responses_dto_1.RegisterResponseDto,
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - Invalid input data' }),
(0, swagger_1.ApiResponse)({ status: 409, description: 'Conflict - User already exists' }),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.RegisterDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "register", null);
__decorate([
(0, common_1.Post)('login'),
(0, public_decorator_1.Public)(),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_1.ApiOperation)({
summary: 'Login user',
description: 'Authenticates user and returns access and refresh tokens',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.LoginDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Login successful',
type: responses_dto_1.LoginResponseDto,
}),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Invalid credentials' }),
__param(0, (0, common_1.Body)()),
__param(1, (0, common_1.Request)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.LoginDto, Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "login", null);
__decorate([
(0, common_1.Post)('refresh'),
(0, public_decorator_1.Public)(),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_1.ApiOperation)({
summary: 'Refresh access token',
description: 'Uses refresh token to get new access and refresh tokens',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.RefreshTokenDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Token refreshed successfully',
type: responses_dto_1.RefreshTokenResponseDto,
}),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Invalid refresh token' }),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.RefreshTokenDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "refreshToken", null);
__decorate([
(0, common_1.Post)('logout'),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_1.ApiOperation)({
summary: 'Logout user',
description: 'Revokes the provided refresh token',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.LogoutDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Logged out successfully',
schema: {
type: 'object',
properties: {
message: { type: 'string', example: 'Logged out successfully' },
},
},
}),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.LogoutDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "logout", null);
__decorate([
(0, common_1.Post)('forgot-password'),
(0, public_decorator_1.Public)(),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_1.ApiOperation)({
summary: 'Request password reset',
description: 'Sends password reset email if user exists',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.ForgotPasswordDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Password reset email sent',
type: responses_dto_1.PasswordResetResponseDto,
}),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.ForgotPasswordDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "forgotPassword", null);
__decorate([
(0, common_1.Post)('reset-password'),
(0, public_decorator_1.Public)(),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_1.ApiOperation)({
summary: 'Reset password',
description: 'Resets password using token from email',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.ResetPasswordDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Password reset successfully',
schema: {
type: 'object',
properties: {
message: { type: 'string', example: 'Password reset successfully' },
},
},
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - Invalid or expired token' }),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.ResetPasswordDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "resetPassword", null);
__decorate([
(0, common_1.Post)('verify-email'),
(0, public_decorator_1.Public)(),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_1.ApiOperation)({
summary: 'Verify email address',
description: 'Verifies email address using token from email',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.VerifyEmailDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Email verified successfully',
type: responses_dto_1.EmailVerificationResponseDto,
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - Invalid or expired token' }),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.VerifyEmailDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "verifyEmail", null);
__decorate([
(0, common_1.Post)('resend-verification'),
(0, public_decorator_1.Public)(),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_1.ApiOperation)({
summary: 'Resend email verification',
description: 'Resends email verification to user',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.ResendVerificationDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Verification email sent',
type: responses_dto_1.EmailVerificationResponseDto,
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - User not found or already verified' }),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.ResendVerificationDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "resendVerification", null);
__decorate([
(0, common_1.Get)('profile'),
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard),
(0, swagger_1.ApiBearerAuth)(),
(0, swagger_1.ApiOperation)({
summary: 'Get user profile',
description: 'Returns current user profile information',
}),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'User profile retrieved successfully',
type: responses_dto_1.AuthUserResponse,
}),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Authentication required' }),
__param(0, (0, current_user_decorator_1.CurrentUser)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "getProfile", null);
__decorate([
(0, common_1.Put)('profile'),
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard),
(0, swagger_1.ApiBearerAuth)(),
(0, swagger_1.ApiOperation)({
summary: 'Update user profile',
description: 'Updates current user profile information',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.UpdateUserDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Profile updated successfully',
type: responses_dto_1.AuthUserResponse,
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - Invalid input data' }),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Authentication required' }),
(0, swagger_1.ApiResponse)({ status: 409, description: 'Conflict - Email or username already exists' }),
__param(0, (0, current_user_decorator_1.CurrentUser)()),
__param(1, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, auth_dto_1.UpdateUserDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "updateProfile", null);
__decorate([
(0, common_1.Put)('change-password'),
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard),
(0, swagger_1.ApiBearerAuth)(),
(0, swagger_1.ApiOperation)({
summary: 'Change password',
description: 'Changes current user password',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.ChangePasswordDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Password changed successfully',
schema: {
type: 'object',
properties: {
message: { type: 'string', example: 'Password changed successfully' },
},
},
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - Current password incorrect' }),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Authentication required' }),
__param(0, (0, current_user_decorator_1.CurrentUser)()),
__param(1, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, auth_dto_1.ChangePasswordDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "changePassword", null);
__decorate([
(0, common_1.Get)('users'),
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, roles_guard_1.RolesGuard),
(0, roles_decorator_1.Roles)('admin'),
(0, swagger_1.ApiBearerAuth)(),
(0, swagger_1.ApiOperation)({
summary: 'Get all users (Admin only)',
description: 'Returns paginated list of all users',
}),
(0, swagger_1.ApiQuery)({ name: 'page', required: false, type: Number, description: 'Page number' }),
(0, swagger_1.ApiQuery)({ name: 'limit', required: false, type: Number, description: 'Items per page' }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Users retrieved successfully',
schema: {
type: 'object',
properties: {
users: {
type: 'array',
items: { $ref: '#/components/schemas/AuthUser' },
},
total: { type: 'number' },
page: { type: 'number' },
limit: { type: 'number' },
},
},
}),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Authentication required' }),
(0, swagger_1.ApiResponse)({ status: 403, description: 'Forbidden - Admin access required' }),
__param(0, (0, common_1.Query)('page')),
__param(1, (0, common_1.Query)('limit')),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "getUsers", null);
__decorate([
(0, common_1.Post)('users'),
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, roles_guard_1.RolesGuard),
(0, roles_decorator_1.Roles)('admin'),
(0, swagger_1.ApiBearerAuth)(),
(0, swagger_1.ApiOperation)({
summary: 'Create user (Admin only)',
description: 'Creates a new user account (pre-verified)',
}),
(0, swagger_1.ApiBody)({ type: auth_dto_1.CreateUserDto }),
(0, swagger_1.ApiResponse)({
status: 201,
description: 'User created successfully',
type: responses_dto_1.AuthUserResponse,
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - Invalid input data' }),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Authentication required' }),
(0, swagger_1.ApiResponse)({ status: 403, description: 'Forbidden - Admin access required' }),
(0, swagger_1.ApiResponse)({ status: 409, description: 'Conflict - User already exists' }),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [auth_dto_1.CreateUserDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "createUser", null);
__decorate([
(0, common_1.Put)('users/:id'),
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, roles_guard_1.RolesGuard),
(0, roles_decorator_1.Roles)('admin'),
(0, swagger_1.ApiBearerAuth)(),
(0, swagger_1.ApiOperation)({
summary: 'Update user (Admin only)',
description: 'Updates user information including roles and active status',
}),
(0, swagger_1.ApiParam)({ name: 'id', description: 'User ID' }),
(0, swagger_1.ApiBody)({ type: auth_dto_1.UpdateUserDto }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'User updated successfully',
type: responses_dto_1.AuthUserResponse,
}),
(0, swagger_1.ApiResponse)({ status: 400, description: 'Bad Request - Invalid input data' }),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Authentication required' }),
(0, swagger_1.ApiResponse)({ status: 403, description: 'Forbidden - Admin access required' }),
(0, swagger_1.ApiResponse)({ status: 409, description: 'Conflict - Email or username already exists' }),
__param(0, (0, common_1.Param)('id')),
__param(1, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, auth_dto_1.UpdateUserDto]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "updateUser", null);
__decorate([
(0, common_1.Delete)('users/:id'),
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, roles_guard_1.RolesGuard),
(0, roles_decorator_1.Roles)('admin'),
(0, swagger_1.ApiBearerAuth)(),
(0, swagger_1.ApiOperation)({
summary: 'Delete user (Admin only)',
description: 'Permanently deletes a user account',
}),
(0, swagger_1.ApiParam)({ name: 'id', description: 'User ID' }),
(0, swagger_1.ApiResponse)({
status: 200,
description: 'User deleted successfully',
schema: {
type: 'object',
properties: {
message: { type: 'string', example: 'User deleted successfully' },
},
},
}),
(0, swagger_1.ApiResponse)({ status: 401, description: 'Unauthorized - Authentication required' }),
(0, swagger_1.ApiResponse)({ status: 403, description: 'Forbidden - Admin access required' }),
__param(0, (0, common_1.Param)('id')),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "deleteUser", null);
exports.AuthController = AuthController = __decorate([
(0, swagger_1.ApiTags)('Authentication'),
(0, common_1.Controller)('auth'),
__metadata("design:paramtypes", [auth_service_1.AuthService])
], AuthController);
//# sourceMappingURL=auth.controller.js.map