src/Infrastructure/HumanResource/User/Security/LocalStrategy.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 30
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A LocalStrategy.validate 0 9 2
1
import { Strategy } from 'passport-local';
2
import { PassportStrategy } from '@nestjs/passport';
3
import { Injectable, UnauthorizedException, Inject } from '@nestjs/common';
4
import { AuthenticatedView } from 'src/Application/HumanResource/User/View/AuthenticatedView';
5
import { IQueryBus } from '@nestjs/cqrs';
6
import { LoginQuery } from 'src/Application/HumanResource/User/Query/LoginQuery';
7
8
@Injectable()
9
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
10
  constructor(
11
    @Inject('IQueryBus')
12
    private readonly queryBus: IQueryBus
13
  ) {
14
    super({
15
      usernameField: 'email'
16
    });
17
  }
18
19
  public async validate(
20
    email: string,
21
    password: string
22
  ): Promise<AuthenticatedView> {
23
    try {
24
      return await this.queryBus.execute(new LoginQuery(email, password));
25
    } catch (err) {
26
      throw new UnauthorizedException('login-error-failed');
27
    }
28
  }
29
}
30