ohmyform/api/src/auth/strategies/jwt.strategy.ts
2019-07-30 00:46:56 +02:00

22 lines
727 B
TypeScript

import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { jwtConstants } from '../constants';
import { AuthService } from "../services/auth.service"
import {AuthUser} from "../interfaces/auth.user.interface"
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: jwtConstants.secret,
});
}
async validate(payload: any): Promise<AuthUser | null> {
return this.authService.validate(payload.username);
}
}