From 400c3d30ddd24ce78d1d92b28343afe756194acc Mon Sep 17 00:00:00 2001 From: "R. A. Souza" Date: Sat, 28 Aug 2021 16:02:20 -0300 Subject: [PATCH] feat(auth): implement auth scope for story endpoint --- src/auth/jwt.strategy.ts | 2 +- src/stories/dto/create-story.dto.ts | 5 +---- src/stories/entities/story.entity.ts | 2 +- src/stories/stories.controller.ts | 9 +++++---- src/stories/stories.service.ts | 11 +++++++---- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/auth/jwt.strategy.ts b/src/auth/jwt.strategy.ts index f4e3824..26ec896 100644 --- a/src/auth/jwt.strategy.ts +++ b/src/auth/jwt.strategy.ts @@ -14,6 +14,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) { } async validate(payload: any) { - return { userId: payload.sub, email: payload.email }; + return { id: payload.sub, email: payload.email }; } } diff --git a/src/stories/dto/create-story.dto.ts b/src/stories/dto/create-story.dto.ts index 7c036a6..b9e7a1d 100644 --- a/src/stories/dto/create-story.dto.ts +++ b/src/stories/dto/create-story.dto.ts @@ -1,9 +1,6 @@ -import { IsDateString, IsInt, IsNotEmpty, IsUrl } from 'class-validator'; +import { IsDateString, IsNotEmpty, IsUrl } from 'class-validator'; export class CreateStoryDto { - @IsInt() - user_id: number; - @IsNotEmpty() repo: string; diff --git a/src/stories/entities/story.entity.ts b/src/stories/entities/story.entity.ts index bb6d3cf..81d9910 100644 --- a/src/stories/entities/story.entity.ts +++ b/src/stories/entities/story.entity.ts @@ -1,6 +1,6 @@ export class Story { id: number; - user_id: number; + user_id: string; repo: string; date: Date; summary: string; diff --git a/src/stories/stories.controller.ts b/src/stories/stories.controller.ts index eea3bf7..c6434b3 100644 --- a/src/stories/stories.controller.ts +++ b/src/stories/stories.controller.ts @@ -6,6 +6,7 @@ import { Patch, Param, Delete, + Req, } from '@nestjs/common'; import { StoriesService } from './stories.service'; import { CreateStoryDto } from './dto/create-story.dto'; @@ -18,13 +19,13 @@ export class StoriesController { constructor(private readonly storiesService: StoriesService) {} @Post() - create(@Body() createStoryDto: CreateStoryDto) { - return this.storiesService.create(createStoryDto); + create(@Body() createStoryDto: CreateStoryDto, @Req() request) { + return this.storiesService.create(createStoryDto, request.user.id); } @Get() - findAll() { - return this.storiesService.findAll(); + findAll(@Req() request) { + return this.storiesService.findAll(request.user.id); } @Get(':id') diff --git a/src/stories/stories.service.ts b/src/stories/stories.service.ts index 0757115..5364447 100644 --- a/src/stories/stories.service.ts +++ b/src/stories/stories.service.ts @@ -8,18 +8,21 @@ import { Story } from './entities/story.entity'; export class StoriesService { constructor(@Inject('Supabase') private db: SupabaseClient) {} - async create(createStoryDto: CreateStoryDto) { + async create(createStoryDto: CreateStoryDto, user_id) { const { data, error } = await this.db .from('stories') - .insert(createStoryDto); + .insert({ ...createStoryDto, user_id }); if (error) throw error; return data; } - async findAll() { - const { data, error } = await this.db.from('stories').select('*'); + async findAll(user_id) { + const { data, error } = await this.db + .from('stories') + .select('*') + .eq('user_id', user_id); if (error) throw error;