Skip to content

Commit

Permalink
Merge pull request #7 from DFanso/feat/auth
Browse files Browse the repository at this point in the history
fix: remove unused codes
  • Loading branch information
DFanso authored Feb 27, 2024
2 parents 6c735b8 + 4e87c12 commit ebb5328
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/auth/CognitoService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable no-console */
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
CognitoIdentityProviderClient,
ConfirmSignUpCommand,
SignUpCommand,
InitiateAuthCommand,
ForgotPasswordCommand, ConfirmForgotPasswordCommand
ForgotPasswordCommand,
ConfirmForgotPasswordCommand,
} from '@aws-sdk/client-cognito-identity-provider';
import { fromEnv } from '@aws-sdk/credential-provider-env';
import * as crypto from 'crypto';
Expand All @@ -21,7 +23,10 @@ export class CognitoService {
private clientSecret: string;
private userPoolId: string;

constructor(private configService: ConfigService, private readonly authService: AuthService,) {
constructor(
private configService: ConfigService,
private readonly authService: AuthService,
) {
this.clientId = this.configService.get<string>('COGNITO_CLIENT_ID');
this.userPoolId = this.configService.get<string>('COGNITO_USER_POOL_ID');
this.clientSecret = this.configService.get<string>('COGNITO_CLIENT_SECRET');
Expand All @@ -39,10 +44,11 @@ export class CognitoService {
}

async registerUser(createUserDto: CreateUserDto): Promise<any> {

if (createUserDto.type === UserType.Labor) {
if (!createUserDto.aboutMe || !createUserDto.services) {
throw new Error('AboutMe and services are required for users of type LABOR.');
throw new Error(
'AboutMe and services are required for users of type LABOR.',
);
}
}
const secretHash = this.generateSecretHash(createUserDto.email);
Expand All @@ -55,28 +61,26 @@ export class CognitoService {
{
Name: 'email',
Value: createUserDto.email,

},

{
Name: 'picture',
Value: createUserDto.profileImage
},
{
Name: 'given_name',
Value: createUserDto.firstName
},
{
Name: 'family_name',
Value: createUserDto.lastName
},
{
Name: 'picture',
Value: createUserDto.profileImage,
},
{
Name: 'given_name',
Value: createUserDto.firstName,
},
{
Name: 'family_name',
Value: createUserDto.lastName,
},
],
};

try {
const data = await this.client.send(new SignUpCommand(params));
console.log('Registration successful:', data);


createUserDto.userId = data.UserSub;
console.log(createUserDto);
Expand Down Expand Up @@ -148,7 +152,7 @@ export class CognitoService {
try {
const data = await this.client.send(new ConfirmSignUpCommand(params));
console.log('Email verification successful:', data);
const user = await this.authService.verify(verifyEmailDto.email)
await this.authService.verify(verifyEmailDto.email);
return data;
} catch (err) {
console.error('Error during email verification:', err);
Expand Down Expand Up @@ -190,7 +194,11 @@ export class CognitoService {
}
}

async confirmForgotPassword(email: string, confirmationCode: string, newPassword: string): Promise<any> {
async confirmForgotPassword(
email: string,
confirmationCode: string,
newPassword: string,
): Promise<any> {
const secretHash = this.generateSecretHash(email);
const params = {
ClientId: this.clientId,
Expand All @@ -201,14 +209,19 @@ export class CognitoService {
};

try {
const data = await this.client.send(new ConfirmForgotPasswordCommand(params));
const data = await this.client.send(
new ConfirmForgotPasswordCommand(params),
);
console.log('Password reset successful:', data);
return {
message: 'Password has been successfully reset.',
};
} catch (err) {
console.error('Error confirming new password:', err);
if (err.name === 'CodeMismatchException' || err.name === 'ExpiredCodeException') {
if (
err.name === 'CodeMismatchException' ||
err.name === 'ExpiredCodeException'
) {
throw new HttpException(
'Invalid or expired verification code. Please try the reset process again.',
HttpStatus.BAD_REQUEST,
Expand All @@ -220,5 +233,4 @@ export class CognitoService {
);
}
}

}

0 comments on commit ebb5328

Please sign in to comment.