@@ -7,7 +7,7 @@ import { UserService } from '@/service/user.service';
77import { getFormData } from '@/utils/fileutils' ;
88import { Component , DestroyRef , inject , signal } from '@angular/core' ;
99import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
10- import { Router , ActivatedRoute , RouterModule } from '@angular/router' ;
10+ import { Router , ActivatedRoute , RouterModule , ParamMap } from '@angular/router' ;
1111import { MenuItem , MessageService } from 'primeng/api' ;
1212import { ButtonModule } from 'primeng/button' ;
1313import { DropdownModule } from 'primeng/dropdown' ;
@@ -16,7 +16,7 @@ import { InputIconModule } from 'primeng/inputicon';
1616import { InputTextModule } from 'primeng/inputtext' ;
1717import { MenuModule } from 'primeng/menu' ;
1818import { ProgressSpinnerModule } from 'primeng/progressspinner' ;
19- import { catchError , delay , take , tap , throwError } from 'rxjs' ;
19+ import { catchError , delay , EMPTY , switchMap , take , tap , throwError } from 'rxjs' ;
2020import { environment } from 'src/environments/environment' ;
2121
2222@Component ( {
@@ -47,124 +47,60 @@ import { environment } from 'src/environments/environment';
4747 providers : [ MessageService ]
4848} )
4949export 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' ,
0 commit comments