app/apps/backend/src/app/auth/auth.controller.ts
Mathis d5370220a4
Improve code consistency and formatting
Standardize import order and formatting across DTOs and controllers. Ensure `HttpCode` usage aligns with correct HTTP status codes. Add CORS setup to `main.ts` for local development support.
2024-10-24 16:14:40 +02:00

80 lines
1.8 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Patch,
Post,
UnauthorizedException,
UseGuards,
} from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { SignInDto, SignUpDto } from "apps/backend/src/app/auth/auth.dto";
import { AuthService } from "apps/backend/src/app/auth/auth.service";
import { UserGuard } from "./auth.guard";
@ApiTags("User authentification")
@Controller("auth")
export class AuthController {
constructor(private readonly authService: AuthService) {}
//TODO Initial account validation for admin privileges
//POST signup
@HttpCode(HttpStatus.CREATED)
@Post("signup")
async signUp(@Body() dto: SignUpDto) {
return this.authService.doRegister(dto);
}
//POST signin
@HttpCode(HttpStatus.OK)
@Post("signin")
async signIn(@Body() dto: SignInDto) {
return this.authService.doLogin(dto);
}
//GET me -- Get current user data via jwt
@ApiBearerAuth()
@HttpCode(HttpStatus.OK)
@Get("me")
@UseGuards(UserGuard)
async getMe(@Body() body: object) {
// @ts-ignore
const targetId = body.sourceUserId;
const userData = await this.authService.fetchUserById(targetId);
if (!userData) {
throw new UnauthorizedException();
}
return userData;
}
//DELETE me
@ApiBearerAuth()
@HttpCode(HttpStatus.FOUND)
@Delete("me")
@UseGuards(UserGuard)
async deleteMe(@Body() body: object) {
// @ts-ignore
const targetId = body.sourceUserId;
try {
await this.authService.deleteUser(targetId);
} catch (err) {
throw new UnauthorizedException();
}
}
/*
//PATCH me
@HttpCode(HttpStatus.OK)
@Patch("me")
@UseGuards(UserGuard)
async patchMe(@Body() body: UpdateUserDto) {
console.log(body);
// @ts-ignore
const targetId = body.sourceUserId;
await this.authService.updateUser(targetId, body);
return await this.authService.fetchUserById(targetId)
}
*/
}