Setup login controller skeleton
parent
68ab4aedde
commit
0fadcfd732
File diff suppressed because it is too large
Load Diff
|
|
@ -7,7 +7,7 @@
|
|||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"format": "prettier --write \"src/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
|
|
@ -19,7 +19,8 @@
|
|||
"@nestjs/core": "^11.0.1",
|
||||
"@nestjs/platform-express": "^11.0.1",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1"
|
||||
"rxjs": "^7.8.1",
|
||||
"typeorm": "^0.3.27"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3.2.0",
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { AccessController } from './access.controller';
|
||||
import { UserService } from './user.service';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
controllers: [AccessController],
|
||||
providers: [UserService],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
export class User {
|
||||
id: number;
|
||||
name: string;
|
||||
password: string;
|
||||
}
|
||||
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue