Skip to content

Commit

Permalink
feat(auth): implement auth scope for story endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rasouza committed Aug 28, 2021
1 parent 220d023 commit 400c3d3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
5 changes: 1 addition & 4 deletions src/stories/dto/create-story.dto.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/stories/entities/story.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class Story {
id: number;
user_id: number;
user_id: string;
repo: string;
date: Date;
summary: string;
Expand Down
9 changes: 5 additions & 4 deletions src/stories/stories.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Patch,
Param,
Delete,
Req,
} from '@nestjs/common';
import { StoriesService } from './stories.service';
import { CreateStoryDto } from './dto/create-story.dto';
Expand All @@ -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')
Expand Down
11 changes: 7 additions & 4 deletions src/stories/stories.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Story>('stories')
.insert(createStoryDto);
.insert({ ...createStoryDto, user_id });

if (error) throw error;

return data;
}

async findAll() {
const { data, error } = await this.db.from<Story>('stories').select('*');
async findAll(user_id) {
const { data, error } = await this.db
.from<Story>('stories')
.select('*')
.eq('user_id', user_id);

if (error) throw error;

Expand Down

0 comments on commit 400c3d3

Please sign in to comment.