请求token验证策略

This commit is contained in:
Jeremy Yin 2019-07-09 21:39:12 +08:00
parent 3d86753efe
commit ebc405f548
1 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { Strategy, ExtractJwt } from "passport-jwt";
import { JwtPayload } from "../auth.interface";
import { UserService } from "../../user/user.service";
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(
private readonly userService: UserService
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'nDSAR3+K4pLD+HIl9xWFY/n7maMxLwPTj5gscZvd9Vo=',
})
}
async validate(payload: JwtPayload) {
console.log(payload)
const { username } = payload
const entity = await this.userService.findByUserName(username)
if (!entity) {
throw new UnauthorizedException('用户不存在')
}
return entity
}
}