Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 30 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |