1
0
forked from Cutlery/immich
Michel Heusschen c584791b65
feat(server): improve validation in controllers (#2149)
* feat(server): improve validation in controllers

* set ValidationPipe config with decorator
2023-04-02 23:24:18 -05:00

60 lines
1.7 KiB
TypeScript

import {
AuthUserDto,
LoginResponseDto,
OAuthCallbackDto,
OAuthConfigDto,
OAuthConfigResponseDto,
OAuthService,
UserResponseDto,
} from '@app/domain';
import { Body, Controller, Get, HttpStatus, Post, Redirect, Req, Res } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { GetAuthUser } from '../decorators/auth-user.decorator';
import { Authenticated } from '../decorators/authenticated.decorator';
import { UseValidation } from '../decorators/use-validation.decorator';
@ApiTags('OAuth')
@Controller('oauth')
@UseValidation()
export class OAuthController {
constructor(private service: OAuthService) {}
@Get('mobile-redirect')
@Redirect()
mobileRedirect(@Req() req: Request) {
return {
url: this.service.getMobileRedirect(req.url),
statusCode: HttpStatus.TEMPORARY_REDIRECT,
};
}
@Post('config')
generateConfig(@Body() dto: OAuthConfigDto): Promise<OAuthConfigResponseDto> {
return this.service.generateConfig(dto);
}
@Post('callback')
async callback(
@Res({ passthrough: true }) res: Response,
@Body() dto: OAuthCallbackDto,
@Req() req: Request,
): Promise<LoginResponseDto> {
const { response, cookie } = await this.service.login(dto, req.secure);
res.header('Set-Cookie', cookie);
return response;
}
@Authenticated()
@Post('link')
link(@GetAuthUser() authUser: AuthUserDto, @Body() dto: OAuthCallbackDto): Promise<UserResponseDto> {
return this.service.link(authUser, dto);
}
@Authenticated()
@Post('unlink')
unlink(@GetAuthUser() authUser: AuthUserDto): Promise<UserResponseDto> {
return this.service.unlink(authUser);
}
}