feat: add basic auth

This commit is contained in:
2026-10-21 10:31:23 +03:00
parent ef2bc6f8b6
commit 0563ebfb10
28 changed files with 633 additions and 176 deletions
+4
View File
@@ -11,4 +11,8 @@ export class CreateUserDto {
@NotBlank()
@CodePointLength(1, 100)
display_name!: string;
@NotBlank()
@CodePointLength(12, 128)
password!: string;
}
+7 -3
View File
@@ -6,8 +6,8 @@ import {
Post,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '../common/auth/auth.guard';
import { CurrentUser } from '../common/auth/current-user.decorator';
import { AuthGuard } from '../auth/auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { User } from '../common/domain';
import { CreateUserDto } from './dto/create-user.dto';
import { toUserResponse, UserResponse } from './dto/user.response';
@@ -20,7 +20,11 @@ export class UsersController {
@Post()
@HttpCode(201)
async create(@Body() dto: CreateUserDto): Promise<UserResponse> {
const user = await this.users.register(dto.username, dto.display_name);
const user = await this.users.register(
dto.username,
dto.display_name,
dto.password,
);
return toUserResponse(user);
}
-2
View File
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { CommonModule } from '../common/common.module';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
@Module({
imports: [CommonModule],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
+8 -2
View File
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import { hashPassword } from '../auth/password';
import { ApiError } from '../common/filters/api-error';
import { User } from '../common/domain';
import { PrismaService, isUniqueViolation } from '../db/prisma.service';
@@ -7,10 +8,15 @@ import { PrismaService, isUniqueViolation } from '../db/prisma.service';
export class UsersService {
constructor(private readonly prisma: PrismaService) {}
async register(username: string, displayName: string): Promise<User> {
async register(
username: string,
displayName: string,
password: string,
): Promise<User> {
const passwordHash = await hashPassword(password);
try {
const row = await this.prisma.user.create({
data: { username, displayName: displayName.trim() },
data: { username, displayName: displayName.trim(), passwordHash },
});
return toUser(row);
} catch (error) {