feat: add basic auth
This commit is contained in:
@@ -11,4 +11,8 @@ export class CreateUserDto {
|
||||
@NotBlank()
|
||||
@CodePointLength(1, 100)
|
||||
display_name!: string;
|
||||
|
||||
@NotBlank()
|
||||
@CodePointLength(12, 128)
|
||||
password!: string;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user