Skip to content

Commit

Permalink
Merge branch 'release/1.0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
allohamora committed Dec 4, 2022
2 parents af92a0b + acf6fc6 commit 3e71c12
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 31 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.0.7](https://github.com/allohamora/nest-auth-example/compare/v1.0.6...v1.0.7) (2022-12-04)

### Features

- migrate to REST ([9ad9355](https://github.com/allohamora/nest-auth-example/commit/9ad93555ea90fb7d25917a01c5ac2333620c2fe8))

### [1.0.6](https://github.com/allohamora/nest-auth-example/compare/v1.0.5...v1.0.6) (2022-03-19)

### Features
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
## Docs

- run the app
- open /api
- open /swagger
- enjoy swagger docs

## Installation
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nest-auth-example",
"version": "1.0.6",
"version": "1.0.7",
"description": "nest auth example",
"author": "https://github.com/allohamora",
"private": true,
Expand Down
14 changes: 7 additions & 7 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AuthController {
@ApiOkResponse({ type: AccessRefreshTokens })
@ApiBody({ type: RegisterLoginDto })
@HttpCode(200)
public async login(
public login(
@CurrentUser() user: User,
@UserAgent() userAgent: string,
@Ip() ip: string,
Expand All @@ -31,26 +31,26 @@ export class AuthController {
@Post('register')
@ApiException({ statusCode: HttpStatus.BAD_REQUEST })
@ApiCreatedResponse({ type: User })
public async register(@Body() registerDto: RegisterLoginDto): Promise<User> {
return await this.authService.register(registerDto);
public register(@Body() registerDto: RegisterLoginDto): Promise<User> {
return this.authService.register(registerDto);
}

@Post('refresh')
@ApiException({ statusCode: HttpStatus.BAD_REQUEST })
@ApiOkResponse({ type: AccessRefreshTokens })
@HttpCode(200)
public async refresh(
public refresh(
@Body() refreshDto: RefreshDto,
@UserAgent() userAgent: string,
@Ip() ip: string,
): Promise<AccessRefreshTokens> {
return await this.authService.refresh(refreshDto, userAgent, ip);
return this.authService.refresh(refreshDto, userAgent, ip);
}

@Auth()
@Get('test')
@Get('me')
@ApiOkResponse({ type: User })
public async test(@CurrentUser() user: User): Promise<User> {
public async me(@CurrentUser() user: User): Promise<User> {
return user;
}
}
6 changes: 3 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ export class AuthService {

const { id } = this.jwtService.decode(refreshToken) as JwtPayloadDto;
const user = await this.userService.findOne(id);
const refresAndAccessTokens = await this.createAccessAndRefreshTokens(user);
const refreshAndAccessTokens = await this.createAccessAndRefreshTokens(user);

session.refreshToken = refresAndAccessTokens.refreshToken;
session.refreshToken = refreshAndAccessTokens.refreshToken;
await this.sessionRepository.save(session);

return refresAndAccessTokens;
return refreshAndAccessTokens;
}

public async register({ login, password }: RegisterLoginDto) {
Expand Down
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pkg from '../package.json';
import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common';
import { ClassSerializerInterceptor, ValidationPipe, VersioningType } from '@nestjs/common';
import { NestFactory, Reflector } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { EntityNotFoundExceptionFilter } from './utils/entity-not-found-exception.filter';

const SWAGGER_PATH = 'api';
const SWAGGER_PATH = 'swagger';
const SERVER_PORT = 3000;

async function bootstrap() {
Expand All @@ -18,6 +18,9 @@ async function bootstrap() {
.addBearerAuth()
.build();

app.setGlobalPrefix('api');
app.enableVersioning({ defaultVersion: '1', type: VersioningType.URI });

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup(SWAGGER_PATH, app, document);

Expand Down
26 changes: 16 additions & 10 deletions src/post/post.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Controller,
UseGuards,
Post as RestPost,
Post as HttpPost,
Put,
Body,
Param,
Expand All @@ -24,45 +24,51 @@ import { CreateUpdatePostDto } from './dto/create-update-post.dto';
import { Post } from './post.entity';
import { PostService } from './post.service';

@ApiTags('post')
@Controller('post')
@ApiTags('Post')
@Controller('posts')
export class PostController {
constructor(private postService: PostService) {}

@Get()
@ApiOkResponse({ type: Post, isArray: true })
public getMany(): Promise<Post[]> {
return this.postService.getMany();
}

@Get(':id')
@ApiException({ statusCode: HttpStatus.NOT_FOUND })
@ApiOkResponse({ type: Post })
public async get(@Param('id', ParseIntPipe) id: number): Promise<Post> {
return await this.postService.get(id);
public getOne(@Param('id', ParseIntPipe) id: number): Promise<Post> {
return this.postService.getOne(id);
}

@Delete(':id')
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can(Action.Delete, Post))
@Auth()
public async delete(@Param('id', ParseIntPipe) id: number, @CurrentAbility() ability: AppAbility): Promise<void> {
return await this.postService.delete(id, ability);
return this.postService.delete(id, ability);
}

@RestPost()
@HttpPost()
@ApiCreatedResponse({ type: Post })
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can(Action.Create, Post))
@Auth()
public async create(@Body() createPostDto: CreateUpdatePostDto, @CurrentUser() user: User): Promise<Post> {
return await this.postService.create(createPostDto, user);
return this.postService.create(createPostDto, user);
}

@Put(':id')
@ApiOkResponse({ type: Post })
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can(Action.Update, Post))
@Auth()
public async update(
public update(
@Body() updatePostDto: CreateUpdatePostDto,
@Param('id', ParseIntPipe) id: number,
@CurrentAbility() ability: AppAbility,
): Promise<Post> {
return await this.postService.update(updatePostDto, id, ability);
return this.postService.update(updatePostDto, id, ability);
}
}
14 changes: 9 additions & 5 deletions src/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ export class PostService {
private postRepository: Repository<Post>,
) {}

public async create({ name, body }: CreateUpdatePostDto, author: User) {
public create({ name, body }: CreateUpdatePostDto, author: User) {
const post = this.postRepository.create({ name, body, author });

return await this.postRepository.save(post);
return this.postRepository.save(post);
}

public async get(id: number) {
return await this.postRepository.findOneOrFail(id);
public getOne(id: number) {
return this.postRepository.findOneOrFail(id);
}

public getMany() {
return this.postRepository.find();
}

public async update({ name, body }: CreateUpdatePostDto, id: number, ability: AppAbility) {
Expand All @@ -34,7 +38,7 @@ export class PostService {
post.name = name;
post.body = body;

return await this.postRepository.save(post);
return this.postRepository.save(post);
}

public async delete(id: number, ability: AppAbility) {
Expand Down

0 comments on commit 3e71c12

Please sign in to comment.