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
@@ -6,14 +6,7 @@ export class AuthInitializationService {
private initialized = new BehaviorSubject<boolean>(false); private initialized = new BehaviorSubject<boolean>(false);
initialized$ = this.initialized.asObservable(); initialized$ = this.initialized.asObservable();
private oidcFailed = new BehaviorSubject<boolean>(false);
oidcFailed$ = this.oidcFailed.asObservable();
markAsInitialized() { markAsInitialized() {
this.initialized.next(true); this.initialized.next(true);
} }
setOidcFailed(failed: boolean) {
this.oidcFailed.next(failed);
}
} }
+17 -24
View File
@@ -16,10 +16,7 @@ export function initializeAuthFactory() {
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
const sub = appSettingsService.appSettings$.subscribe(settings => { const sub = appSettingsService.appSettings$.subscribe(settings => {
if (!settings) return; if (settings) {
sub.unsubscribe();
if (settings.oidcEnabled && settings.oidcProviderDetails) { if (settings.oidcEnabled && settings.oidcProviderDetails) {
const details = settings.oidcProviderDetails; const details = settings.oidcProviderDetails;
@@ -36,40 +33,36 @@ export function initializeAuthFactory() {
oauthService.loadDiscoveryDocumentAndTryLogin() oauthService.loadDiscoveryDocumentAndTryLogin()
.then(() => { .then(() => {
console.log('[OIDC] Discovery document loaded and login attempted');
if (oauthService.hasValidAccessToken()) { if (oauthService.hasValidAccessToken()) {
console.log('[OIDC] Valid access token found'); authService.tokenSubject.next(oauthService.getAccessToken())
console.log('[OIDC] Valid access token found after tryLogin');
router.navigate(['/dashboard']);
oauthService.setupAutomaticSilentRefresh(); oauthService.setupAutomaticSilentRefresh();
websocketInitializer(authService); websocketInitializer(authService);
authInitService.markAsInitialized();
resolve();
} else { } else {
console.warn('[OIDC] No valid access token. Will proceed to app and show login page.'); console.log('[OIDC] No valid access token found, attempting silent login with prompt=none');
oauthService.initImplicitFlow();
resolve();
} }
}) })
.catch(err => { .catch(err => {
console.error('[OIDC] Failed to load discovery document or login:', err);
authInitService.setOidcFailed(true);
})
.finally(() => {
authInitService.markAsInitialized(); authInitService.markAsInitialized();
console.error(
'OIDC initialization failed: Unable to complete OpenID Connect discovery or login. ' +
'This may be due to an incorrect issuer URL, client ID, or network issue. ' +
'Falling back to local login. Details:', err
);
resolve(); resolve();
}); });
} else if (settings.remoteAuthEnabled) {
authService.remoteLogin().subscribe({
next: () => {
authInitService.markAsInitialized();
resolve();
},
error: err => {
console.error('[Remote Login] failed:', err);
authInitService.markAsInitialized();
resolve();
}
});
} else { } else {
authInitService.markAsInitialized(); authInitService.markAsInitialized();
resolve(); resolve();
} }
sub.unsubscribe();
}
}); });
}); });
}; };
@@ -20,11 +20,9 @@ export class AuthService {
private oAuthService = inject(OAuthService); private oAuthService = inject(OAuthService);
private router = inject(Router); 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(); 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 }> { 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( 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 { saveInternalTokens(accessToken: string, refreshToken: string): void {
localStorage.setItem('accessToken_Internal', accessToken); localStorage.setItem('accessToken_Internal', accessToken);
localStorage.setItem('refreshToken_Internal', refreshToken); localStorage.setItem('refreshToken_Internal', refreshToken);
@@ -81,14 +68,9 @@ export class AuthService {
localStorage.removeItem('accessToken_Internal'); localStorage.removeItem('accessToken_Internal');
localStorage.removeItem('refreshToken_Internal'); localStorage.removeItem('refreshToken_Internal');
this.tokenSubject.next(null); this.tokenSubject.next(null);
this.logoutSubject.next();
this.getRxStompService().deactivate(); this.getRxStompService().deactivate();
if (this.oAuthService.clientId) {
this.oAuthService.logOut();
} else {
this.router.navigate(['/login']); this.router.navigate(['/login']);
} }
}
getRxStompService(): RxStompService { getRxStompService(): RxStompService {
if (!this.rxStompService) { if (!this.rxStompService) {
@@ -3,24 +3,26 @@ import { AuthService } from './auth.service';
import {UserService} from '../../settings/user-management/user.service'; import {UserService} from '../../settings/user-management/user.service';
import {filter, catchError} from 'rxjs/operators'; import {filter, catchError} from 'rxjs/operators';
import {of} from 'rxjs'; import {of} from 'rxjs';
import {OAuthService} from 'angular-oauth2-oidc';
@Injectable({providedIn: 'root'}) @Injectable({providedIn: 'root'})
export class StartupService { export class StartupService {
private auth = inject(AuthService); private authService = inject(AuthService);
private userSvc = inject(UserService); private userService = inject(UserService);
load(): Promise<void> { load(): Promise<void> {
this.auth.token$ this.authService.token$
.pipe(filter(t => !!t)) .pipe(filter(t => !!t))
.subscribe(() => { .subscribe(() => {
this.userSvc.getMyself() this.userService.getMyself()
.pipe(catchError(() => of(null))) .pipe(catchError(() => of(null)))
.subscribe(user => { .subscribe(user => {
if (user) { if (user) {
this.userSvc.setInitialUser(user); this.userService.setInitialUser(user);
} }
}); });
}); });
return Promise.resolve(); return Promise.resolve();
} }
} }