Skip to content
Snippets Groups Projects
Commit aba25a45 authored by Samuli Virtapohja's avatar Samuli Virtapohja
Browse files

auditing backend

parent 74e4d57a
No related branches found
No related tags found
2 merge requests!59Development to master,!58Development to testing
...@@ -27,6 +27,8 @@ import { ReplayModule } from './replay/replay.module'; ...@@ -27,6 +27,8 @@ import { ReplayModule } from './replay/replay.module';
TypeOrmModule checks ormconfig.json for database connection. 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 Providers can be found from shared folder
- HttpErrorFilter - HttpErrorFilter
- LoggingInterceptor - LoggingInterceptor
......
...@@ -82,7 +82,7 @@ export class FactionController { ...@@ -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 // :id is the id of the game, and is needed for GameStates to check the state of the game
@Put('join-faction/:id') @Put('join-faction/:id')
@UseGuards(new AuthGuard()) @UseGuards(new AuthGuard())
@GameStates('CREATED', 'STARTED') @GameStates('CREATED', 'STARTED', 'PAUSED')
@UsePipes(new ValidationPipe()) @UsePipes(new ValidationPipe())
joinFaction( joinFaction(
@User('id') person, @User('id') person,
......
...@@ -4,6 +4,8 @@ import { AppModule } from './app.module'; ...@@ -4,6 +4,8 @@ import { AppModule } from './app.module';
/* /*
Main.ts starts the server. Main.ts starts the server.
.env.PORT is not defined, port 5000 will be listened by default
*/ */
async function bootstrap() { async function bootstrap() {
...@@ -14,6 +16,6 @@ async function bootstrap() { ...@@ -14,6 +16,6 @@ async function bootstrap() {
// Cors is needed for application/json POST // Cors is needed for application/json POST
app.enableCors(); app.enableCors();
// Server will listen on port // Server will listen on port
await app.listen(port); await app.listen(process.env.PORT || port);
} }
bootstrap(); bootstrap();
...@@ -18,8 +18,9 @@ UserController is being used for routing: ...@@ -18,8 +18,9 @@ UserController is being used for routing:
- Register - Register
- Verify is checking for logged in user - Verify is checking for logged in user
*/
See shared files for more information on decorators.
*/
@Controller('user') @Controller('user')
export class UserController { export class UserController {
......
import { IsString, IsNotEmpty, Length } from 'class-validator'; 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 { export class UserDTO {
// uses class-validator built in validations @IsString()
// see https://github.com/typestack/class-validator @IsNotEmpty()
@IsString() @IsNotEmpty() @Length(3, 31) @Length(3, 31)
name: string; name: string;
@IsString() @IsNotEmpty() @Length(3, 255) @IsString()
password: string; @IsNotEmpty()
} @Length(3, 255)
\ No newline at end of file password: string;
}
...@@ -11,6 +11,15 @@ import * as jwt from 'jsonwebtoken'; ...@@ -11,6 +11,15 @@ import * as jwt from 'jsonwebtoken';
import { Game_PersonEntity } from '../game/game.entity'; import { Game_PersonEntity } from '../game/game.entity';
import { Exclude } from 'class-transformer'; 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') @Entity('Person')
export class PersonEntity { export class PersonEntity {
@PrimaryGeneratedColumn('uuid') id: string; @PrimaryGeneratedColumn('uuid') id: string;
...@@ -39,6 +48,7 @@ export class PersonEntity { ...@@ -39,6 +48,7 @@ export class PersonEntity {
return await bcrypt.compareSync(attempt, this.password); 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() { private get token() {
const { id, name } = this; const { id, name } = this;
return jwt.sign( return jwt.sign(
......
...@@ -3,13 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm'; ...@@ -3,13 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { UserController } from './user.controller'; import { UserController } from './user.controller';
import { UserService } from './user.service'; import { UserService } from './user.service';
import { PersonEntity} from './user.entity'; import { PersonEntity } from './user.entity';
import { GameEntity } from '../game/game.entity';
/* /*
Entities Entities
- PersonEntity - PersonEntity
- GameEntity
Controllers Controllers
- UserController - UserController
......
...@@ -5,9 +5,15 @@ import { InjectRepository } from '@nestjs/typeorm'; ...@@ -5,9 +5,15 @@ import { InjectRepository } from '@nestjs/typeorm';
import { PersonEntity } from './user.entity'; import { PersonEntity } from './user.entity';
import { UserDTO } from './user.dto'; 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() @Injectable()
export class UserService { export class UserService {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment