@@ -2,6 +2,9 @@ import { Injectable } from '@angular/core';
22import { HttpClient , HttpParams , HttpHeaders } from '@angular/common/http' ;
33import { ActivatedRoute , Router } from '@angular/router' ;
44
5+ import { AlertService } from '../../app/core/alert/alert.service' ;
6+ import { TranslateService } from '@ngx-translate/core' ;
7+
58/** Custom Services */
69import { AuthenticationService } from '../core/authentication/authentication.service' ;
710import { Credentials } from '../core/authentication/credentials.model' ;
@@ -18,12 +21,13 @@ export class AuthService {
1821 private frontUrl = environment . OIDC . oidcFrontUrl ;
1922 private redirectUri = `${ this . frontUrl } #/callback` ;
2023 private refreshTimeoutId : any = null ;
21-
2224 constructor (
2325 private authenticationService : AuthenticationService ,
2426 private http : HttpClient ,
2527 private route : ActivatedRoute ,
26- private router : Router
28+ private router : Router ,
29+ private alertService : AlertService ,
30+ private translateService : TranslateService
2731 ) { }
2832
2933 async login ( ) {
@@ -169,7 +173,12 @@ export class AuthService {
169173 } )
170174 . then ( ( res ) => {
171175 if ( ! res . ok ) {
172- throw new Error ( 'Error retrieving user data from backend' ) ;
176+ this . alertService . alert ( {
177+ type : 'User Details' ,
178+ message : this . translateService . instant ( 'errors.Username or password incorrect.' )
179+ } ) ;
180+ this . sesionEnd ( ) ;
181+ return false ;
173182 }
174183 return res . json ( ) ;
175184 } )
@@ -180,7 +189,7 @@ export class AuthService {
180189 window . location . href = '/#/home' ;
181190 } )
182191 . catch ( ( error ) => {
183- console . error ( 'Error consuming backend:' , error ) ;
192+ this . alertService . alert ( { type : 'User Details' , message : error } ) ;
184193 } ) ;
185194 }
186195
@@ -378,4 +387,46 @@ export class AuthService {
378387 this . refreshToken ( ) ;
379388 } , refreshInMs ) ;
380389 }
390+
391+ async sesionEnd ( ) {
392+ const idToken = localStorage . getItem ( 'id_token' ) ;
393+ const postLogoutRedirectUri = this . frontUrl + '#/login' ;
394+
395+ if ( this . refreshTimeoutId ) {
396+ clearTimeout ( this . refreshTimeoutId ) ;
397+ this . refreshTimeoutId = null ;
398+ }
399+
400+ if ( ! idToken ) {
401+ window . location . href = postLogoutRedirectUri ;
402+ return ;
403+ }
404+
405+ const logoutUrl = `${ this . baseUrl } oidc/v1/end_session?id_token_hint=${ idToken } &post_logout_redirect_uri=${ encodeURIComponent ( postLogoutRedirectUri ) } ` ;
406+
407+ try {
408+ const response = await fetch ( logoutUrl , {
409+ method : 'GET' ,
410+ credentials : 'include'
411+ } ) ;
412+
413+ if ( ! response . ok ) {
414+ console . error ( 'Error en logout:' , response . status , response . statusText ) ;
415+ }
416+ } catch ( error ) {
417+ console . error ( 'Error de red en logout:' , error ) ;
418+ } finally {
419+ sessionStorage . removeItem ( 'mifosXCredentials' ) ;
420+ sessionStorage . removeItem ( 'mifosXZitadelTokenDetails' ) ;
421+ localStorage . removeItem ( 'access_token' ) ;
422+ localStorage . removeItem ( 'expires_in' ) ;
423+ localStorage . removeItem ( 'id_token' ) ;
424+ localStorage . removeItem ( 'refresh_token' ) ;
425+ localStorage . removeItem ( 'refresh_expires_in' ) ;
426+ localStorage . removeItem ( 'token_start_time' ) ;
427+ localStorage . removeItem ( 'code_verifier' ) ;
428+ localStorage . removeItem ( 'mifosXZitadel' ) ;
429+ localStorage . removeItem ( 'auth_code' ) ;
430+ }
431+ }
381432}
0 commit comments