Backend NestJS/Prisma, application mobile Expo (Tap to Pay Stripe), tableau de bord web Vite et documentation (cadrage en brouillon, décisions d'architecture). Secrets et fichiers locaux exclus par .gitignore. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import { Body, Controller, Get, HttpCode, HttpStatus, Patch, Post, Put, Req } from '@nestjs/common';
|
|
import { Throttle } from '@nestjs/throttler';
|
|
import type { Request } from 'express';
|
|
import { AuthUser, CurrentUser } from '../common/current-user.decorator';
|
|
import { Public } from '../common/public.decorator';
|
|
import { AuthService, ClientContext } from './auth.service';
|
|
import { ChangePasswordDto } from './dto/change-password.dto';
|
|
import { ForgotPasswordDto } from './dto/forgot-password.dto';
|
|
import { UpdateProfileDto } from './dto/update-profile.dto';
|
|
import { LoginDto } from './dto/login.dto';
|
|
import { RefreshDto } from './dto/refresh.dto';
|
|
import { RegisterDto } from './dto/register.dto';
|
|
import { ResetPasswordDto } from './dto/reset-password.dto';
|
|
|
|
const clientContext = (req: Request): ClientContext => ({
|
|
ip: req.ip,
|
|
userAgent: req.get('user-agent'),
|
|
});
|
|
|
|
// Limites strictes (par IP) sur les routes exposées au brute-force.
|
|
const STRICT = { default: { limit: 5, ttl: 60_000 } };
|
|
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(private readonly auth: AuthService) {}
|
|
|
|
@Public()
|
|
@Throttle(STRICT)
|
|
@Post('register')
|
|
register(@Body() dto: RegisterDto, @Req() req: Request) {
|
|
return this.auth.register(dto, clientContext(req));
|
|
}
|
|
|
|
@Public()
|
|
@Throttle(STRICT)
|
|
@Post('login')
|
|
@HttpCode(HttpStatus.OK)
|
|
login(@Body() dto: LoginDto, @Req() req: Request) {
|
|
return this.auth.login(dto, clientContext(req));
|
|
}
|
|
|
|
@Public()
|
|
@Throttle({ default: { limit: 20, ttl: 60_000 } })
|
|
@Post('refresh')
|
|
@HttpCode(HttpStatus.OK)
|
|
refresh(@Body() dto: RefreshDto, @Req() req: Request) {
|
|
return this.auth.refresh(dto.refreshToken, clientContext(req));
|
|
}
|
|
|
|
@Post('logout')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
logout(@Body() dto: RefreshDto, @CurrentUser() user: AuthUser) {
|
|
return this.auth.logout(dto.refreshToken, user.id);
|
|
}
|
|
|
|
@Public()
|
|
@Throttle({ default: { limit: 3, ttl: 60_000 } })
|
|
@Post('forgot-password')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
forgotPassword(@Body() dto: ForgotPasswordDto, @Req() req: Request) {
|
|
return this.auth.forgotPassword(dto.email, clientContext(req));
|
|
}
|
|
|
|
@Public()
|
|
@Throttle(STRICT)
|
|
@Post('reset-password')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
resetPassword(@Body() dto: ResetPasswordDto, @Req() req: Request) {
|
|
return this.auth.resetPassword(dto.token, dto.password, clientContext(req));
|
|
}
|
|
|
|
@Patch('me')
|
|
updateProfile(@CurrentUser() user: AuthUser, @Body() dto: UpdateProfileDto, @Req() req: Request) {
|
|
return this.auth.updateProfile(user.id, dto, req.ip);
|
|
}
|
|
|
|
@Put('password')
|
|
@Throttle(STRICT)
|
|
changePassword(@CurrentUser() user: AuthUser, @Body() dto: ChangePasswordDto, @Req() req: Request) {
|
|
return this.auth.changePassword(user.id, dto.currentPassword, dto.newPassword, clientContext(req));
|
|
}
|
|
|
|
@Get('me')
|
|
me(@CurrentUser() user: AuthUser) {
|
|
return this.auth.me(user.id);
|
|
}
|
|
}
|