42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import { Module } from '@nestjs/common';
|
|
import { JwtModule } from '@nestjs/jwt';
|
|
import { PassportModule } from '@nestjs/passport';
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
import { AuthService } from './auth.service';
|
|
import { AuthController } from './auth.controller';
|
|
import { JwtStrategy } from './strategies/jwt.strategy';
|
|
import { User } from '../user/user.entity';
|
|
|
|
@Module({
|
|
imports: [
|
|
TypeOrmModule.forFeature([User]),
|
|
PassportModule.register({ defaultStrategy: 'jwt' }),
|
|
JwtModule.registerAsync({
|
|
imports: [ConfigModule],
|
|
// @ts-expect-error - JWT expiresIn accepts string but type definition is strict
|
|
useFactory: (configService: ConfigService) => {
|
|
const expiresIn = configService.get<string>(
|
|
'JWT_EXPIRES_IN',
|
|
'7d',
|
|
);
|
|
const secret = configService.get<string>(
|
|
'JWT_SECRET',
|
|
'your-secret-key-change-in-production',
|
|
);
|
|
return {
|
|
secret: secret,
|
|
signOptions: {
|
|
expiresIn: expiresIn || '7d', // 默认7天过期
|
|
},
|
|
};
|
|
},
|
|
inject: [ConfigService],
|
|
}),
|
|
],
|
|
controllers: [AuthController],
|
|
providers: [AuthService, JwtStrategy],
|
|
exports: [AuthService, JwtModule],
|
|
})
|
|
export class AuthModule {}
|