BE/src/auth/auth.controller.ts
2025-06-18 17:15:21 +05:30

103 lines
3.3 KiB
TypeScript

import { Body, Controller, Get, HttpCode, Post, Put, Req, Res, UseInterceptors } from '@nestjs/common';
import { AuthService } from './auth.service';
import { ApiTags } from '@nestjs/swagger';
import { AuthLoginDTO } from './auth.dto';
import { Request, Response } from 'express';
import { LogoutInterceptor } from 'src/interceptors/logout.interceptor';
@ApiTags('Auth')
@Controller()
export class AuthController {
constructor(private readonly authService: AuthService) { }
// @Post('/login')
// login(@Body() body: AuthLoginDTO) {
// return this.authService.login(body);
// }
@Post('login')
@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: true, // set to true if you're on HTTPS
sameSite: 'none', // 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: true, // set to true if you're on HTTPS
sameSite: 'none', // 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')
async register(@Body() body: AuthLoginDTO, @Req() req: Request) {
return this.authService.registerUser(body, req);
}
@UseInterceptors(LogoutInterceptor)
@HttpCode(200)
@Post('logout')
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')
async forgotPassword() {
return this.authService.forgotPassword();
}
@Get('refresh-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: true, // set to true if you're on HTTPS
sameSite: 'none', // 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: true, // set to true if you're on HTTPS
sameSite: 'none', // 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" }
}
// @Get('checkRoles')
// @UseGuards(JwtAuthGuard, RolesGuard)
// @Roles('ca')
roleChecking() {
return { message: "role checking successfull..." }
}
}