/
events-jwt.strategy.ts
31 lines (29 loc) · 930 Bytes
/
events-jwt.strategy.ts
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
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { User } from '@altairgraphql/db';
import { AuthService } from '../auth.service';
import { JwtDto } from '../models/jwt.dto';
@Injectable()
export class EventsJwtStrategy extends PassportStrategy(
Strategy,
'events-jwt'
) {
constructor(
private readonly authService: AuthService,
readonly configService: ConfigService
) {
super({
jwtFromRequest: ExtractJwt.fromUrlQueryParameter('slt'),
secretOrKey: configService.get('EVENTS_JWT_ACCESS_SECRET'),
});
}
async validate(payload: JwtDto): Promise<User> {
const user = await this.authService.validateUser(payload.userId);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}