Skip to content

Commit

Permalink
0.24.0 (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik committed Aug 16, 2024
2 parents 1cf1f54 + 3791833 commit ce5d7fb
Show file tree
Hide file tree
Showing 129 changed files with 2,157 additions and 791 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"arrowParens": "always",
"endOfLine": "lf"
"endOfLine": "auto"
}
8 changes: 4 additions & 4 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@impler/api",
"version": "0.23.1",
"version": "0.24.0",
"author": "implerhq",
"license": "MIT",
"private": true,
Expand All @@ -19,9 +19,9 @@
"test": "cross-env TZ=UTC NODE_ENV=test E2E_RUNNER=true mocha --timeout 10000 --require ts-node/register --exit src/**/**/*.spec.ts"
},
"dependencies": {
"@impler/dal": "^0.23.1",
"@impler/services": "^0.23.1",
"@impler/shared": "^0.23.1",
"@impler/dal": "^0.24.0",
"@impler/services": "^0.24.0",
"@impler/shared": "^0.24.0",
"@nestjs/common": "^9.1.2",
"@nestjs/core": "^9.1.2",
"@nestjs/jwt": "^10.0.1",
Expand Down
88 changes: 84 additions & 4 deletions apps/api/src/app/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,38 @@ import {
Controller,
Get,
Post,
Put,
Res,
UseGuards,
UseInterceptors,
} from '@nestjs/common';

import { IJwtPayload } from '@impler/shared';
import { AuthService } from './services/auth.service';
import { IStrategyResponse } from '@shared/types/auth.types';
import { CONSTANTS, COOKIE_CONFIG } from '@shared/constants';
import { UserSession } from '@shared/framework/user.decorator';
import { ApiException } from '@shared/exceptions/api.exception';
import { StrategyUser } from './decorators/strategy-user.decorator';
import { RegisterUserDto, LoginUserDto, RequestForgotPasswordDto, ResetPasswordDto } from './dtos';
import {
RegisterUser,
RegisterUserCommand,
RegisterUserDto,
LoginUserDto,
RequestForgotPasswordDto,
ResetPasswordDto,
OnboardUserDto,
VerifyDto,
UpdateUserDto,
} from './dtos';
import {
Verify,
LoginUser,
ResendOTP,
UpdateUser,
OnboardUser,
RegisterUser,
ResetPassword,
LoginUserCommand,
RegisterUserCommand,
ResetPasswordCommand,
RequestForgotPassword,
RequestForgotPasswordCommand,
Expand All @@ -36,8 +50,13 @@ import {
@UseInterceptors(ClassSerializerInterceptor)
export class AuthController {
constructor(
private registerUser: RegisterUser,
private verify: Verify,
private resendOTP: ResendOTP,
private loginUser: LoginUser,
private updateUser: UpdateUser,
private onboardUser: OnboardUser,
private authService: AuthService,
private registerUser: RegisterUser,
private resetPassword: ResetPassword,
private requestForgotPassword: RequestForgotPassword
) {}
Expand Down Expand Up @@ -88,6 +107,18 @@ export class AuthController {
return user;
}

@Put('/me')
async updateUserRoute(@UserSession() user: IJwtPayload, @Body() body: UpdateUserDto, @Res() response: Response) {
const { success, token } = await this.updateUser.execute(user._id, body);
if (token)
response.cookie(CONSTANTS.AUTH_COOKIE_NAME, token, {
...COOKIE_CONFIG,
domain: process.env.COOKIE_DOMAIN,
});

response.send({ success });
}

@Get('/logout')
logout(@Res() response: Response) {
response.clearCookie(CONSTANTS.AUTH_COOKIE_NAME, {
Expand All @@ -112,6 +143,50 @@ export class AuthController {
response.send(registeredUser);
}

@Post('/verify')
async verifyRoute(@Body() body: VerifyDto, @UserSession() user: IJwtPayload, @Res() response: Response) {
const { token, screen } = await this.verify.execute(user._id, { code: body.otp });
response.cookie(CONSTANTS.AUTH_COOKIE_NAME, token, {
...COOKIE_CONFIG,
domain: process.env.COOKIE_DOMAIN,
});

response.send({ screen });
}

@Post('/onboard')
async onboardUserRoute(
@Body() body: OnboardUserDto,
@UserSession() user: IJwtPayload,
@Res({ passthrough: true }) res: Response
) {
const projectWithEnvironment = await this.onboardUser.execute({
_userId: user._id,
projectName: body.projectName,
role: body.role,
companySize: body.companySize,
source: body.source,
});
const token = this.authService.getSignedToken(
{
_id: user._id,
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
profilePicture: user.profilePicture,
isEmailVerified: user.isEmailVerified,
accessToken: projectWithEnvironment.environment.apiKeys[0].key,
},
projectWithEnvironment.project._id
);
res.cookie(CONSTANTS.AUTH_COOKIE_NAME, token, {
...COOKIE_CONFIG,
domain: process.env.COOKIE_DOMAIN,
});

return projectWithEnvironment;
}

@Post('/login')
async login(@Body() body: LoginUserDto, @Res() response: Response) {
const loginUser = await this.loginUser.execute(
Expand Down Expand Up @@ -145,4 +220,9 @@ export class AuthController {

response.send(resetPassword);
}

@Post('verify/resend')
async resendOTPRoute(@UserSession() user: IJwtPayload) {
return await this.resendOTP.execute(user._id);
}
}
15 changes: 8 additions & 7 deletions apps/api/src/app/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Global, MiddlewareConsumer, Module, NestModule, Provider, RequestMethod } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import * as passport from 'passport';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { Global, MiddlewareConsumer, Module, NestModule, Provider, RequestMethod } from '@nestjs/common';

import { USE_CASES } from './usecases';
import { CONSTANTS } from '@shared/constants';
import { PassportModule } from '@nestjs/passport';
import { AuthController } from './auth.controller';
import { PaymentAPIService } from '@impler/services';
import { AuthService } from './services/auth.service';
import { SharedModule } from '../shared/shared.module';
import { GitHubStrategy } from './services/passport/github.strategy';
import { JwtStrategy } from './services/passport/jwt.strategy';
import { LeadService } from '@shared/services/lead.service';
import { PaymentAPIService } from '@impler/services';
import { JwtStrategy } from './services/passport/jwt.strategy';
import { GitHubStrategy } from './services/passport/github.strategy';

const AUTH_STRATEGIES: Provider[] = [JwtStrategy];

Expand All @@ -33,7 +34,7 @@ if (process.env.GITHUB_OAUTH_CLIENT_ID) {
}),
],
controllers: [AuthController],
providers: [AuthService, ...AUTH_STRATEGIES, ...USE_CASES, LeadService, PaymentAPIService],
providers: [AuthService, LeadService, ...AUTH_STRATEGIES, ...USE_CASES, PaymentAPIService],
exports: [AuthService],
})
export class AuthModule implements NestModule {
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/app/auth/dtos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export * from './register-user.dto';
export * from './login-user.dto';
export * from './request-forgot-password.dto';
export * from './reset-password.dto';
export * from './onboard-user.dto';
export * from './verify.dto';
export * from './update-user.dto';
32 changes: 32 additions & 0 deletions apps/api/src/app/auth/dtos/onboard-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IsDefined, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class OnboardUserDto {
@ApiProperty({
description: 'Size of the company',
})
@IsString()
@IsDefined()
companySize: string;

@ApiProperty({
description: 'Role of the user',
})
@IsString()
@IsDefined()
role: string;

@ApiProperty({
description: 'Source from where the user heard about us',
})
@IsString()
@IsDefined()
source: string;

@ApiProperty({
description: 'Name of the Project',
})
@IsString()
@IsDefined()
projectName: string;
}
11 changes: 11 additions & 0 deletions apps/api/src/app/auth/dtos/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsOptional } from 'class-validator';

export class UpdateUserDto {
@ApiProperty({
description: 'Email of the user',
})
@IsEmail()
@IsOptional()
email?: string;
}
7 changes: 7 additions & 0 deletions apps/api/src/app/auth/dtos/verify.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsDefined, IsString } from 'class-validator';

export class VerifyDto {
@IsString()
@IsDefined()
otp: string;
}
19 changes: 10 additions & 9 deletions apps/api/src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { JwtService } from '@nestjs/jwt';
import { Injectable, UnauthorizedException } from '@nestjs/common';

import { IJwtPayload } from '@impler/shared';
import { CONSTANTS } from '@shared/constants';
import { CONSTANTS, LEAD_SIGNUP_USING } from '@shared/constants';
import { PaymentAPIService } from '@impler/services';
import { LeadService } from '@shared/services/lead.service';
import { UserEntity, UserRepository, EnvironmentRepository } from '@impler/dal';
import { UserNotFoundException } from '@shared/exceptions/user-not-found.exception';
import { IAuthenticationData, IStrategyResponse } from '@shared/types/auth.types';
Expand All @@ -15,7 +14,6 @@ import { IncorrectLoginCredentials } from '@shared/exceptions/incorrect-login-cr
export class AuthService {
constructor(
private jwtService: JwtService,
private leadService: LeadService,
private userRepository: UserRepository,
private environmentRepository: EnvironmentRepository,
private paymentAPIService: PaymentAPIService
Expand All @@ -30,18 +28,14 @@ export class AuthService {
if (!user) {
const userObj: Partial<UserEntity> = {
email: profile.email,
isEmailVerified: true,
firstName: profile.firstName,
lastName: profile.lastName,
signupMethod: LEAD_SIGNUP_USING.GITHUB,
profilePicture: profile.avatar_url,
...(provider ? { tokens: [provider] } : {}),
};
user = await this.userRepository.create(userObj);
await this.leadService.createLead({
'First Name': user.firstName,
'Last Name': user.lastName,
'Lead Email': user.email,
'Lead Source': 'Github Signup',
});
userCreated = true;

const userData = {
Expand Down Expand Up @@ -71,6 +65,7 @@ export class AuthService {
lastName: user.lastName,
profilePicture: user.profilePicture,
accessToken: apiKey?.apiKey,
isEmailVerified: user.isEmailVerified,
},
apiKey?.projectId
),
Expand Down Expand Up @@ -104,6 +99,7 @@ export class AuthService {
firstName: user.firstName,
lastName: user.lastName,
accessToken: apiKey?.apiKey,
isEmailVerified: user.isEmailVerified,
},
apiKey?.projectId
),
Expand All @@ -123,6 +119,7 @@ export class AuthService {
firstName: user.firstName,
lastName: user.lastName,
accessToken: apiKey?.apiKey,
isEmailVerified: user.isEmailVerified,
},
apiKey?.projectId
);
Expand All @@ -134,6 +131,7 @@ export class AuthService {
firstName: string;
lastName: string;
email: string;
isEmailVerified: boolean;
profilePicture?: string;
accessToken?: string;
},
Expand All @@ -148,6 +146,7 @@ export class AuthService {
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
isEmailVerified: user.isEmailVerified,
profilePicture: user.profilePicture,
accessToken: user.accessToken,
},
Expand Down Expand Up @@ -196,6 +195,7 @@ export class AuthService {
firstName: user.firstName,
lastName: user.lastName,
accessToken: apiKey,
isEmailVerified: user.isEmailVerified,
},
environment._projectId
);
Expand All @@ -211,6 +211,7 @@ export class AuthService {
firstName: user.firstName,
lastName: user.lastName,
accessToken: apiKey?.apiKey,
isEmailVerified: user.isEmailVerified,
},
apiKey?.projectId
);
Expand Down
Loading

0 comments on commit ce5d7fb

Please sign in to comment.