api/src/Infrastructure/User/Security/BearerStrategy.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 25
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 20
mnd 1
bc 1
fnc 1
dl 0
loc 25
rs 10
bpm 1
cpm 2
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A BearerStrategy.validate 0 8 2
1
import { Strategy } from 'passport-http-bearer';
2
import { PassportStrategy } from '@nestjs/passport';
3
import { Injectable, UnauthorizedException, Inject } from '@nestjs/common';
4
import { IUserRepository } from 'src/Domain/User/Repository/IUserRepository';
5
import { UserAuthView } from './UserAuthView';
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<UserAuthView> {
17
    const user = await this.userRepository.findOneByApiToken(token);
18
    if (!user) {
19
      throw new UnauthorizedException();
20
    }
21
22
    return new UserAuthView(user.getId(), user.getRole());
23
  }
24
}
25