import { Controller, Post, Get, Body, Headers, UnauthorizedException } from '@nestjs/common'; import { AuthService } from './auth.service'; import { IsEmail, IsString, MinLength, IsOptional } from 'class-validator'; class SignUpDto { @IsEmail() email: string; @IsString() @MinLength(6) password: string; @IsOptional() @IsString() name?: string; } class SignInDto { @IsEmail() email: string; @IsString() password: string; } @Controller('auth') export class AuthController { constructor(private authService: AuthService) {} @Post('signup') async signUp(@Body() body: SignUpDto) { const result = await this.authService.signUp(body.email, body.password, body.name); return { user: { id: result.user.id, email: result.user.email, name: result.user.name, }, token: result.token, }; } @Post('signin') async signIn(@Body() body: SignInDto) { const result = await this.authService.signIn(body.email, body.password); return { user: { id: result.user.id, email: result.user.email, name: result.user.name, }, token: result.token, }; } @Post('signout') async signOut(@Headers('authorization') authHeader: string) { if (!authHeader || !authHeader.startsWith('Bearer ')) { throw new UnauthorizedException('No token provided'); } const token = authHeader.substring(7); await this.authService.signOut(token); return { success: true }; } @Get('session') async getSession(@Headers('authorization') authHeader: string) { if (!authHeader || !authHeader.startsWith('Bearer ')) { throw new UnauthorizedException('No token provided'); } const token = authHeader.substring(7); const session = await this.authService.getSession(token); if (!session) { throw new UnauthorizedException('Invalid or expired session'); } return { user: { id: session.user.id, email: session.user.email, name: session.user.name, }, }; } }