70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import { Controller, Post, Body, Get, Param, UseGuards, Request } from '@nestjs/common';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { AuthService } from '../../../application/services/auth.service';
|
|
|
|
@Controller('api/v1/auth')
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) {}
|
|
|
|
@Post('login')
|
|
async login(@Body() body: { email: string; password: string }) {
|
|
return this.authService.login(body.email, body.password);
|
|
}
|
|
|
|
/**
|
|
* Register a new user.
|
|
* If companyName is provided, creates a new tenant (self-service registration).
|
|
* Otherwise joins the default tenant as viewer.
|
|
*/
|
|
@Post('register')
|
|
async register(
|
|
@Body() body: { email: string; password: string; name: string; companyName?: string },
|
|
) {
|
|
return this.authService.register(
|
|
body.email,
|
|
body.password,
|
|
body.name,
|
|
body.companyName,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Validate an invitation token (public endpoint).
|
|
*/
|
|
@Get('invite/:token')
|
|
async validateInvite(@Param('token') token: string) {
|
|
return this.authService.validateInvite(token);
|
|
}
|
|
|
|
/**
|
|
* Accept an invitation and create a user account (public endpoint).
|
|
*/
|
|
@Post('accept-invite')
|
|
async acceptInvite(
|
|
@Body() body: { token: string; password: string; name: string },
|
|
) {
|
|
return this.authService.acceptInvite(body.token, body.password, body.name);
|
|
}
|
|
|
|
@Get('profile')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
async getProfile(@Request() req: any) {
|
|
return req.user;
|
|
}
|
|
|
|
@Post('refresh')
|
|
async refreshToken(@Body() body: { refreshToken: string }) {
|
|
return this.authService.refreshToken(body.refreshToken);
|
|
}
|
|
|
|
@Post('api-keys')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
async createApiKey(@Request() req: any, @Body() body: { name: string }) {
|
|
return this.authService.createApiKey(
|
|
req.user.userId,
|
|
req.user.tenantId,
|
|
body.name,
|
|
);
|
|
}
|
|
}
|