diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 5dd5170..a0f45fb 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -5,6 +5,8 @@ import { ApiHeader, ApiBasicAuth, ApiOkResponse, + ApiOperation, + ApiResponse, } from "@nestjs/swagger"; import { Controller, @@ -30,7 +32,7 @@ import { AuthService } from "./auth.service"; import { JwtAuthGuard } from "src/common/guards/keycloak.guard"; import { APIID } from "src/common/utils/api-id.config"; import { AllExceptionsFilter } from "src/common/filters/exception.filter"; -import { Response } from "express"; +import { Request, Response } from "express"; @ApiTags("Auth") @Controller("auth") @@ -47,6 +49,19 @@ export class AuthController { return this.authService.login(authDto,response); } + @Post('/validateAndRegister') + @UseGuards(JwtAuthGuard) + @ApiBasicAuth("access-token") + @ApiOperation({ summary: 'validate And Register with Google' }) + @ApiResponse({ status: 200, description: 'User Validated successfully' }) + @ApiResponse({ status: 201, description: 'User Registered successfully' }) + async validateAndRegister( + @Req() request: Request, + @Res() response: Response + ) { + return this.authService.validateAndRegister(request, response); + } + @UseFilters(new AllExceptionsFilter(APIID.USER_AUTH)) @Get("/") @UseGuards(JwtAuthGuard) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index f39903c..4bc60ba 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -5,7 +5,12 @@ import jwt_decode from "jwt-decode"; import APIResponse from "src/common/responses/response"; import { KeycloakService } from "src/common/utils/keycloak.service"; import { APIID } from "src/common/utils/api-id.config"; -import { Response } from "express"; +import { User } from "src/user/entities/user-entity"; +import { Request, Response } from "express"; +import { UserCreateDto } from "src/user/dto/user-create.dto"; +import { PostgresUserService } from "src/adapters/postgres/user-adapter"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Repository } from "typeorm"; type LoginResponse = { @@ -19,9 +24,60 @@ export class AuthService { constructor( private readonly useradapter: UserAdapter, - private readonly keycloakService: KeycloakService - ) {} + private readonly keycloakService: KeycloakService, + private userService : PostgresUserService, + @InjectRepository(User) + private userRepository: Repository, + ) { } + async validateAndRegister(request :Request,response: Response) { + const decoded :any = jwt_decode(request.headers.authorization); + const userCreateDto = new UserCreateDto({ + userId: decoded.sub, + username: decoded.email, + email: decoded.email, + name: decoded.name, + tenantCohortRoleMapping: [], + customFields: [], + createdBy: decoded.sub, + updatedBy: decoded.sub, + }); + try { + const checkUserinDb = await this.userRepository.find({ + where: [ + { username: decoded.email,userId: decoded.sub }, + ], + }) + if(checkUserinDb.length>0) { + return APIResponse.success( + response, + APIID.GOOGLE_SIGNIN, + 'User validated successfully', + HttpStatus.OK, + 'User validated successfully' + ); + } + else { + const createUserDB = await this.userService.createUserInDatabase(request, userCreateDto, response); + return APIResponse.success( + response, + APIID.GOOGLE_SIGNUP, + createUserDB, + HttpStatus.CREATED, + 'User Registered successfully' + ); + } + + } catch (error) { + return APIResponse.error( + response, + APIID.GOOGLE_SIGNIN, + 'INTERNAL_SERVER_ERROR', + error.message || 'Internal Server Error', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } async login(authDto,response: Response) { const apiId = APIID.LOGIN; const { username, password } = authDto; diff --git a/src/common/utils/api-id.config.ts b/src/common/utils/api-id.config.ts index a61f502..49a841f 100644 --- a/src/common/utils/api-id.config.ts +++ b/src/common/utils/api-id.config.ts @@ -37,6 +37,8 @@ export const APIID = { FIELDVALUES_CREATE: "api.fieldValues.create", FIELDVALUES_SEARCH: "api.fieldValues.search", FIELD_OPTIONS_DELETE: "api.fields.options.delete", + GOOGLE_SIGNIN :"api.google.signin", + GOOGLE_SIGNUP :"api.google.signup", LOGIN: "api.login", LOGOUT: "api.logout", REFRESH: "api.refresh",