-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjwt-refresh.strategy.ts
More file actions
51 lines (45 loc) · 1.65 KB
/
jwt-refresh.strategy.ts
File metadata and controls
51 lines (45 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { ConfigService } from '@nestjs/config';
import {
BadRequestException,
UnauthorizedException,
} from '@nestjs/common/exceptions';
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { CustomerService } from '@customer/application/customer.service';
import { BusinessService } from '../../business/application/business.service';
import { DriverService } from '../../driver/application/driver.service';
import { IUserService } from '../user.interface';
import { UserDto } from '../presentation/user.dto';
@Injectable()
export class JwtRefreshStrategy extends PassportStrategy(Strategy, 'refresh') {
private readonly userServices: {
[key: string]: IUserService;
} = {};
constructor(
private readonly configService: ConfigService,
private readonly customerService: CustomerService,
private readonly businessService: BusinessService,
private readonly driverService: DriverService,
) {
super({
secretOrKey: configService.get<string>('jwt/refresh/secret'),
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
passReqToCallback: true,
});
this.userServices.customer = customerService;
this.userServices.business = businessService;
this.userServices.driver = driverService;
}
async validate(req: Request, payload: any): Promise<UserDto> {
if (!payload) {
throw new UnauthorizedException();
}
if (payload.tokenType !== 'refresh') {
throw new BadRequestException();
}
return await this.userServices[payload.userType].getOne({
userId: payload.subject,
});
}
}