49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { Module, Global } from '@nestjs/common';
|
|
import { PassportModule } from '@nestjs/passport';
|
|
import { JwtModule } from '@nestjs/jwt';
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
import { APP_GUARD } from '@nestjs/core';
|
|
import { AuthController } from './auth.controller';
|
|
import { AuthService } from './auth.service';
|
|
import { SupabaseService } from './supabase.service';
|
|
import { JwtAuthGuard } from './guards/jwt-auth.guard';
|
|
import { UsersModule } from '../users/users.module';
|
|
|
|
@Global()
|
|
@Module({
|
|
imports: [
|
|
PassportModule.register({ defaultStrategy: 'jwt' }),
|
|
JwtModule.registerAsync({
|
|
imports: [ConfigModule],
|
|
useFactory: async (configService: ConfigService) => {
|
|
const secret = configService.get<string>('JWT_SECRET');
|
|
if (!secret || secret.trim() === '') {
|
|
throw new Error(
|
|
'JWT_SECRET is not set or empty. Set it in .env and run Docker with: docker compose --env-file .env up -d',
|
|
);
|
|
}
|
|
return {
|
|
secret: secret.trim(),
|
|
signOptions: {
|
|
expiresIn: '7d',
|
|
},
|
|
};
|
|
},
|
|
inject: [ConfigService],
|
|
}),
|
|
UsersModule,
|
|
],
|
|
controllers: [AuthController],
|
|
providers: [
|
|
AuthService,
|
|
SupabaseService,
|
|
JwtAuthGuard,
|
|
{
|
|
provide: APP_GUARD,
|
|
useClass: JwtAuthGuard,
|
|
},
|
|
],
|
|
exports: [AuthService, SupabaseService, JwtAuthGuard],
|
|
})
|
|
export class AuthModule {}
|