All files / src/modules/api/auth auth.controller.ts

0% Statements 0/10
0% Branches 0/2
0% Functions 0/4
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                                       
import { AuthUser } from "@/common/decorators/AuthUser.decorator";
import { DiscordAuthGuard } from "@/common/guards/Discord.guard";
import { JwtAuthGuard } from "@/common/guards/Jwt.guard";
import { Config } from "@/modules/shared/config/types";
import { UserEntity } from "@/modules/shared/database/entities";
import { Cookies, Routes, Services, isInProduction } from "@/types/Constants";
import { Controller, Get, HttpCode, HttpStatus, Inject, Redirect, Req, Res, UseGuards } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { ApiTags } from "@nestjs/swagger";
import { FastifyReply, FastifyRequest } from "fastify";
import { IAuthService } from "./interfaces/IAuthService.interface";
 
@ApiTags(Routes.Auth)
@Controller(Routes.Auth)
export class AuthController {
	public constructor(
		@Inject(Services.Auth) private readonly authService: IAuthService,
		private readonly configService: ConfigService,
	) {}
 
	@Get("login")
	@UseGuards(DiscordAuthGuard)
	public login(): void {}
 
	@Get("redirect")
	@HttpCode(HttpStatus.CREATED)
	@UseGuards(DiscordAuthGuard)
	@Redirect(isInProduction ? "http://localhost:4401/@me" : "http://localhost:4400/api/auth/status")
	public async redirect(@Req() req: FastifyRequest, @Res() res: FastifyReply): Promise<void> {
		const jwt = await this.authService.login(req.user);
		res.cookie(Cookies.JWT, jwt, {
			httpOnly: true,
			maxAge: this.configService.getOrThrow<Config["API"]>("API").MaxAge,
			path: "/",
		});
	}
 
	@Get("status")
	@UseGuards(JwtAuthGuard)
	public status(@AuthUser() user: UserEntity): UserEntity {
		return user;
	}
}