Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .env.copy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=admin
POSTGRES_PASSWORD=admin
POSTGRES_DB=nestjs
PORT=5000

JWT_SECRET=abc
JWT_EXPIRATION_TIME=360000
AWS_REGION=abc
AWS_ACCESS_KEY_ID=abc
AWS_SECRET_ACCESS_KEY=abc
AWS_PUBLIC_BUCKET_NAME=abc
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
$ npm install
```

```bash
$ docker-compose up
```

## Running the app

```bash
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"
services:
postgres:
container_name: postgres
image: postgres:latest
image: postgres:9.6
ports:
- "5432:5432"
volumes:
Expand Down
5 changes: 5 additions & 0 deletions docker.env.copy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_USER=admin
POSTGRES_PASSWORD=admin
POSTGRES_DB=nestjs
PGADMIN_DEFAULT_EMAIL=admin@admin.com
PGADMIN_DEFAULT_PASSWORD=admin
4,966 changes: 2,967 additions & 1,999 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"uuid": "^8.3.0"
},
"devDependencies": {
"@nestjs/cli": "^7.0.0",
"@nestjs/cli": "^7.4.1",
"@nestjs/schematics": "^7.0.0",
"@nestjs/testing": "^7.3.1",
"@types/express": "^4.17.6",
Expand Down
8 changes: 4 additions & 4 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Module } from '@nestjs/common';
import { PostsModule } from './posts/posts.module';
import { ConfigModule } from '@nestjs/config';
import * as Joi from '@hapi/joi';
import { DatabaseModule } from './database/database.module';
import { AuthenticationModule } from './authentication/authentication.module';
import { UsersModule } from './users/users.module';
import { CategoriesModule } from './categories/categories.module';
import { DatabaseModule } from './database/database.module';
import { UsersModule } from './users/users.module';

@Module({
imports: [
Expand All @@ -26,10 +26,10 @@ import { CategoriesModule } from './categories/categories.module';
PORT: Joi.number(),
})
}),
DatabaseModule,
AuthenticationModule,
UsersModule,
CategoriesModule,
DatabaseModule,
UsersModule,
],
controllers: [],
providers: [],
Expand Down
19 changes: 7 additions & 12 deletions src/authentication/authentication.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {
Body,
Req,
Controller,
HttpCode,
Post,
UseGuards,
Get, ClassSerializerInterceptor, UseInterceptors,
Controller, UseInterceptors, ClassSerializerInterceptor,
HttpCode, UseGuards, Get, Post, Body, Req,
} from '@nestjs/common';
import { AuthenticationService } from './authentication.service';
import RegisterDto from './dto/register.dto';
Expand All @@ -27,18 +22,18 @@ export class AuthenticationController {

@HttpCode(200)
@UseGuards(LocalAuthenticationGuard)
@Post('log-in')
async logIn(@Req() request: RequestWithUser) {
const {user} = request;
@Post('login')
async login(@Req() request: RequestWithUser) {
const { user } = request;
const cookie = this.authenticationService.getCookieWithJwtToken(user.id);
request.res.setHeader('Set-Cookie', cookie);
return user;
}

@UseGuards(JwtAuthenticationGuard)
@Post('log-out')
@Post('logout')
@HttpCode(200)
async logOut(@Req() request: RequestWithUser) {
async logout(@Req() request: RequestWithUser) {
request.res.setHeader('Set-Cookie', this.authenticationService.getCookieForLogOut());
}

Expand Down
3 changes: 2 additions & 1 deletion src/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class AuthenticationService {
public getCookieWithJwtToken(userId: number) {
const payload: TokenPayload = { userId };
const token = this.jwtService.sign(payload);
return `Authentication=${token}; HttpOnly; Path=/; Max-Age=${this.configService.get('JWT_EXPIRATION_TIME')}`;
const expiresIn = this.configService.get('JWT_EXPIRATION_TIME');
return `Authentication=${token}; HttpOnly; Path=/; Max-Age=${expiresIn}`;
}

public getCookieForLogOut() {
Expand Down
11 changes: 2 additions & 9 deletions src/categories/categories.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
UseGuards,
UseInterceptors,
ClassSerializerInterceptor, Post,
Controller, UseInterceptors, ClassSerializerInterceptor, HttpCode,
UseGuards, Get, Post, Put, Patch, Delete, Body, Req, Param,
} from '@nestjs/common';
import CategoriesService from './categories.service';
import CreateCategoryDto from './dto/createCategory.dto';
Expand Down
13 changes: 2 additions & 11 deletions src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
UseGuards,
Req,
UseInterceptors,
ClassSerializerInterceptor,
Controller, UseInterceptors, ClassSerializerInterceptor, HttpCode,
UseGuards, Get, Post, Put, Patch, Delete, Body, Req, Param,
} from '@nestjs/common';
import PostsService from './posts.service';
import CreatePostDto from './dto/createPost.dto';
Expand Down