style: Fix formatting and spacing in multiple files
Some checks failed
CI / Get Changed Files (pull_request) Successful in 7s
CI / prettier (pull_request) Successful in 22s
CI / Checkstyle Main (pull_request) Successful in 53s
CI / eslint (pull_request) Failing after 1m8s
CI / test-build (pull_request) Successful in 1m35s

This commit is contained in:
Jan K9f 2025-04-02 16:11:53 +02:00
commit 617654caeb
Signed by: jank
GPG key ID: B9F475106B20F144
5 changed files with 102 additions and 82 deletions

View file

@ -6,7 +6,6 @@ import { Router } from '@angular/router';
import { environment } from '../../environments/environment';
import { catchError, from, of, tap } from 'rxjs';
@Injectable({
providedIn: 'root',
})
@ -42,12 +41,13 @@ export class AuthService {
console.log('Auth service initializing');
this.oauthService.configure(this.authConfig);
this.setupEventHandling();
// Check if we're on the callback page
const hasAuthParams = window.location.search.includes('code=') ||
window.location.search.includes('token=') ||
window.location.search.includes('id_token=');
const hasAuthParams =
window.location.search.includes('code=') ||
window.location.search.includes('token=') ||
window.location.search.includes('id_token=');
if (hasAuthParams) {
console.log('Auth parameters detected in URL, processing code flow');
// We're in the OAuth callback
@ -58,37 +58,42 @@ export class AuthService {
this.checkExistingSession();
}
}
private processCodeFlow() {
// Try to exchange the authorization code for tokens
this.oauthService.tryLogin({
onTokenReceived: context => {
console.log('Token received in code flow:', context);
// Manually create a token_received event
this.handleSuccessfulLogin();
}
}).catch(err => {
console.error('Error processing code flow:', err);
});
this.oauthService
.tryLogin({
onTokenReceived: (context) => {
console.log('Token received in code flow:', context);
// Manually create a token_received event
this.handleSuccessfulLogin();
},
})
.catch((err) => {
console.error('Error processing code flow:', err);
});
}
private checkExistingSession() {
// Try login on startup
this.oauthService.loadDiscoveryDocumentAndTryLogin().then((isLoggedIn) => {
console.log('Initial login attempt result:', isLoggedIn);
if (isLoggedIn && !this.user) {
this.handleSuccessfulLogin();
}
}).catch(err => {
console.error('Error during initial login attempt:', err);
});
this.oauthService
.loadDiscoveryDocumentAndTryLogin()
.then((isLoggedIn) => {
console.log('Initial login attempt result:', isLoggedIn);
if (isLoggedIn && !this.user) {
this.handleSuccessfulLogin();
}
})
.catch((err) => {
console.error('Error during initial login attempt:', err);
});
}
private setupEventHandling() {
this.oauthService.events.subscribe((event: OAuthEvent) => {
console.log('Auth event:', event);
if (event.type === 'token_received') {
this.handleSuccessfulLogin();
} else if (event.type === 'token_refresh_error' || event.type === 'token_expires') {
@ -102,44 +107,46 @@ export class AuthService {
console.log('Token valid:', this.oauthService.hasValidAccessToken());
console.log('ID token valid:', this.oauthService.hasValidIdToken());
console.log('Access token:', this.oauthService.getAccessToken());
// Extract claims from id token if available
let claims = this.oauthService.getIdentityClaims();
console.log('ID token claims:', claims);
// If we have claims, use that as profile
if (claims && (claims['sub'] || claims['email'])) {
console.log('Using ID token claims as profile');
this.processUserProfile(claims);
return;
}
// Otherwise try to load user profile
try {
from(this.oauthService.loadUserProfile()).pipe(
tap(profile => console.log('User profile loaded:', profile)),
catchError(error => {
console.error('Error loading user profile:', error);
// If we can't load the profile but have a token, create a minimal profile
if (this.oauthService.hasValidAccessToken()) {
const token = this.oauthService.getAccessToken();
// Create a basic profile from the token
const minimalProfile = {
sub: 'user-' + Math.random().toString(36).substring(2, 10),
preferred_username: 'user' + Date.now()
};
return of({ info: minimalProfile });
from(this.oauthService.loadUserProfile())
.pipe(
tap((profile) => console.log('User profile loaded:', profile)),
catchError((error) => {
console.error('Error loading user profile:', error);
// If we can't load the profile but have a token, create a minimal profile
if (this.oauthService.hasValidAccessToken()) {
const token = this.oauthService.getAccessToken();
// Create a basic profile from the token
const minimalProfile = {
sub: 'user-' + Math.random().toString(36).substring(2, 10),
preferred_username: 'user' + Date.now(),
};
return of({ info: minimalProfile });
}
return of(null);
})
)
.subscribe((profile) => {
if (profile) {
this.processUserProfile(profile);
} else {
console.error('Could not load or create user profile');
this.router.navigate(['/']);
}
return of(null);
})
).subscribe(profile => {
if (profile) {
this.processUserProfile(profile);
} else {
console.error('Could not load or create user profile');
this.router.navigate(['/']);
}
});
});
} catch (err) {
console.error('Exception in handleSuccessfulLogin:', err);
// Try to navigate to home if we have a token anyway
@ -150,7 +157,7 @@ export class AuthService {
}
}
}
private processUserProfile(profile: any) {
this.fromUserProfile(profile).subscribe({
next: (user) => {
@ -166,7 +173,7 @@ export class AuthService {
} else {
this.router.navigate(['/']);
}
}
},
});
}
@ -174,14 +181,17 @@ export class AuthService {
console.log('Initiating login flow');
try {
// First ensure discovery document is loaded
this.oauthService.loadDiscoveryDocument().then(() => {
console.log('Discovery document loaded, starting login flow');
this.oauthService.initLoginFlow();
}).catch(err => {
console.error('Error loading discovery document:', err);
// Try login anyway with configured endpoints
this.oauthService.initLoginFlow();
});
this.oauthService
.loadDiscoveryDocument()
.then(() => {
console.log('Discovery document loaded, starting login flow');
this.oauthService.initLoginFlow();
})
.catch((err) => {
console.error('Error loading discovery document:', err);
// Try login anyway with configured endpoints
this.oauthService.initLoginFlow();
});
} catch (err) {
console.error('Exception in login:', err);
// Try direct login as a fallback
@ -222,7 +232,7 @@ export class AuthService {
getAccessToken() {
return this.oauthService.getAccessToken();
}
getUser() {
return this.user;
}