| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- /*
- * @Author: 白雾茫茫丶<baiwumm.com>
- * @Date: 2024-08-01 16:32:15
- * @LastEditors: 白雾茫茫丶<baiwumm.com>
- * @LastEditTime: 2024-08-05 14:06:24
- * @Description: JWT 鉴权模块
- */
- import { Injectable, UnauthorizedException } from '@nestjs/common';
- import { PassportStrategy } from '@nestjs/passport';
- import { Request } from 'express';
- import { ExtractJwt, Strategy } from 'passport-jwt';
- import { PrismaService } from '@/modules/prisma/prisma.service';
- @Injectable()
- export class JwtStrategy extends PassportStrategy(Strategy) {
- constructor(private prisma: PrismaService) {
- super({
- // 提供从请求中提取 JWT 的方法。我们将使用在 API 请求的授权头中提供token的标准方法
- jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
- // 为了明确起见,我们选择默认的 false 设置,
- // 它将确保 JWT 没有过期的责任委托给 Passport 模块。
- // 这意味着,如果我们的路由提供了一个过期的 JWT ,请求将被拒绝,并发送 401 未经授权的响应。Passport 会自动为我们办理
- ignoreExpiration: false,
- // 使用权宜的选项来提供对称的秘密来签署令牌
- secretOrKey: process.env.JWT_SECRET,
- passReqToCallback: true,
- });
- }
- async validate(req: Request, payload: CommonType.TokenPayload) {
- // 获取当前 token
- const token = ExtractJwt.fromAuthHeaderAsBearerToken()(req);
- // 缺少令牌
- if (!token) {
- throw new UnauthorizedException('未登录');
- }
- const user = await this.prisma.user.findUnique({
- where: {
- id: payload.sub,
- },
- });
- if (!user) {
- throw new UnauthorizedException('token令牌非法,请重新登录');
- }
- return payload;
- }
- }
|