auth => clean archi
This commit is contained in:
137
src/app/ui/authentification/auth.facade.ts
Normal file
137
src/app/ui/authentification/auth.facade.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { inject, Injectable, signal } from '@angular/core';
|
||||
import { AUTH_REPOSITORY_TOKEN } from '@app/infrastructure/authentification/auth-repository.token';
|
||||
import { LoaderAction } from '@app/domain/loader-action.util';
|
||||
import { ActionType } from '@app/domain/action-type.util';
|
||||
import { ErrorResponse } from '@app/domain/error-response.util';
|
||||
import { LoginUseCase } from '@app/usecase/authentification/login.usecase';
|
||||
import { RegisterUseCase } from '@app/usecase/authentification/register.usecase';
|
||||
import { LoginDto } from '@app/domain/authentification/dto/login-dto';
|
||||
import { RegisterDto } from '@app/domain/authentification/dto/register-dto';
|
||||
import { User } from '@app/domain/users/user.model';
|
||||
import { AuthResponse } from '@app/domain/authentification/auth.repository';
|
||||
import { ProfileDTO } from '@app/domain/profiles/dto/create-profile.dto';
|
||||
import { ProfileFacade } from '@app/ui/profiles/profile.facade';
|
||||
import { SendVerificationEmailUsecase } from '@app/usecase/authentification/send-verification-email.usecase';
|
||||
import { LogoutUseCase } from '@app/usecase/authentification/logout.usecase';
|
||||
import { VerifyAuthenticatedUsecase } from '@app/usecase/authentification/verify-authenticated.usecase';
|
||||
import { VerifyEmailUseCase } from '@app/usecase/authentification/verify-email.usecase';
|
||||
import { GetCurrentUserUseCase } from '@app/usecase/authentification/get-current-user.usecase';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class AuthFacade {
|
||||
private readonly authRepository = inject(AUTH_REPOSITORY_TOKEN);
|
||||
|
||||
private readonly profileFacade = new ProfileFacade();
|
||||
|
||||
private readonly loginUseCase = new LoginUseCase(this.authRepository);
|
||||
private readonly registerUseCase = new RegisterUseCase(this.authRepository);
|
||||
private readonly logoutUseCase = new LogoutUseCase(this.authRepository);
|
||||
private readonly getUserUseCase = new GetCurrentUserUseCase(this.authRepository);
|
||||
private readonly sendVerificationEmailUseCase = new SendVerificationEmailUsecase(
|
||||
this.authRepository
|
||||
);
|
||||
private readonly verifyAuthenticatedUseCase = new VerifyAuthenticatedUsecase(this.authRepository);
|
||||
private readonly verifyEmailUseCase = new VerifyEmailUseCase(this.authRepository);
|
||||
|
||||
readonly isAuthenticated = signal<boolean>(false);
|
||||
readonly isEmailVerified = signal<boolean>(false);
|
||||
readonly isVerificationEmailSent = signal<boolean>(false);
|
||||
|
||||
readonly user = signal<User | undefined>(undefined);
|
||||
readonly authResponse = signal<AuthResponse | undefined>(undefined);
|
||||
|
||||
readonly loading = signal<LoaderAction>({ isLoading: false, action: ActionType.NONE });
|
||||
readonly error = signal<ErrorResponse>({
|
||||
action: ActionType.NONE,
|
||||
hasError: false,
|
||||
message: null,
|
||||
});
|
||||
|
||||
login(loginDto: LoginDto) {
|
||||
this.handleError(ActionType.READ, false, null, true);
|
||||
|
||||
this.loginUseCase.execute(loginDto).subscribe({
|
||||
next: async (res: AuthResponse) => {
|
||||
this.authResponse.set(res);
|
||||
this.getCurrentUser();
|
||||
this.handleError(ActionType.READ, false, null, false);
|
||||
},
|
||||
error: (err) => {
|
||||
this.handleError(ActionType.READ, true, err.message, false);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
register(registerDto: RegisterDto) {
|
||||
this.handleError(ActionType.CREATE, false, null, true);
|
||||
this.registerUseCase.execute(registerDto).subscribe({
|
||||
next: (user) => {
|
||||
this.getCurrentUser();
|
||||
this.sendVerificationEmail(registerDto.email);
|
||||
this.createDefaultProfile(user.id);
|
||||
this.handleError(ActionType.CREATE, false, null, false);
|
||||
},
|
||||
error: (err) => {
|
||||
this.handleError(ActionType.CREATE, true, err.message, false);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
logout() {
|
||||
this.logoutUseCase.execute();
|
||||
this.getCurrentUser();
|
||||
}
|
||||
|
||||
verifyAuthenticatedUser() {
|
||||
this.isAuthenticated.set(this.verifyAuthenticatedUseCase.execute());
|
||||
}
|
||||
|
||||
verifyEmail() {
|
||||
this.isEmailVerified.set(this.verifyEmailUseCase.execute());
|
||||
}
|
||||
|
||||
getCurrentUser() {
|
||||
this.user.set(this.getUserUseCase.execute());
|
||||
}
|
||||
|
||||
private sendVerificationEmail(email: string) {
|
||||
this.handleError(ActionType.CREATE, false, null, true);
|
||||
this.sendVerificationEmailUseCase.execute(email).subscribe({
|
||||
next: (res) => {
|
||||
this.isVerificationEmailSent.set(res);
|
||||
this.handleError(ActionType.CREATE, false, null, false);
|
||||
},
|
||||
error: (err) => {
|
||||
this.handleError(ActionType.CREATE, true, err.message, false);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private createDefaultProfile(userId: string) {
|
||||
const profileDto: ProfileDTO = {
|
||||
profession: 'Profession non renseignée',
|
||||
utilisateur: userId,
|
||||
reseaux: {
|
||||
facebook: '',
|
||||
github: '',
|
||||
instagram: '',
|
||||
linkedIn: '',
|
||||
web: '',
|
||||
x: '',
|
||||
youTube: '',
|
||||
},
|
||||
};
|
||||
|
||||
this.profileFacade.create(profileDto);
|
||||
}
|
||||
|
||||
private handleError(
|
||||
action: ActionType = ActionType.NONE,
|
||||
hasError: boolean,
|
||||
message: string | null = null,
|
||||
isLoading = false
|
||||
) {
|
||||
this.error.set({ action, hasError, message });
|
||||
this.loading.set({ action, isLoading });
|
||||
}
|
||||
}
|
||||
@@ -11,14 +11,12 @@ import { CreateProfileUseCase } from '@app/usecase/profiles/create-profile.useca
|
||||
import { UpdateProfileUseCase } from '@app/usecase/profiles/update-profile.usecase';
|
||||
import { GetProfileUseCase } from '@app/usecase/profiles/get-profile.usecase';
|
||||
import { ProfileDTO } from '@app/domain/profiles/dto/create-profile.dto';
|
||||
import { AuthService } from '@app/core/services/authentication/auth.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ProfileFacade {
|
||||
private profileRepository = inject(PROFILE_REPOSITORY_TOKEN);
|
||||
protected readonly authService = inject(AuthService);
|
||||
|
||||
private listUseCase = new ListProfilesUseCase(this.profileRepository);
|
||||
private createUseCase = new CreateProfileUseCase(this.profileRepository);
|
||||
@@ -82,7 +80,6 @@ export class ProfileFacade {
|
||||
this.updateUseCase.execute(profileId, profile).subscribe({
|
||||
next: (profile: Profile) => {
|
||||
this.profile.set(ProfilePresenter.toViewModel(profile));
|
||||
this.authService.updateUser();
|
||||
this.handleError(ActionType.UPDATE, false, null, false);
|
||||
},
|
||||
error: (err) => {
|
||||
|
||||
@@ -11,14 +11,12 @@ import { CreateProjectDto } from '@app/domain/projects/dto/create-project.dto';
|
||||
import { ErrorResponse } from '@app/domain/error-response.util';
|
||||
import { ActionType } from '@app/domain/action-type.util';
|
||||
import { LoaderAction } from '@app/domain/loader-action.util';
|
||||
import { AuthService } from '@app/core/services/authentication/auth.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ProjectFacade {
|
||||
private readonly projectRepo = inject(PROJECT_REPOSITORY_TOKEN);
|
||||
private readonly authService = inject(AuthService);
|
||||
|
||||
private readonly createUseCase = new CreateProjectUseCase(this.projectRepo);
|
||||
private readonly listUseCase = new ListProjectUseCase(this.projectRepo);
|
||||
@@ -86,7 +84,6 @@ export class ProjectFacade {
|
||||
this.UpdateUseCase.execute(userId, data).subscribe({
|
||||
next: (project: Project) => {
|
||||
this.project.set(this.projectPresenter.toViewModel(project));
|
||||
this.authService.updateUser();
|
||||
this.handleError(ActionType.UPDATE, false, null, false);
|
||||
},
|
||||
error: (err) => {
|
||||
|
||||
@@ -7,7 +7,6 @@ import { ActionType } from '@app/domain/action-type.util';
|
||||
import { ErrorResponse } from '@app/domain/error-response.util';
|
||||
import { UserViewModel } from '@app/ui/users/user.presenter.model';
|
||||
import { UserPresenter } from '@app/ui/users/user.presenter';
|
||||
import { AuthService } from '@app/core/services/authentication/auth.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -15,8 +14,6 @@ import { AuthService } from '@app/core/services/authentication/auth.service';
|
||||
export class UserFacade {
|
||||
private readonly userRepository = inject(USER_REPOSITORY_TOKEN);
|
||||
|
||||
private readonly authService = inject(AuthService);
|
||||
|
||||
private readonly getUseCase = new GetUserUseCase(this.userRepository);
|
||||
private readonly updateUseCase = new UpdateUserUseCase(this.userRepository);
|
||||
|
||||
@@ -49,7 +46,6 @@ export class UserFacade {
|
||||
this.updateUseCase.execute(userId, user).subscribe({
|
||||
next: (user) => {
|
||||
this.user.set(this.userPresenter.toViewModel(user));
|
||||
this.authService.updateUser();
|
||||
this.handleError(ActionType.UPDATE, false, null, false);
|
||||
},
|
||||
error: (err) => {
|
||||
|
||||
Reference in New Issue
Block a user