import { Body, Controller, Get, HttpCode, Post, Put, Req, Res } from '@nestjs/common'; import { AuthService } from './auth.service'; import { ApiTags } from '@nestjs/swagger'; import { AuthLoginDTO } from './auth.dto'; import { Request, Response } from 'express'; @Controller() export class AuthController { constructor(private readonly authService: AuthService) { } @ApiTags('Auth') @Post('/login') login(@Body() body: AuthLoginDTO) { return this.authService.login(body); } @Post('login-client') @HttpCode(200) async loginClient(@Body() body: AuthLoginDTO, @Res({ passthrough: true }) res: Response, @Req() req: Request) { let k: any = await this.authService.loginUser(body.P_EMAILADDR, body.P_PASSWORD, req); if (k.access_token) { res.cookie('access_token', k.access_token, { httpOnly: true, secure: false, // set to true if you're on HTTPS sameSite: 'lax', // adjust based on your needs (strict, lax , none) maxAge: 90 * 1000, // convert seconds to ms if applicable }); } if (k.refresh_token) { res.cookie('refresh_token', k.refresh_token, { httpOnly: true, secure: false, // set to true if you're on HTTPS sameSite: 'lax', // adjust based on your needs (strict, lax , none) maxAge: 90 * 1000, // convert seconds to ms if applicable }); } return { statusCode: 200, message: "Logged-In Successfully", email: k.email } } // @UseGuards(RegisterGuard) @Post('register-client') async register(@Body() body: AuthLoginDTO, @Req() req: Request) { return this.authService.registerUser(body, req); } // @UseInterceptors(LogoutInterceptor) @HttpCode(200) @Post('logout-client') async logout(@Req() req: any, @Res() res: Response) { const refreshToken = req.user?.refreshToken; let rst: any = await this.authService.logoutUser(refreshToken); if (rst.statusCode === 200) { res.clearCookie('access_token'); res.clearCookie('refresh_token'); } res.json({ ...rst }) } @Put('forgot-password-client') async forgotPassword() { return this.authService.forgotPassword(); } @Get('refresh-client-tokens') async getTokenFromRefreshToken(@Req() req: Request, @Res({ passthrough: true }) res: Response) { let k: any = await this.authService.getTokenFromRefreshToken(req) if (k.access_token) { res.cookie('access_token', k.access_token, { httpOnly: true, secure: false, // set to true if you're on HTTPS sameSite: 'lax', // adjust based on your needs (strict, lax , none) maxAge: 90 * 1000, // convert seconds to ms if applicable path: '/' }); } if (k.refresh_token) { res.cookie('refresh_token', k.refresh_token, { httpOnly: true, secure: false, // set to true if you're on HTTPS sameSite: 'lax', // adjust based on your needs (strict, lax , none) maxAge: 90 * 1000, // convert seconds to ms if applicable path: '/' }); } return { statusCode: 200, message: "Tokens refreshed successfully" } } }