import { ExtractJwt, Strategy } from 'passport-jwt'; import { PassportStrategy } from '@nestjs/passport'; import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PrismaService } from '../../prisma/prisma.service'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor( private configService: ConfigService, private prisma: PrismaService, ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.get('JWT_SECRET') || 'your-secret-key', }); } async validate(payload: any) { // 查询租户是否为超级租户 let isSuperTenant = false; if (payload.tenantId) { const tenant = await this.prisma.tenant.findUnique({ where: { id: payload.tenantId }, select: { isSuper: true }, }); isSuperTenant = tenant?.isSuper === 1; } return { userId: payload.sub, username: payload.username, tenantId: payload.tenantId, isSuperTenant, userType: payload.userType || 'adult', parentUserId: payload.parentUserId || null, }; } }