Files
keyvault/api/src/modules/auth/auth.controller.ts
2026-02-20 13:17:58 +01:00

47 lines
1.1 KiB
TypeScript

import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Post,
Req,
UseGuards,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthCodeDto } from 'src/model/dto';
import { User } from 'src/model/entitites';
import { AuthGuard } from 'src/core/guards/auth.guard';
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
@Post('auth-code')
async registerOrLoginWithAuthCode(
@Body() authDto: AuthCodeDto,
): Promise<User> {
const user = await this.authService.registerOrLoginWithAuthCode(authDto);
if (user == null) {
throw new HttpException('forbidden', HttpStatus.FORBIDDEN);
}
return user;
}
@UseGuards(AuthGuard)
@Get('me')
getMe(@Req() req: AuthenticatedRequest) {
return req.user;
}
@Post('refresh')
async getNewAccessToken(@Body() b: any) {
if (b.refreshToken) {
return this.authService.getNewToken(b.refreshToken);
}
throw new HttpException('no token', HttpStatus.BAD_REQUEST);
}
}