Skip to content

Commit b6a7ab9

Browse files
committed
fix frontend routes
1 parent 16d9789 commit b6a7ab9

File tree

3 files changed

+42
-106
lines changed

3 files changed

+42
-106
lines changed

frontend/ultima-ng-19.0.0/src/app/pages/auth/auth.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default [
55
path: '',
66
loadComponent: () => import('./auth.component').then((c) => c.AuthComponent),
77
children: [
8-
//{ path: '', loadComponent: () => import('../home/home.component').then((c) => c.HomeComponent) },
8+
{ path: '', loadComponent: () => import('../home/home.component').then((c) => c.HomeComponent) },
99
{ path: 'credit', loadComponent: () => import('./credit/credit.component').then((c) => c.CreditComponent) },
1010
{ path: 'credit/demandeInd/:codeClient', loadComponent: () => import('./credit/demande-ind/demande-ind.component').then((c) => c.DemandeIndComponent) },
1111
{ path: 'error', loadComponent: () => import('./error').then((c) => c.Error) },

frontend/ultima-ng-19.0.0/src/app/pages/home/home.component.ts

Lines changed: 40 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { UserService } from '@/service/user.service';
77
import { getFormData } from '@/utils/fileutils';
88
import { Component, DestroyRef, inject, signal } from '@angular/core';
99
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
10-
import { Router, ActivatedRoute, RouterModule } from '@angular/router';
10+
import { Router, ActivatedRoute, RouterModule, ParamMap } from '@angular/router';
1111
import { MenuItem, MessageService } from 'primeng/api';
1212
import { ButtonModule } from 'primeng/button';
1313
import { DropdownModule } from 'primeng/dropdown';
@@ -16,7 +16,7 @@ import { InputIconModule } from 'primeng/inputicon';
1616
import { InputTextModule } from 'primeng/inputtext';
1717
import { MenuModule } from 'primeng/menu';
1818
import { ProgressSpinnerModule } from 'primeng/progressspinner';
19-
import { catchError, delay, take, tap, throwError } from 'rxjs';
19+
import { catchError, delay, EMPTY, switchMap, take, tap, throwError } from 'rxjs';
2020
import { environment } from 'src/environments/environment';
2121

2222
@Component({
@@ -47,124 +47,60 @@ import { environment } from 'src/environments/environment';
4747
providers: [MessageService]
4848
})
4949
export class HomeComponent {
50-
private readonly redirectBaseUrl: string = environment.redirectUri;
5150
loading = signal<boolean>(true);
52-
private router = inject(Router);
51+
isAuthenticatedAndRedirecting = signal<boolean>(false); // Ajoutez cette ligne
52+
5353
private destroyRef = inject(DestroyRef);
54+
private router = inject(Router);
5455
private storage = inject(StorageService);
5556
private userService = inject(UserService);
5657
private activatedRoute = inject(ActivatedRoute);
5758
private messageService = inject(MessageService);
5859

59-
// Add a signal to track if user is authenticated and redirecting
60-
isAuthenticatedAndRedirecting = signal<boolean>(false);
60+
private readonly redirectBaseUrl: string = environment.redirectUri;
6161

6262
ngOnInit(): void {
63-
// Keep loading true until we explicitly set it to false
64-
this.loading.set(true);
65-
66-
// First check if user is already authenticated
63+
// Si déjà authentifié
6764
if (this.userService.isAuthenticated() && !this.userService.isTokenExpired()) {
68-
console.log('User already authenticated, redirecting...');
65+
this.isAuthenticatedAndRedirecting.set(true); // Ajoutez cette ligne
6966
const redirectUrl = this.storage.getRedirectUrl() || '/dashboards';
70-
71-
// Mark as authenticated and redirecting
72-
this.isAuthenticatedAndRedirecting.set(true);
73-
74-
// Keep loading true during navigation
75-
this.router
76-
.navigate([redirectUrl])
77-
.then(() => {
78-
console.log('Navigation complete for authenticated user');
79-
})
80-
.catch((error) => {
81-
console.error('Navigation error:', error);
82-
// Only set loading to false if navigation fails
83-
this.loading.set(false);
84-
this.isAuthenticatedAndRedirecting.set(false);
85-
});
86-
87-
return; // Exit early - don't process OAuth codes for already authenticated users
67+
this.router.navigate([redirectUrl]);
68+
return;
8869
}
8970

90-
console.log('Checking for OAuth code in DashboardAnalytics');
71+
// Gérer OAuth callback
9172
this.activatedRoute.queryParamMap
9273
.pipe(
93-
take(1), // Only take first emission to prevent multiple subscriptions
74+
switchMap((params: ParamMap) => {
75+
const code = params.get('code');
76+
if (code) {
77+
this.loading.set(true);
78+
return this.userService.validateCode$(this.formData(code));
79+
} else {
80+
this.loading.set(false);
81+
return EMPTY;
82+
}
83+
}),
84+
delay(1000),
9485
takeUntilDestroyed(this.destroyRef)
9586
)
96-
.subscribe((params) => {
97-
// Get code from either route params or window location
98-
const code = params.get('code');
99-
console.log('OAuth code from ActivatedRoute:', code);
100-
101-
const urlParams = new URLSearchParams(window.location.search);
102-
const urlCode = urlParams.get('code');
103-
console.log('OAuth code from window.location:', urlCode);
104-
105-
const authCode = code || urlCode;
106-
107-
if (authCode) {
108-
console.log('Processing authentication with code:', authCode);
109-
110-
// Ensure loading is true during validation
111-
this.loading.set(true);
112-
113-
this.userService
114-
.validateCode$(this.formData(authCode))
115-
.pipe(
116-
delay(3 * 1000),
117-
takeUntilDestroyed(this.destroyRef),
118-
// Ensure loading stays true during this operation
119-
tap(() => this.loading.set(true)),
120-
catchError((err) => {
121-
console.error('Authentication error:', err);
122-
this.loading.set(false);
123-
this.messageService.add({
124-
severity: 'error',
125-
summary: 'Verification Account',
126-
detail: typeof err === 'string' ? err : 'Authentication failed'
127-
});
128-
return throwError(() => err);
129-
})
130-
)
131-
.subscribe({
132-
next: (response: IAuthentication) => {
133-
this.saveToken(response);
134-
console.log('Authentication successful, tokens saved');
135-
136-
// Mark as authenticated and redirecting
137-
this.isAuthenticatedAndRedirecting.set(true);
138-
139-
const currentUrlWithoutParams = this.router.url.split('?')[0];
140-
141-
// Navigate and keep loading true until navigation completes
142-
this.router
143-
//.navigateByUrl(currentUrlWithoutParams)
144-
.navigate(['/dashboards'])
145-
.then(() => {
146-
console.log('Navigation complete after authentication');
147-
// Navigation successful - component should be destroyed
148-
})
149-
.catch((error) => {
150-
console.error('Navigation error after authentication:', error);
151-
// Only set loading to false if navigation fails
152-
this.loading.set(false);
153-
this.isAuthenticatedAndRedirecting.set(false);
154-
});
155-
},
156-
error: () => {
157-
// Error handling moved to catchError operator
158-
this.isAuthenticatedAndRedirecting.set(false);
159-
}
160-
});
161-
} else {
162-
console.log('No OAuth code found, continuing normal initialization');
163-
// Add a small delay before showing content to prevent flicker
164-
setTimeout(() => {
165-
this.loading.set(false);
166-
}, 100);
167-
}
87+
.subscribe({
88+
next: (response: IAuthentication) => {
89+
this.saveToken(response);
90+
this.isAuthenticatedAndRedirecting.set(true); // Ajoutez cette ligne
91+
const redirectUrl = this.storage.getRedirectUrl() || '/dashboards';
92+
this.router.navigate([redirectUrl]);
93+
},
94+
error: (error) => {
95+
this.loading.set(false);
96+
this.isAuthenticatedAndRedirecting.set(false); // Ajoutez cette ligne
97+
this.messageService.add({
98+
severity: 'error',
99+
summary: 'Authentication Failed',
100+
detail: typeof error === 'string' ? error : 'Please try again'
101+
});
102+
},
103+
complete: () => console.log('Authentication complete')
168104
});
169105
}
170106

@@ -178,11 +114,11 @@ export class HomeComponent {
178114
});
179115

180116
private saveToken = (response: IAuthentication) => {
181-
console.log('Saving auth tokens');
182117
this.storage.set(Key.TOKEN, response.access_token);
183118
this.storage.set(Key.REFRESH_TOKEN, response.refresh_token || response.access_token);
184119
};
185120

121+
// Ajoutez les autres méthodes si elles sont utilisées dans le template
186122
demandesItems: MenuItem[] = [
187123
{
188124
label: 'Demande de Crédit Individuel',

frontend/ultima-ng-19.0.0/src/app/pages/landing/components/topbar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class Topbar {
5555
private authServer = environment.authServer;
5656
private redirectUri = environment.redirectUri;
5757

58-
// Fixed code challenge for this example
58+
// Fixed code challenge
5959
private codeChallenge = 'HK02sitqCRpUlfLEX2xl4JGqaVQhNDsfTWH-oQzJHGw';
6060

6161
handleScroll(id: string) {

0 commit comments

Comments
 (0)