2024-03-20 19:32:04 +01:00
|
|
|
import { Body, Controller, Get, HttpStatus, Post, Redirect, Req, Res } from '@nestjs/common';
|
|
|
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
|
|
|
import { Request, Response } from 'express';
|
2023-01-23 23:13:42 -05:00
|
|
|
import {
|
2023-12-09 23:34:12 -05:00
|
|
|
AuthDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
LoginResponseDto,
|
2023-09-01 07:08:42 -04:00
|
|
|
OAuthAuthorizeResponseDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
OAuthCallbackDto,
|
|
|
|
|
OAuthConfigDto,
|
2024-03-20 23:53:07 +01:00
|
|
|
} from 'src/dtos/auth.dto';
|
|
|
|
|
import { UserResponseDto } from 'src/dtos/user.dto';
|
2024-03-20 15:15:01 -05:00
|
|
|
import { Auth, Authenticated, GetLoginDetails, PublicRoute } from 'src/middleware/auth.guard';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { AuthService, LoginDetails } from 'src/services/auth.service';
|
2022-11-14 21:24:25 -05:00
|
|
|
|
|
|
|
|
@ApiTags('OAuth')
|
|
|
|
|
@Controller('oauth')
|
2023-05-28 12:30:01 -04:00
|
|
|
@Authenticated()
|
2022-11-14 21:24:25 -05:00
|
|
|
export class OAuthController {
|
2023-07-15 00:03:56 -04:00
|
|
|
constructor(private service: AuthService) {}
|
2022-11-14 21:24:25 -05:00
|
|
|
|
2023-05-28 12:30:01 -04:00
|
|
|
@PublicRoute()
|
2022-12-29 15:47:30 -05:00
|
|
|
@Get('mobile-redirect')
|
|
|
|
|
@Redirect()
|
2024-02-02 04:18:00 +01:00
|
|
|
redirectOAuthToMobile(@Req() request: Request) {
|
2023-03-24 00:53:56 -04:00
|
|
|
return {
|
2024-02-02 04:18:00 +01:00
|
|
|
url: this.service.getMobileRedirect(request.url),
|
2023-03-24 00:53:56 -04:00
|
|
|
statusCode: HttpStatus.TEMPORARY_REDIRECT,
|
|
|
|
|
};
|
2022-12-29 15:47:30 -05:00
|
|
|
}
|
|
|
|
|
|
2023-09-01 07:08:42 -04:00
|
|
|
@PublicRoute()
|
|
|
|
|
@Post('authorize')
|
2023-11-03 21:33:15 -04:00
|
|
|
startOAuth(@Body() dto: OAuthConfigDto): Promise<OAuthAuthorizeResponseDto> {
|
2023-09-01 07:08:42 -04:00
|
|
|
return this.service.authorize(dto);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 12:30:01 -04:00
|
|
|
@PublicRoute()
|
2022-12-29 15:47:30 -05:00
|
|
|
@Post('callback')
|
2023-11-03 21:33:15 -04:00
|
|
|
async finishOAuth(
|
2023-01-23 23:13:42 -05:00
|
|
|
@Res({ passthrough: true }) res: Response,
|
2023-03-31 17:14:01 +02:00
|
|
|
@Body() dto: OAuthCallbackDto,
|
2023-04-25 22:19:23 -04:00
|
|
|
@GetLoginDetails() loginDetails: LoginDetails,
|
2022-11-20 11:43:10 -06:00
|
|
|
): Promise<LoginResponseDto> {
|
2023-07-15 00:03:56 -04:00
|
|
|
const { response, cookie } = await this.service.callback(dto, loginDetails);
|
2023-03-24 00:53:56 -04:00
|
|
|
res.header('Set-Cookie', cookie);
|
2023-01-23 23:13:42 -05:00
|
|
|
return response;
|
2022-11-14 21:24:25 -05:00
|
|
|
}
|
2022-12-26 10:35:52 -05:00
|
|
|
|
|
|
|
|
@Post('link')
|
2023-12-09 23:34:12 -05:00
|
|
|
linkOAuthAccount(@Auth() auth: AuthDto, @Body() dto: OAuthCallbackDto): Promise<UserResponseDto> {
|
|
|
|
|
return this.service.link(auth, dto);
|
2022-12-26 10:35:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('unlink')
|
2023-12-09 23:34:12 -05:00
|
|
|
unlinkOAuthAccount(@Auth() auth: AuthDto): Promise<UserResponseDto> {
|
|
|
|
|
return this.service.unlink(auth);
|
2022-12-26 10:35:52 -05:00
|
|
|
}
|
2022-11-14 21:24:25 -05:00
|
|
|
}
|