Always show the login page, regardless of OIDC status
This commit is contained in:
committed by
Aditya Chandel
parent
9b1eb42381
commit
46db40247c
@@ -6,7 +6,14 @@ export class AuthInitializationService {
|
||||
private initialized = new BehaviorSubject<boolean>(false);
|
||||
initialized$ = this.initialized.asObservable();
|
||||
|
||||
private oidcFailed = new BehaviorSubject<boolean>(false);
|
||||
oidcFailed$ = this.oidcFailed.asObservable();
|
||||
|
||||
markAsInitialized() {
|
||||
this.initialized.next(true);
|
||||
}
|
||||
|
||||
setOidcFailed(failed: boolean) {
|
||||
this.oidcFailed.next(failed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,58 +16,59 @@ export function initializeAuthFactory() {
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
const sub = appSettingsService.appSettings$.subscribe(settings => {
|
||||
if (settings) {
|
||||
if (settings.oidcEnabled && settings.oidcProviderDetails) {
|
||||
const details = settings.oidcProviderDetails;
|
||||
if (!settings) return;
|
||||
|
||||
oauthService.configure({
|
||||
issuer: details.issuerUri,
|
||||
clientId: details.clientId,
|
||||
scope: 'openid profile email offline_access',
|
||||
redirectUri: window.location.origin + '/oauth2-callback',
|
||||
responseType: 'code',
|
||||
showDebugInformation: false,
|
||||
requireHttps: false,
|
||||
strictDiscoveryDocumentValidation: false,
|
||||
sub.unsubscribe();
|
||||
|
||||
if (settings.oidcEnabled && settings.oidcProviderDetails) {
|
||||
const details = settings.oidcProviderDetails;
|
||||
|
||||
oauthService.configure({
|
||||
issuer: details.issuerUri,
|
||||
clientId: details.clientId,
|
||||
scope: 'openid profile email offline_access',
|
||||
redirectUri: window.location.origin + '/oauth2-callback',
|
||||
responseType: 'code',
|
||||
showDebugInformation: false,
|
||||
requireHttps: false,
|
||||
strictDiscoveryDocumentValidation: false,
|
||||
});
|
||||
|
||||
oauthService.loadDiscoveryDocumentAndTryLogin()
|
||||
.then(() => {
|
||||
if (oauthService.hasValidAccessToken()) {
|
||||
console.log('[OIDC] Valid access token found');
|
||||
oauthService.setupAutomaticSilentRefresh();
|
||||
websocketInitializer(authService);
|
||||
} else {
|
||||
console.warn('[OIDC] No valid access token. Will proceed to app and show login page.');
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('[OIDC] Failed to load discovery document or login:', err);
|
||||
authInitService.setOidcFailed(true);
|
||||
})
|
||||
.finally(() => {
|
||||
authInitService.markAsInitialized();
|
||||
resolve();
|
||||
});
|
||||
|
||||
oauthService.loadDiscoveryDocumentAndTryLogin()
|
||||
.then(() => {
|
||||
console.log('[OIDC] Discovery document loaded and login attempted');
|
||||
if (oauthService.hasValidAccessToken()) {
|
||||
console.log('[OIDC] Valid access token found after tryLogin');
|
||||
router.navigate(['/dashboard']);
|
||||
oauthService.setupAutomaticSilentRefresh();
|
||||
websocketInitializer(authService);
|
||||
authInitService.markAsInitialized();
|
||||
resolve();
|
||||
} else {
|
||||
console.log('[OIDC] No valid access token found, attempting silent login with prompt=none');
|
||||
oauthService.initCodeFlow();
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
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();
|
||||
});
|
||||
} else if (settings.remoteAuthEnabled) {
|
||||
authService.remoteLogin().subscribe({
|
||||
next: () => {
|
||||
resolve();
|
||||
},
|
||||
error: resolve
|
||||
});
|
||||
} else {
|
||||
authInitService.markAsInitialized();
|
||||
resolve();
|
||||
}
|
||||
sub.unsubscribe();
|
||||
} else if (settings.remoteAuthEnabled) {
|
||||
authService.remoteLogin().subscribe({
|
||||
next: () => {
|
||||
authInitService.markAsInitialized();
|
||||
resolve();
|
||||
},
|
||||
error: err => {
|
||||
console.error('[Remote Login] failed:', err);
|
||||
authInitService.markAsInitialized();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
authInitService.markAsInitialized();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ import {Component, inject, OnInit} from '@angular/core';
|
||||
import {Router} from '@angular/router';
|
||||
import {OAuthService} from 'angular-oauth2-oidc';
|
||||
import {AuthService} from '../../service/auth.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
|
||||
@Component({
|
||||
selector: 'app-oidc-callback',
|
||||
@@ -9,28 +10,29 @@ import {AuthService} from '../../service/auth.service';
|
||||
styleUrls: ['./oidc-callback.component.scss']
|
||||
})
|
||||
export class OidcCallbackComponent implements OnInit {
|
||||
|
||||
private router = inject(Router);
|
||||
private oAuthService = inject(OAuthService);
|
||||
private authService = inject(AuthService);
|
||||
private oauthService = inject(OAuthService);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
ngOnInit(): void {
|
||||
/*this.oAuthService.tryLoginCodeFlow().then(info => {
|
||||
if (this.oAuthService.hasValidAccessToken()) {
|
||||
const accessToken = this.oAuthService.getAccessToken();
|
||||
const refreshToken = this.oAuthService.getRefreshToken();
|
||||
this.authService.saveOidcTokens(accessToken, refreshToken ?? '');
|
||||
|
||||
this.authService.getRxStompService().activate();
|
||||
this.oAuthService.setupAutomaticSilentRefresh();
|
||||
async ngOnInit(): Promise<void> {
|
||||
try {
|
||||
await this.oauthService.tryLoginCodeFlow();
|
||||
if (this.oauthService.hasValidAccessToken()) {
|
||||
this.router.navigate(['/dashboard']);
|
||||
} else {
|
||||
console.error('OIDC code exchange failed');
|
||||
this.router.navigate(['/login']);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('OIDC error:', err);
|
||||
this.router.navigate(['/login']);
|
||||
});*/
|
||||
} catch (e) {
|
||||
console.error('[OIDC Callback] Login failed', e);
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'OIDC Login Failed',
|
||||
detail: 'Redirecting to local login...',
|
||||
life: 3000
|
||||
});
|
||||
setTimeout(() => {
|
||||
this.router.navigate(['/login']);
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user