From aba25a45619beaf4de5095878bef4c19b931253d Mon Sep 17 00:00:00 2001
From: Samuli Virtapohja <l4721@student.jamk.fi>
Date: Wed, 24 Jul 2019 11:54:05 +0300
Subject: [PATCH] auditing backend

---
 src/app.module.ts                 |  2 ++
 src/faction/faction.controller.ts |  2 +-
 src/main.ts                       |  4 +++-
 src/user/user.controller.ts       |  3 ++-
 src/user/user.dto.ts              | 22 +++++++++++++++-------
 src/user/user.entity.ts           | 10 ++++++++++
 src/user/user.module.ts           |  5 +----
 src/user/user.service.ts          | 10 ++++++++--
 8 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/src/app.module.ts b/src/app.module.ts
index fea4f14..30a7c06 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -27,6 +27,8 @@ import { ReplayModule } from './replay/replay.module';
 
   TypeOrmModule checks ormconfig.json for database connection.
 
+  More information on global decorators can be found from shared folder.
+  
   Providers can be found from shared folder
    - HttpErrorFilter
    - LoggingInterceptor
diff --git a/src/faction/faction.controller.ts b/src/faction/faction.controller.ts
index 60ae33a..24e4a9a 100644
--- a/src/faction/faction.controller.ts
+++ b/src/faction/faction.controller.ts
@@ -82,7 +82,7 @@ export class FactionController {
   // :id is the id of the game, and is needed for GameStates to check the state of the game
   @Put('join-faction/:id')
   @UseGuards(new AuthGuard())
-  @GameStates('CREATED', 'STARTED')
+  @GameStates('CREATED', 'STARTED', 'PAUSED')
   @UsePipes(new ValidationPipe())
   joinFaction(
     @User('id') person,
diff --git a/src/main.ts b/src/main.ts
index 2999e14..6504af3 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -4,6 +4,8 @@ import { AppModule } from './app.module';
 
 /*
   Main.ts starts the server.
+
+  .env.PORT is not defined, port 5000 will be listened by default
 */
 
 async function bootstrap() {
@@ -14,6 +16,6 @@ async function bootstrap() {
   // Cors is needed for application/json POST
   app.enableCors();
   // Server will listen on port
-  await app.listen(port);
+  await app.listen(process.env.PORT || port);
 }
 bootstrap();
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 9afb56d..6e39509 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -18,8 +18,9 @@ UserController is being used for routing:
 - Register
 
 - Verify is checking for logged in user
-*/
 
+See shared files for more information on decorators.
+*/
 
 @Controller('user')
 export class UserController {
diff --git a/src/user/user.dto.ts b/src/user/user.dto.ts
index c5bfa70..4f1b8d6 100644
--- a/src/user/user.dto.ts
+++ b/src/user/user.dto.ts
@@ -1,10 +1,18 @@
 import { IsString, IsNotEmpty, Length } from 'class-validator';
 
+/*
+Contains Validation for UserDTO
+uses class-validator built in validations
+see https://github.com/typestack/class-validator
+*/
+
 export class UserDTO {
-    // uses class-validator built in validations
-    // see https://github.com/typestack/class-validator
-    @IsString() @IsNotEmpty() @Length(3, 31)
-    name: string;
-    @IsString() @IsNotEmpty() @Length(3, 255)
-    password: string;
-}
\ No newline at end of file
+  @IsString()
+  @IsNotEmpty()
+  @Length(3, 31)
+  name: string;
+  @IsString()
+  @IsNotEmpty()
+  @Length(3, 255)
+  password: string;
+}
diff --git a/src/user/user.entity.ts b/src/user/user.entity.ts
index 716020a..216eeb8 100644
--- a/src/user/user.entity.ts
+++ b/src/user/user.entity.ts
@@ -11,6 +11,15 @@ import * as jwt from 'jsonwebtoken';
 import { Game_PersonEntity } from '../game/game.entity';
 import { Exclude } from 'class-transformer';
 
+/*
+UserEntity reflects database table.
+
+Before handling password to database we encrypt it with bcrypt.
+password field will be excluded unless we call repository relation.
+
+token will be created when user registers or logs in to the system.
+*/
+
 @Entity('Person')
 export class PersonEntity {
   @PrimaryGeneratedColumn('uuid') id: string;
@@ -39,6 +48,7 @@ export class PersonEntity {
     return await bcrypt.compareSync(attempt, this.password);
   }
 
+  // creates token from id and name, it will be created through jsonwebtoken and .env SECRET field
   private get token() {
     const { id, name } = this;
     return jwt.sign(
diff --git a/src/user/user.module.ts b/src/user/user.module.ts
index a160e47..8b01d81 100644
--- a/src/user/user.module.ts
+++ b/src/user/user.module.ts
@@ -3,13 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
 
 import { UserController } from './user.controller';
 import { UserService } from './user.service';
-import { PersonEntity} from './user.entity';
-import { GameEntity } from '../game/game.entity';
-
+import { PersonEntity } from './user.entity';
 /*
 Entities
 - PersonEntity
-- GameEntity
 
 Controllers
 - UserController
diff --git a/src/user/user.service.ts b/src/user/user.service.ts
index e405972..d34dec0 100644
--- a/src/user/user.service.ts
+++ b/src/user/user.service.ts
@@ -5,9 +5,15 @@ import { InjectRepository } from '@nestjs/typeorm';
 import { PersonEntity } from './user.entity';
 import { UserDTO } from './user.dto';
 /*
-UserService contains 
-*/
+UserService contains functions for 
+- Login
+- Register
+
+Both functions return logged in users tokenObject
+See more info in UserEntity.
 
+See more info on DTO in it's respective file
+*/
 
 @Injectable()
 export class UserService {
-- 
GitLab