Skip to content

Commit

Permalink
Remove redundant login calls
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelim01 committed Nov 13, 2024
1 parent c048ba0 commit 48066d2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,70 +88,48 @@ export class EditPasswordDialogComponent {
}

onPasswordSubmit() {
if (this.editPasswordForm.valid) {
this.isProcessingPassword = true;
if (!this.editPasswordForm.valid) {
return;
}

// Check if password is correct first
this.authenticationService
.login(this.user!.username, this.editPasswordForm.get('oldPassword')?.value)
.subscribe({
next: () => {
this.authenticationService
.updatePassword(
this.user!.username,
this.editPasswordForm.get('oldPassword')?.value,
this.editPasswordForm.get('password')?.value,
)
.subscribe({
next: () => {
// Reset states and forms on success
this.closeDialog();
this.isProcessingPassword = true;

this.messageService.add({
severity: 'success',
summary: 'Password Updated',
detail: 'Your password has been updated successfully!',
});
},
error: error => {
this.isProcessingPassword = false;
const status = error.cause.status;
let errorMessage = 'An unknown error occurred';
if (status === 401) {
errorMessage = 'Try loging out and log back in. Expired token';
} else if (status === 404) {
errorMessage = 'Try loging out and log back in. User ID Not Found';
} else if (status === 409) {
errorMessage = 'Username or Email already exists';
} else if (status === 500) {
errorMessage = 'Internal Server Error';
}
this.messageService.add({
severity: 'error',
summary: 'Editing Password Erorr',
detail: errorMessage,
});
},
});
},
error: error => {
this.isProcessingPassword = false;
const status = error.cause.status;
let errorMessage = 'An unknown error occurred';
if (status === 400) {
errorMessage = 'Missing/Invalid passwords';
} else if (status === 401) {
errorMessage = 'Your old password is invalid';
} else if (status === 500) {
errorMessage = 'Internal Server Error';
}
this.messageService.add({
severity: 'error',
summary: 'Submission Error',
detail: errorMessage,
});
},
});
}
this.authenticationService
.updatePassword(
this.user!.username,
this.editPasswordForm.get('oldPassword')?.value,
this.editPasswordForm.get('password')?.value,
)
.subscribe({
next: () => {
// Reset states and forms on success
this.closeDialog();

this.messageService.add({
severity: 'success',
summary: 'Password Updated',
detail: 'Your password has been updated successfully!',
});
},
error: error => {
this.isProcessingPassword = false;
const status = error.cause.status;
let errorMessage = 'An unknown error occurred';
if (status === 401) {
errorMessage = 'Try loging out and log back in. Expired token';
} else if (status === 404) {
errorMessage = 'Try loging out and log back in. User ID Not Found';
} else if (status === 409) {
errorMessage = 'Username or Email already exists';
} else if (status === 500) {
errorMessage = 'Internal Server Error';
}
this.messageService.add({
severity: 'error',
summary: 'Editing Password Erorr',
detail: errorMessage,
});
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,65 +61,43 @@ export class EditProfileDialogComponent {
}

onEditSubmit() {
if (this.editProfileForm.valid) {
this.isProcessingEdit = true;
if (!this.editProfileForm.valid) {
return;
}

// Check if password is correct first
this.authenticationService
.login(this.user!.username, this.editProfileForm.get('password')?.value)
.subscribe({
next: () => {
this.authenticationService
.updateUsernameAndEmail(
this.editProfileForm.get('username')?.value,
this.editProfileForm.get('email')?.value,
this.editProfileForm.get('password')?.value,
)
.subscribe({
next: () => {
this.closeDialog();
this.isProcessingEdit = true;

this.messageService.add({
severity: 'success',
summary: 'Profile Updated',
detail: 'Your profile has been updated successfully!',
});
},
error: error => {
this.isProcessingEdit = false;
const status = error.cause.status;
let errorMessage = 'An unknown error occurred';
if (status === 401) {
errorMessage = 'Your session has expired. Please log out and log back in.';
} else if (status === 409) {
errorMessage = 'The username or email already exists.';
}
this.messageService.add({
severity: 'error',
summary: 'Editing Profile Erorr',
detail: errorMessage,
});
},
});
},
error: error => {
this.isProcessingEdit = false;
const status = error.cause.status;
let errorMessage = 'An unknown error occurred';
if (status === 400) {
errorMessage = 'Missing/Invalid passwords';
} else if (status === 401) {
errorMessage = 'Your password is invalid';
} else if (status === 500) {
errorMessage = 'Internal Server Error';
}
this.messageService.add({
severity: 'error',
summary: 'Submission Error',
detail: errorMessage,
});
},
});
}
this.authenticationService
.updateUsernameAndEmail(
this.editProfileForm.get('username')?.value,
this.editProfileForm.get('email')?.value,
this.editProfileForm.get('password')?.value,
)
.subscribe({
next: () => {
this.closeDialog();

this.messageService.add({
severity: 'success',
summary: 'Profile Updated',
detail: 'Your profile has been updated successfully!',
});
},
error: error => {
this.isProcessingEdit = false;
const status = error.cause.status;
let errorMessage = 'An unknown error occurred';
if (status === 401) {
errorMessage = 'Your session has expired. Please log out and log back in.';
} else if (status === 409) {
errorMessage = 'The username or email already exists.';
}
this.messageService.add({
severity: 'error',
summary: 'Editing Profile Erorr',
detail: errorMessage,
});
},
});
}
}
3 changes: 0 additions & 3 deletions frontend/src/app/account/profile/profile.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ToastModule } from 'primeng/toast';
import { DividerModule } from 'primeng/divider';
import { InputTextModule } from 'primeng/inputtext';
import { FormUtilsService } from '../../../_services/form.utils.service';
import { Router } from '@angular/router';
import { EditProfileDialogComponent } from './edit-profile-dialog/edit-profile-dialog.component';
import { EditPasswordDialogComponent } from './edit-password-dialog/edit-password-dialog.component';

Expand Down Expand Up @@ -37,8 +36,6 @@ export class ProfileComponent implements OnInit {

constructor(
private authenticationService: AuthenticationService,
private messageService: MessageService,
private router: Router,
public formUtils: FormUtilsService,
) {}

Expand Down
4 changes: 2 additions & 2 deletions services/user/src/controller/user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ export async function updateUsernameAndEmail(req: Request, res: Response) {

const userByUsername = await _findUserByUsername(username);
if (userByUsername && userByUsername.id !== userId) {
handleConflict(res, 'username already exists');
handleConflict(res, 'The username already exists');
return;
}
const userByEmail = await _findUserByEmail(email);
if (userByEmail && userByEmail.id !== userId) {
handleConflict(res, 'email already exists');
handleConflict(res, 'The email already exists');
return;
}

Expand Down

0 comments on commit 48066d2

Please sign in to comment.