Files
swiss-datashare/backend/src/auth/auth.controller.ts

89 lines
2.7 KiB
TypeScript
Raw Normal View History

import {
Body,
Controller,
ForbiddenException,
HttpCode,
2022-12-05 15:53:24 +01:00
Patch,
Post,
2022-12-05 15:53:24 +01:00
UseGuards,
} from "@nestjs/common";
2022-10-24 12:11:10 +02:00
import { Throttle } from "@nestjs/throttler";
2022-12-05 15:53:24 +01:00
import { User } from "@prisma/client";
import { ConfigService } from "src/config/config.service";
import { AuthService } from "./auth.service";
import { AuthTotpService } from "./authTotp.service";
2022-12-05 15:53:24 +01:00
import { GetUser } from "./decorator/getUser.decorator";
import { AuthRegisterDTO } from "./dto/authRegister.dto";
2022-10-10 17:58:42 +02:00
import { AuthSignInDTO } from "./dto/authSignIn.dto";
import { AuthSignInTotpDTO } from "./dto/authSignInTotp.dto";
import { EnableTotpDTO } from "./dto/enableTotp.dto";
import { RefreshAccessTokenDTO } from "./dto/refreshAccessToken.dto";
2022-12-05 15:53:24 +01:00
import { UpdatePasswordDTO } from "./dto/updatePassword.dto";
import { VerifyTotpDTO } from "./dto/verifyTotp.dto";
2022-12-05 15:53:24 +01:00
import { JwtGuard } from "./guard/jwt.guard";
@Controller("auth")
export class AuthController {
constructor(
private authService: AuthService,
private authTotpService: AuthTotpService,
private config: ConfigService
) {}
2022-10-24 12:11:10 +02:00
@Throttle(10, 5 * 60)
@Post("signUp")
async signUp(@Body() dto: AuthRegisterDTO) {
if (!this.config.get("ALLOW_REGISTRATION"))
throw new ForbiddenException("Registration is not allowed");
return this.authService.signUp(dto);
}
2022-10-24 12:11:10 +02:00
@Throttle(10, 5 * 60)
@Post("signIn")
2022-10-13 23:23:33 +02:00
@HttpCode(200)
2022-10-10 17:58:42 +02:00
signIn(@Body() dto: AuthSignInDTO) {
return this.authService.signIn(dto);
}
@Throttle(10, 5 * 60)
@Post("signIn/totp")
@HttpCode(200)
signInTotp(@Body() dto: AuthSignInTotpDTO) {
return this.authTotpService.signInTotp(dto);
}
2022-12-05 15:53:24 +01:00
@Patch("password")
@UseGuards(JwtGuard)
async updatePassword(@GetUser() user: User, @Body() dto: UpdatePasswordDTO) {
await this.authService.updatePassword(user, dto.oldPassword, dto.password);
}
@Post("token")
@HttpCode(200)
async refreshAccessToken(@Body() body: RefreshAccessTokenDTO) {
const accessToken = await this.authService.refreshAccessToken(
body.refreshToken
);
return { accessToken };
}
@Post("totp/enable")
@UseGuards(JwtGuard)
async enableTotp(@GetUser() user: User, @Body() body: EnableTotpDTO) {
return this.authTotpService.enableTotp(user, body.password);
}
@Post("totp/verify")
@UseGuards(JwtGuard)
async verifyTotp(@GetUser() user: User, @Body() body: VerifyTotpDTO) {
return this.authTotpService.verifyTotp(user, body.password, body.code);
}
@Post("totp/disable")
@UseGuards(JwtGuard)
async disableTotp(@GetUser() user: User, @Body() body: VerifyTotpDTO) {
// Note: We use VerifyTotpDTO here because it has both fields we need: password and totp code
return this.authTotpService.disableTotp(user, body.password, body.code);
}
}