Skip to content

Commit

Permalink
Merge pull request #18 from vishnuvinay89/all-saas-0.2-dev
Browse files Browse the repository at this point in the history
Task #227474 - Create API for handling sinup and signin of user and add user details in DB incase of signup
  • Loading branch information
sudeeppr1998 authored Jan 28, 2025
2 parents 93f8c8d + 38498ce commit 57a6555
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
ApiHeader,
ApiBasicAuth,
ApiOkResponse,
ApiOperation,
ApiResponse,
} from "@nestjs/swagger";
import {
Controller,
Expand All @@ -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")
Expand All @@ -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)
Expand Down
62 changes: 59 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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<User>,
) { }

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;
Expand Down
2 changes: 2 additions & 0 deletions src/common/utils/api-id.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 57a6555

Please sign in to comment.