| Total Complexity | 2 |
| Complexity/F | 2 |
| Lines of Code | 26 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import {Strategy} from 'passport-http-bearer'; |
||
| 2 | import {PassportStrategy} from '@nestjs/passport'; |
||
| 3 | import {Injectable, UnauthorizedException, Inject} from '@nestjs/common'; |
||
| 4 | import {User} from 'src/Domain/HumanResource/User/User.entity'; |
||
| 5 | import {IUserRepository} from 'src/Domain/HumanResource/User/Repository/IUserRepository'; |
||
| 6 | |||
| 7 | @Injectable() |
||
| 8 | export class BearerStrategy extends PassportStrategy(Strategy, 'bearer') { |
||
| 9 | constructor( |
||
| 10 | @Inject('IUserRepository') |
||
| 11 | private readonly userRepository: IUserRepository |
||
| 12 | ) { |
||
| 13 | super(); |
||
| 14 | } |
||
| 15 | |||
| 16 | public async validate(token: string): Promise<User> { |
||
| 17 | const user = await this.userRepository.findOneByApiToken(token); |
||
| 18 | |||
| 19 | if (!(user instanceof User)) { |
||
| 20 | throw new UnauthorizedException(); |
||
| 21 | } |
||
| 22 | |||
| 23 | return user; |
||
| 24 | } |
||
| 25 | } |
||
| 26 |