Fix OIDC auto-login not triggering in new browser tabs

This commit is contained in:
aditya.chandel
2025-08-12 22:42:14 -06:00
committed by Aditya Chandel
parent e41ecb49d0
commit 22b2c013bd
4 changed files with 59 additions and 89 deletions
@@ -20,11 +20,9 @@ export class AuthService {
private oAuthService = inject(OAuthService);
private router = inject(Router);
private tokenSubject = new BehaviorSubject<string | null>(this.getOidcAccessToken() || this.getInternalAccessToken());
public tokenSubject = new BehaviorSubject<string | null>(this.getOidcAccessToken() || this.getInternalAccessToken());
public token$ = this.tokenSubject.asObservable();
private logoutSubject = new Subject<void>();
public logout$ = this.logoutSubject.asObservable();
internalLogin(credentials: { username: string; password: string }): Observable<{ accessToken: string; refreshToken: string, isDefaultPassword: string }> {
return this.http.post<{ accessToken: string; refreshToken: string, isDefaultPassword: string }>(`${this.apiUrl}/login`, credentials).pipe(
@@ -48,17 +46,6 @@ export class AuthService {
);
}
remoteLogin(): Observable<{ accessToken: string; refreshToken: string, isDefaultPassword: string }> {
return this.http.get<{ accessToken: string; refreshToken: string, isDefaultPassword: string }>(`${this.apiUrl}/remote`).pipe(
tap((response) => {
if (response.accessToken && response.refreshToken) {
this.saveInternalTokens(response.accessToken, response.refreshToken);
this.initializeWebSocketConnection();
}
})
);
}
saveInternalTokens(accessToken: string, refreshToken: string): void {
localStorage.setItem('accessToken_Internal', accessToken);
localStorage.setItem('refreshToken_Internal', refreshToken);
@@ -81,13 +68,8 @@ export class AuthService {
localStorage.removeItem('accessToken_Internal');
localStorage.removeItem('refreshToken_Internal');
this.tokenSubject.next(null);
this.logoutSubject.next();
this.getRxStompService().deactivate();
if (this.oAuthService.clientId) {
this.oAuthService.logOut();
} else {
this.router.navigate(['/login']);
}
this.router.navigate(['/login']);
}
getRxStompService(): RxStompService {
@@ -1,26 +1,28 @@
import { Injectable, inject } from '@angular/core';
import { AuthService } from './auth.service';
import { UserService } from '../../settings/user-management/user.service';
import { filter, catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import {Injectable, inject} from '@angular/core';
import {AuthService} from './auth.service';
import {UserService} from '../../settings/user-management/user.service';
import {filter, catchError} from 'rxjs/operators';
import {of} from 'rxjs';
import {OAuthService} from 'angular-oauth2-oidc';
@Injectable({ providedIn: 'root' })
@Injectable({providedIn: 'root'})
export class StartupService {
private auth = inject(AuthService);
private userSvc = inject(UserService);
private authService = inject(AuthService);
private userService = inject(UserService);
load(): Promise<void> {
this.auth.token$
this.authService.token$
.pipe(filter(t => !!t))
.subscribe(() => {
this.userSvc.getMyself()
this.userService.getMyself()
.pipe(catchError(() => of(null)))
.subscribe(user => {
if (user) {
this.userSvc.setInitialUser(user);
this.userService.setInitialUser(user);
}
});
});
return Promise.resolve();
}
}