Skip to content

Commit d2ee4ca

Browse files
committed
add facades for components & ui facades
1 parent eb5f9c2 commit d2ee4ca

102 files changed

Lines changed: 11949 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/** @format */
2+
3+
import { inject, signal } from "@angular/core";
4+
import { ActivatedRouteSnapshot, CanActivateFn, Router, UrlTree } from "@angular/router";
5+
import { AuthService } from "projects/social_platform/src/app/api/auth";
6+
import { Observable, of } from "rxjs";
7+
8+
export const ProfileEditRequiredGuard: CanActivateFn = (
9+
route: ActivatedRouteSnapshot
10+
): Observable<boolean | UrlTree> => {
11+
const router = inject(Router);
12+
const authService = inject(AuthService);
13+
14+
const loggedUserId = signal<number | undefined>(undefined);
15+
16+
authService.profile.subscribe({
17+
next: profile => {
18+
loggedUserId.set(profile.id);
19+
},
20+
});
21+
22+
const profileId = Number(route.paramMap.get("id"));
23+
if (profileId !== loggedUserId()) {
24+
return of(router.createUrlTree([`/office/profile/${profileId}/`]));
25+
}
26+
27+
return of(router.createUrlTree([`/office/profel/${profileId}/`]));
28+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/** @format */
2+
3+
import { inject, Injectable, signal } from "@angular/core";
4+
import { ActivatedRoute, Router } from "@angular/router";
5+
import { TokenService } from "@corelib";
6+
import { filter, interval, map, Subject, takeUntil } from "rxjs";
7+
import { AuthService } from "../auth.service";
8+
9+
@Injectable()
10+
export class AuthEmailService {
11+
private readonly tokenService = inject(TokenService);
12+
private readonly route = inject(ActivatedRoute);
13+
private readonly authService = inject(AuthService);
14+
private readonly router = inject(Router);
15+
16+
private readonly destroy$ = new Subject<void>();
17+
18+
// ConfirmEmail Component
19+
private readonly userEmail = signal<string | undefined>(undefined);
20+
21+
// VerificationEmail Component
22+
readonly counter = signal<number>(0);
23+
private timerStarted = false;
24+
25+
// ConfirmEmail Component
26+
27+
initializationTokens(): void {
28+
this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe(queries => {
29+
const { access_token: accessToken, refresh_token: refreshToken } = queries;
30+
this.tokenService.memTokens({ access: accessToken, refresh: refreshToken });
31+
32+
if (this.tokenService.getTokens() !== null) {
33+
this.router
34+
.navigateByUrl("/office")
35+
.then(() => console.debug("Route changed from ConfirmEmailComponent"));
36+
}
37+
});
38+
}
39+
40+
// VerificationEmail Component
41+
42+
initializationEmail(): void {
43+
this.route.queryParams
44+
.pipe(
45+
map(r => r["adress"]),
46+
takeUntil(this.destroy$)
47+
)
48+
.subscribe(address => {
49+
this.userEmail.set(address);
50+
});
51+
}
52+
53+
destroy(): void {
54+
this.destroy$.next();
55+
this.destroy$.complete();
56+
}
57+
58+
onResend(): void {
59+
if (!this.userEmail()) return;
60+
61+
this.authService
62+
.resendEmail(this.userEmail()!)
63+
.pipe(takeUntil(this.destroy$))
64+
.subscribe(() => {
65+
this.counter.set(60);
66+
});
67+
}
68+
69+
initializationTimer(): void {
70+
if (this.timerStarted) return;
71+
this.timerStarted = true;
72+
this.timer$.subscribe();
73+
}
74+
75+
timer$ = interval(1000).pipe(
76+
filter(() => this.counter() > 0),
77+
map(() => this.counter.update(c => c - 1)),
78+
takeUntil(this.destroy$)
79+
);
80+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/** @format */
2+
3+
import { inject, Injectable } from "@angular/core";
4+
import { ActivatedRoute, Router } from "@angular/router";
5+
import { TokenService, ValidationService } from "@corelib";
6+
import { Subject, takeUntil } from "rxjs";
7+
import { AuthService } from "../auth.service";
8+
import { AuthUIInfoService } from "./ui/auth-ui-info.service";
9+
import { LoginRequest } from "../../../domain/auth/http.model";
10+
11+
@Injectable()
12+
export class AuthLoginService {
13+
private readonly tokenService = inject(TokenService);
14+
private readonly authService = inject(AuthService);
15+
private readonly route = inject(ActivatedRoute);
16+
private readonly router = inject(Router);
17+
private readonly authUIInfoService = inject(AuthUIInfoService);
18+
19+
private readonly destroy$ = new Subject<void>();
20+
21+
// Login Component
22+
readonly showPassword = this.authUIInfoService.showPassword;
23+
readonly loginIsSubmitting = this.authUIInfoService.loginIsSubmitting;
24+
readonly errorWrongAuth = this.authUIInfoService.errorWrongAuth;
25+
26+
destroy(): void {
27+
this.destroy$.next();
28+
this.destroy$.complete();
29+
}
30+
31+
onSubmit(loginForm: LoginRequest) {
32+
const redirectType = this.route.snapshot.queryParams["redirect"];
33+
34+
this.loginIsSubmitting.set(true);
35+
36+
this.authService
37+
.login(loginForm)
38+
.pipe(takeUntil(this.destroy$))
39+
.subscribe({
40+
next: res => {
41+
this.tokenService.memTokens(res);
42+
this.loginIsSubmitting.set(false);
43+
44+
if (!redirectType)
45+
this.router
46+
.navigateByUrl("/office")
47+
.then(() => console.debug("Route changed from LoginComponent"));
48+
else if (redirectType === "program")
49+
this.router
50+
.navigateByUrl("/office/program/list")
51+
.then(() => console.debug("Route changed from LoginComponent"));
52+
},
53+
error: error => {
54+
if (error.status === 401) {
55+
this.errorWrongAuth.set(true);
56+
}
57+
58+
this.loginIsSubmitting.set(false);
59+
},
60+
});
61+
}
62+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/** @format */
2+
3+
import { inject, Injectable, signal } from "@angular/core";
4+
import { FormGroup } from "@angular/forms";
5+
import { ActivatedRoute, Router } from "@angular/router";
6+
import { ValidationService } from "@corelib";
7+
import { map, Subject, takeUntil } from "rxjs";
8+
import { AuthService } from "../auth.service";
9+
import { AuthUIInfoService } from "./ui/auth-ui-info.service";
10+
11+
@Injectable()
12+
export class AuthPasswordService {
13+
private readonly route = inject(ActivatedRoute);
14+
private readonly authService = inject(AuthService);
15+
private readonly router = inject(Router);
16+
private readonly validationService = inject(ValidationService);
17+
private readonly authUIInfoService = inject(AuthUIInfoService);
18+
19+
private readonly destroy$ = new Subject<void>();
20+
21+
// ResetPassword-Confirmation Component
22+
readonly email = this.route.queryParams.pipe(
23+
map(r => r["email"]),
24+
takeUntil(this.destroy$)
25+
);
26+
27+
// ResetPassword Component
28+
readonly isSubmitting = this.authUIInfoService.isSubmitting;
29+
readonly errorRequest = this.authUIInfoService.errorRequest;
30+
readonly credsSubmitInitiated = this.authUIInfoService.credsSubmitInitiated;
31+
private readonly errorServer = this.authUIInfoService.errorServer;
32+
33+
// ResetPassword Component
34+
onSubmitResetPassword(resetForm: FormGroup): void {
35+
if (!this.validationService.getFormValidation(resetForm)) return;
36+
37+
this.errorServer.set(false);
38+
this.isSubmitting.set(true);
39+
40+
this.authService.resetPassword(resetForm.value.email).subscribe({
41+
next: () => {
42+
this.router
43+
.navigate(["/auth/reset_password/confirm"], {
44+
queryParams: { email: resetForm.value.email },
45+
})
46+
.then(() => console.debug("ResetPasswordComponent"));
47+
},
48+
error: () => {
49+
this.errorServer.set(true);
50+
this.isSubmitting.set(false);
51+
52+
resetForm.reset();
53+
},
54+
complete: () => {
55+
this.isSubmitting.set(false);
56+
},
57+
});
58+
}
59+
60+
// SetPassword Component
61+
onSubmitSetPassword(passwordForm: FormGroup): void {
62+
this.credsSubmitInitiated.set(true);
63+
const token = this.route.snapshot.queryParamMap.get("token");
64+
65+
if (!token || !this.validationService.getFormValidation(passwordForm)) return;
66+
67+
this.authService.setPassword(passwordForm.value.password, token).subscribe({
68+
next: () => {
69+
this.router.navigateByUrl("/auth/login").then(() => console.debug("SetPasswordComponent"));
70+
},
71+
error: error => {
72+
console.error("Error setting password:", error);
73+
this.errorRequest.set(true);
74+
},
75+
});
76+
}
77+
78+
destroy(): void {
79+
this.destroy$.next();
80+
this.destroy$.complete();
81+
}
82+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/** @format */
2+
3+
import { inject, Injectable, signal } from "@angular/core";
4+
import { FormGroup } from "@angular/forms";
5+
import { Router } from "@angular/router";
6+
import { Subject, takeUntil } from "rxjs";
7+
import { AuthService } from "../auth.service";
8+
import { ValidationService } from "@corelib";
9+
import { AuthUIInfoService } from "./ui/auth-ui-info.service";
10+
import dayjs from "dayjs";
11+
12+
@Injectable()
13+
export class AuthRegisterService {
14+
private readonly authService = inject(AuthService);
15+
private readonly router = inject(Router);
16+
private readonly validationService = inject(ValidationService);
17+
private readonly authUIInfoService = inject(AuthUIInfoService);
18+
19+
readonly registerAgreement = this.authUIInfoService.registerAgreement;
20+
readonly ageAgreement = this.authUIInfoService.ageAgreement;
21+
readonly registerIsSubmitting = this.authUIInfoService.registerIsSubmitting;
22+
readonly credsSubmitInitiated = this.authUIInfoService.credsSubmitInitiated;
23+
readonly infoSubmitInitiated = this.authUIInfoService.infoSubmitInitiated;
24+
25+
readonly showPassword = this.authUIInfoService.showPassword;
26+
readonly showPasswordRepeat = this.authUIInfoService.showPasswordRepeat;
27+
28+
readonly isUserCreationModalError = this.authUIInfoService.isUserCreationModalError;
29+
30+
readonly step = this.authUIInfoService.step;
31+
32+
readonly serverErrors = signal<string[]>([]);
33+
34+
private readonly destroy$ = new Subject<void>();
35+
36+
destroy(): void {
37+
this.destroy$.next();
38+
this.destroy$.complete();
39+
}
40+
41+
onSendForm(registerForm: FormGroup): void {
42+
const form = this.authUIInfoService.prepareFormValues(registerForm);
43+
44+
this.registerIsSubmitting.set(true);
45+
46+
this.authService
47+
.register(form)
48+
.pipe(takeUntil(this.destroy$))
49+
.subscribe({
50+
next: () => {
51+
this.registerIsSubmitting.set(false);
52+
this.router.navigateByUrl("/auth/verification/email?adress=" + form.email);
53+
},
54+
error: error => {
55+
const emailErrors = error?.error?.email;
56+
57+
if (error.status === 400 && Array.isArray(emailErrors)) {
58+
this.serverErrors.set(Object.values(error.error).flat() as string[]);
59+
} else if (error.status === 500) {
60+
this.isUserCreationModalError.set(true);
61+
}
62+
63+
this.registerIsSubmitting.set(false);
64+
},
65+
});
66+
}
67+
}

0 commit comments

Comments
 (0)