From 79bf505797718ea8bec8d7f46e7c03b60eb58711 Mon Sep 17 00:00:00 2001 From: vishnu vinay Date: Tue, 28 Jan 2025 11:48:07 +0530 Subject: [PATCH 1/3] Task #227474 - Create API for handling sinup and signin of user and add user details in DB incase of signup --- src/auth/auth.controller.ts | 17 ++++++++- src/auth/auth.service.ts | 62 +++++++++++++++++++++++++++++-- src/common/utils/api-id.config.ts | 2 + 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 5dd5170..a291beb 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('/signin') + @UseGuards(JwtAuthGuard) + @ApiBasicAuth("access-token") + @ApiOperation({ summary: 'Signup or Signin with Google' }) + @ApiResponse({ status: 200, description: 'User signed in successfully' }) + @ApiResponse({ status: 201, description: 'User signed up successfully' }) + async signUpAndSignIn( + @Req() request: Request, + @Res() response: Response + ) { + return this.authService.signUpAndSignIn(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..54fdafc 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 signUpAndSignIn(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 signed in successfully', + HttpStatus.OK, + 'User signed in successfully' + ); + } + else { + const createUserDB = await this.userService.createUserInDatabase(request, userCreateDto, response); + return APIResponse.success( + response, + APIID.GOOGLE_SIGNUP, + createUserDB, + HttpStatus.CREATED, + 'User Signed Up 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", From 18e3f5822ed496138282363dd846ef92c2fe6840 Mon Sep 17 00:00:00 2001 From: vishnu vinay Date: Tue, 28 Jan 2025 15:20:45 +0530 Subject: [PATCH 2/3] api end point name change --- src/auth/auth.controller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index a291beb..c4d8e78 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -49,7 +49,7 @@ export class AuthController { return this.authService.login(authDto,response); } - @Post('/signin') + @Post('/validateAndRegister') @UseGuards(JwtAuthGuard) @ApiBasicAuth("access-token") @ApiOperation({ summary: 'Signup or Signin with Google' }) From 38498ce092525a808a23efe2e9fb6b533ac1a265 Mon Sep 17 00:00:00 2001 From: vishnu vinay Date: Tue, 28 Jan 2025 16:05:19 +0530 Subject: [PATCH 3/3] message changes for validateAndRegister --- src/auth/auth.controller.ts | 10 +++++----- src/auth/auth.service.ts | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index c4d8e78..a0f45fb 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -52,14 +52,14 @@ export class AuthController { @Post('/validateAndRegister') @UseGuards(JwtAuthGuard) @ApiBasicAuth("access-token") - @ApiOperation({ summary: 'Signup or Signin with Google' }) - @ApiResponse({ status: 200, description: 'User signed in successfully' }) - @ApiResponse({ status: 201, description: 'User signed up successfully' }) - async signUpAndSignIn( + @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.signUpAndSignIn(request, response); + return this.authService.validateAndRegister(request, response); } @UseFilters(new AllExceptionsFilter(APIID.USER_AUTH)) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 54fdafc..4bc60ba 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -30,7 +30,7 @@ export class AuthService { private userRepository: Repository, ) { } - async signUpAndSignIn(request :Request,response: Response) { + async validateAndRegister(request :Request,response: Response) { const decoded :any = jwt_decode(request.headers.authorization); const userCreateDto = new UserCreateDto({ userId: decoded.sub, @@ -52,9 +52,9 @@ export class AuthService { return APIResponse.success( response, APIID.GOOGLE_SIGNIN, - 'User signed in successfully', + 'User validated successfully', HttpStatus.OK, - 'User signed in successfully' + 'User validated successfully' ); } else { @@ -64,7 +64,7 @@ export class AuthService { APIID.GOOGLE_SIGNUP, createUserDB, HttpStatus.CREATED, - 'User Signed Up successfully' + 'User Registered successfully' ); }