Setup login controller skeleton

main
MiguelMLorente 2025-11-16 21:37:16 +01:00
parent 68ab4aedde
commit 0fadcfd732
9 changed files with 586 additions and 110 deletions

575
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@
"license": "UNLICENSED", "license": "UNLICENSED",
"scripts": { "scripts": {
"build": "nest build", "build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "format": "prettier --write \"src/**/*.ts\"",
"start": "nest start", "start": "nest start",
"start:dev": "nest start --watch", "start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch", "start:debug": "nest start --debug --watch",
@ -19,7 +19,8 @@
"@nestjs/core": "^11.0.1", "@nestjs/core": "^11.0.1",
"@nestjs/platform-express": "^11.0.1", "@nestjs/platform-express": "^11.0.1",
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1" "rxjs": "^7.8.1",
"typeorm": "^0.3.27"
}, },
"devDependencies": { "devDependencies": {
"@eslint/eslintrc": "^3.2.0", "@eslint/eslintrc": "^3.2.0",

24
src/access.controller.ts Normal file
View File

@ -0,0 +1,24 @@
import { Controller, Logger, Post, UnauthorizedException } from '@nestjs/common';
import { UserService } from './user.service';
@Controller('/access')
export class AccessController {
private readonly logger = new Logger(AccessController.name);
constructor(private readonly userService: UserService) {}
@Post('/login')
public login(name: string, password: string): void {
this.logger.debug("Received login request");
const user = this.userService.getUserByName(name);
if (user.password !== password) {
throw new UnauthorizedException();
}
}
@Post('/register')
public new(name: string, password: string): void {
this.logger.debug("Received register request");
this.userService.createUser(name, password);
}
}

View File

@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

View File

@ -1,10 +1,10 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AppController } from './app.controller'; import { AccessController } from './access.controller';
import { AppService } from './app.service'; import { UserService } from './user.service';
@Module({ @Module({
imports: [], imports: [],
controllers: [AppController], controllers: [AccessController],
providers: [AppService], providers: [UserService],
}) })
export class AppModule {} export class AppModule {}

View File

@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

5
src/dto/user.ts Normal file
View File

@ -0,0 +1,5 @@
export class User {
id: number;
name: string;
password: string;
}

View File

@ -1,8 +1,8 @@
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000); await app.listen(process.env.PORT ?? 3000);
} }
bootstrap(); bootstrap();

31
src/user.service.ts Normal file
View File

@ -0,0 +1,31 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { User } from './dto/user';
@Injectable()
export class UserService {
private readonly users: User[] = [];
constructor() {}
public getUserByName(name: string): User {
const user = this.users.find((u) => u.name === name);
if (!user) {
throw new NotFoundException();
}
return user;
}
public createUser(name: string, password: string): void {
try {
this.getUserByName(name);
throw new NotFoundException();
} catch (e) {
if (!(e instanceof NotFoundException)) throw e;
}
this.users.push({
id: this.users.length,
name,
password,
});
}
}