Skip to content

Instantly share code, notes, and snippets.

@yaroslavTsebro
Created November 17, 2023 12:31
Show Gist options
  • Select an option

  • Save yaroslavTsebro/d8fb669eee810b881dfe053cc695305f to your computer and use it in GitHub Desktop.

Select an option

Save yaroslavTsebro/d8fb669eee810b881dfe053cc695305f to your computer and use it in GitHub Desktop.
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class AccessTokenGuard extends AuthGuard('jwt') {}
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { ConfigService } from '@nestjs/config';
import { AuthService } from '../service/auth.service';
import { AccessTokenPayload } from '../entity/access-token-payload.interface';
import User from '../user/entity/user.entity';
@Injectable()
export class AccessTokenStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor(
configService: ConfigService,
private readonly authService: AuthService,
) {
super({
jwtFromRequest: ExtractJwt.fromExtractors([
(request: any) => {
console.log(request?.cookies?.Authentication);
return (
request?.cookies?.Authentication ||
request?.Authentication ||
request?.headers.Authentication
);
},
]),
secretOrKey: configService.get<string>('JWT_ACCESS_SECRET'),
});
}
validate(payload: AccessTokenPayload): Promise<User> {
console.log(payload);
return this.authService.getById(payload.id);
}
}
import {
Controller,
Get,
Inject,
Param,
Post,
UseGuards,
Delete,
Put,
Body,
} from '@nestjs/common';
import { TAG_SERVICE } from '../service/tag-service.interface';
import { TagService } from '../service/tag.service';
import { CurrentUser } from '@app/common';
import User from 'src/auth/user/entity/user.entity';
import { CreateTagDto } from '../entity/dto/create-tag.dto';
import { UpdateTagDto } from '../entity/dto/update-tag.dto';
import Tag from '../entity/tag.entity';
import { AccessTokenGuard } from 'src/auth/guard/access-token.guard';
@Controller('tag')
export class TagController {
constructor(@Inject(TAG_SERVICE) private readonly tagService: TagService) {}
@Post()
@UseGuards(AccessTokenGuard)
async create(
@Body() dto: CreateTagDto,
@CurrentUser() user: User,
): Promise<Tag> {
return await this.tagService.create(dto, user);
}
@Get('created-by-me')
@UseGuards(AccessTokenGuard)
async getTagsCreatedByUser(@CurrentUser() user: User): Promise<Tag[]> {
return await this.tagService.getTagsCreatedByUser(user);
}
@Get('name-like/:name')
async getByNameLike(@Param('name') name: string): Promise<Tag[]> {
return await this.tagService.getByNameLike(name);
}
@Get(':id')
async getById(@Param('id') id: number): Promise<Tag> {
return await this.tagService.getById(id);
}
@Delete(':id')
async delete(@Param('id') id: number): Promise<number> {
return await this.tagService.delete(id);
}
@Put()
async update(@Body() dto: UpdateTagDto, id: number): Promise<Tag> {
return await this.tagService.update(dto, id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment