feat:初始化项目

This commit is contained in:
R524809
2026-01-23 17:42:48 +08:00
commit d4fc8f44ee
107 changed files with 17487 additions and 0 deletions
@@ -0,0 +1,38 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User, UserStatus } from '../../users/user.entity';
import { JwtPayload } from '../interfaces/jwt-payload.interface';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(
private readonly configService: ConfigService,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: configService.get<string>(
'JWT_SECRET',
'your-secret-key-change-in-production',
),
});
}
async validate(payload: JwtPayload): Promise<User> {
const user = await this.userRepository.findOne({
where: { id: payload.sub },
});
if (!user || user.status !== UserStatus.ACTIVE) {
throw new UnauthorizedException('用户不存在或已被禁用');
}
return user;
}
}