feat: implement security headers and rate limiting; update environment variables and documentation

This commit is contained in:
Nils-Johan Gynther
2026-04-21 08:06:21 +02:00
parent c1d51c771e
commit 7748ad311f
13 changed files with 133 additions and 23 deletions
+3
View File
@@ -1,4 +1,5 @@
import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
import { Throttle } from '@nestjs/throttler';
import { AuthService } from './auth.service';
import { RegisterDto } from './dto/register.dto';
import { LoginDto } from './dto/login.dto';
@@ -9,12 +10,14 @@ export class AuthController {
constructor(private readonly authService: AuthService) {}
@Public()
@Throttle({ default: { ttl: 60_000, limit: 10 } })
@Post('register')
register(@Body() dto: RegisterDto) {
return this.authService.register(dto);
}
@Public()
@Throttle({ default: { ttl: 60_000, limit: 10 } })
@HttpCode(HttpStatus.OK)
@Post('login')
login(@Body() dto: LoginDto) {
+5 -1
View File
@@ -11,7 +11,11 @@ import { UsersModule } from '../users/users.module';
UsersModule,
PassportModule,
JwtModule.register({
secret: process.env.JWT_SECRET ?? 'changeme',
secret: (() => {
const secret = process.env.JWT_SECRET;
if (!secret) throw new Error('JWT_SECRET saknas i miljövariabler');
return secret;
})(),
signOptions: { expiresIn: '7d' },
}),
],
+3 -1
View File
@@ -5,10 +5,12 @@ import { ExtractJwt, Strategy } from 'passport-jwt';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
const secret = process.env.JWT_SECRET;
if (!secret) throw new Error('JWT_SECRET saknas i miljövariabler');
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: process.env.JWT_SECRET ?? 'changeme',
secretOrKey: secret,
});
}