120 lines
3.4 KiB
TypeScript
120 lines
3.4 KiB
TypeScript
import { Body, Controller, Get, HttpCode, Post, Put, Req, Res, UseGuards, UseInterceptors } from '@nestjs/common';
|
|
import { AuthService } from './auth.service';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
import { AuthLoginDTO, AuthLoginOnlyDTO, SendMailDTO } from './auth.dto';
|
|
import { Request, Response } from 'express';
|
|
import { LogoutInterceptor } from 'src/interceptors/logout.interceptor';
|
|
import { RegisterGuard } from 'src/guards/register.guard';
|
|
import { RolesGuard } from 'src/guards/roles.guard';
|
|
import { Roles } from 'src/decorators/roles.decorator';
|
|
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
|
|
|
|
@ApiTags('Auth')
|
|
@Controller("1")
|
|
export class AuthController {
|
|
constructor(
|
|
private readonly authService: AuthService,
|
|
) { }
|
|
|
|
@Post('login')
|
|
@HttpCode(200)
|
|
async loginClient(@Body() body: AuthLoginOnlyDTO, @Res({ passthrough: true }) res: Response, @Req() req: Request) {
|
|
|
|
let k: any = await this.authService.loginUser(body.P_EMAILADDR.toLowerCase(), body.P_PASSWORD, body.P_APPLICATIONNAME, req);
|
|
|
|
if (k.access_token) {
|
|
res.cookie('access_token', k.access_token, {
|
|
httpOnly: true,
|
|
secure: true,
|
|
sameSite: 'none'
|
|
});
|
|
}
|
|
|
|
if (k.refresh_token) {
|
|
res.cookie('refresh_token', k.refresh_token, {
|
|
httpOnly: true,
|
|
secure: true,
|
|
sameSite: 'none'
|
|
});
|
|
}
|
|
|
|
return { statusCode: 200, message: "Logged-In Successfully", email: k.email }
|
|
}
|
|
|
|
// @UseGuards(RegisterGuard)
|
|
@Post('register')
|
|
async register(@Body() body: AuthLoginDTO) {
|
|
return this.authService.registerUser(body);
|
|
}
|
|
|
|
@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', {
|
|
httpOnly: true,
|
|
secure: true,
|
|
sameSite: 'none'
|
|
});
|
|
res.clearCookie('refresh_token', {
|
|
httpOnly: true,
|
|
secure: true,
|
|
sameSite: 'none'
|
|
});
|
|
}
|
|
res.json({ ...rst })
|
|
}
|
|
|
|
@UseGuards(RegisterGuard)
|
|
@Put('forgot-password')
|
|
async forgotPassword(@Body() body: AuthLoginDTO, @Req() req: Request) {
|
|
return this.authService.forgotPassword(body, req);
|
|
}
|
|
|
|
@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,
|
|
sameSite: 'none',
|
|
maxAge: 8 * 60 * 60 * 1000,
|
|
path: '/'
|
|
});
|
|
}
|
|
|
|
if (k.refresh_token) {
|
|
res.cookie('refresh_token', k.refresh_token, {
|
|
httpOnly: true,
|
|
secure: true,
|
|
sameSite: 'none',
|
|
maxAge: 8 * 60 * 60 * 1000,
|
|
path: '/'
|
|
});
|
|
}
|
|
|
|
return { statusCode: 200, message: "Tokens refreshed successfully" }
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles('sa')
|
|
@Post('sendmail')
|
|
@HttpCode(200)
|
|
async sendMail(@Body() body: SendMailDTO) {
|
|
// let token = await this.jwtService.generateToken({ email: mail })
|
|
return this.authService.sendMail(body);
|
|
}
|
|
|
|
// @Get('checkRoles')
|
|
// @UseGuards(JwtAuthGuard, RolesGuard)
|
|
// @Roles('ca')
|
|
roleChecking() {
|
|
return { message: "role checking successfull..." }
|
|
}
|
|
}
|