Skip to content

Commit

Permalink
refactor: update auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
stardustmeg committed Aug 21, 2024
1 parent 556e5bd commit e26ce0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
25 changes: 2 additions & 23 deletions src/app/auth/pages/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { Router, RouterLink } from '@angular/router';
import { RouterLink } from '@angular/router';

import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
import { PasswordModule } from 'primeng/password';
import { firstValueFrom } from 'rxjs';

import { OverriddenHttpErrorResponse } from '@/app/api/models/errorResponse';
import { SignInResponse } from '@/app/api/models/signInResponse';
import { User } from '@/app/api/models/user';
import { SignInService } from '@/app/api/signInService/sign-in.service';
import { LocalStorageService } from '@/app/core/services/local-storage/local-storage.service';
import { PersonalInfoService } from '@/app/profile/services/personalInfo/personal-info.service';
import { APP_PATH } from '@/app/shared/constants/routes';
import { PASSWORD_MIN_LENGTH, REGEX } from '@/app/shared/validators/constants/constants';
import { minTrimmedLengthValidator } from '@/app/shared/validators/validators';

Expand All @@ -29,10 +22,6 @@ import { AuthService } from '../../services/auth-service/auth.service';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LoginComponent {
private signInService = inject(SignInService);
private localStorageService = inject(LocalStorageService);
private personalInfoService = inject(PersonalInfoService);
private router = inject(Router);
private fb = inject(FormBuilder);

public authService = inject(AuthService);
Expand All @@ -51,17 +40,7 @@ export class LoginComponent {

public submitForm(): void {
if (this.loginForm.valid) {
firstValueFrom(this.signInService.signIn(this.userData))
.then((data: SignInResponse) => {
const { email } = this.userData;
this.authService.setLoginSignals(this.userData);
this.personalInfoService.setUserInfo(email);
this.localStorageService.saveCurrentUser(email, data.token);
this.router.navigate([APP_PATH.DEFAULT]);
})
.catch((err: OverriddenHttpErrorResponse) => {
this.loginForm.setErrors({ [err.error.reason]: true });
});
this.authService.loginUser(this.userData, this.loginForm);
} else {
Object.values(this.loginForm.controls).forEach((control) => {
if (control.invalid) {
Expand Down
28 changes: 26 additions & 2 deletions src/app/auth/services/auth-service/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { inject, Injectable, OnDestroy, signal } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Router } from '@angular/router';

import { Subscription } from 'rxjs';
import { firstValueFrom, Subscription } from 'rxjs';

import { ADMIN_CREDENTIALS } from '@/app/admin/constants/adminCredentials';
import { OverriddenHttpErrorResponse } from '@/app/api/models/errorResponse';
import { SignInResponse } from '@/app/api/models/signInResponse';
import { User } from '@/app/api/models/user';
import { SignInService } from '@/app/api/signInService/sign-in.service';
import STORE_KEYS from '@/app/core/constants/store';
import { LocalStorageService } from '@/app/core/services/local-storage/local-storage.service';
import { APP_ROUTE } from '@/app/shared/constants/routes';
import { PersonalInfoService } from '@/app/profile/services/personalInfo/personal-info.service';
import { APP_PATH, APP_ROUTE } from '@/app/shared/constants/routes';
import { USER_MESSAGE } from '@/app/shared/services/userMessage/constants/user-messages';
import { UserMessageService } from '@/app/shared/services/userMessage/user-message.service';

import { SignUpService } from '../../../api/signUpService/sign-up.service';
import { isOverriddenHttpErrorResponse } from './helpers/helper';

@Injectable({
providedIn: 'root',
})
export class AuthService implements OnDestroy {
private signUpService = inject(SignUpService);
private signInService = inject(SignInService);
private personalInfoService = inject(PersonalInfoService);
private router = inject(Router);
private userMessageService = inject(UserMessageService);
private localStorageService = inject(LocalStorageService);
Expand Down Expand Up @@ -46,6 +53,23 @@ export class AuthService implements OnDestroy {
});
}

public async loginUser(userData: User, loginForm: FormGroup): Promise<void> {
try {
const data: SignInResponse = await firstValueFrom(this.signInService.signIn(userData));
const { email } = userData;
this.setLoginSignals(userData);
this.personalInfoService.setUserInfo(email);
this.localStorageService.saveCurrentUser(email, data.token);
this.router.navigate([APP_PATH.DEFAULT]);
this.userMessageService.showSuccessMessage(USER_MESSAGE.LOGIN_SUCCESSFUL);
} catch (err: unknown) {
if (isOverriddenHttpErrorResponse(err)) {
loginForm.setErrors({ [err.error.reason]: true });
}
this.userMessageService.showErrorMessage(USER_MESSAGE.LOGIN_ERROR);
}
}

public setLoginSignals(userData: User): void {
this.isLoggedIn$$.set(true);
if (userData.email === ADMIN_CREDENTIALS.email) {
Expand Down
9 changes: 9 additions & 0 deletions src/app/auth/services/auth-service/helpers/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OverriddenHttpErrorResponse } from '@/app/api/models/errorResponse';

export const isOverriddenHttpErrorResponse = (error: unknown): error is OverriddenHttpErrorResponse =>
typeof error === 'object' &&
error !== null &&
'error' in error &&
typeof error.error === 'object' &&
error.error !== null &&
'reason' in error.error;

0 comments on commit e26ce0f

Please sign in to comment.