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
parent fa09a8533f
commit 617654caeb
Signed by: jank
GPG key ID: B9F475106B20F144
5 changed files with 102 additions and 82 deletions

View file

@ -9,7 +9,7 @@ import { OAuthStorage, provideOAuthClient } from 'angular-oauth2-oidc';
import { httpInterceptor } from './shared/interceptor/http.interceptor';
function storageFactory() {
return new class implements OAuthStorage {
return new (class implements OAuthStorage {
private data: { [key: string]: string } = {};
getItem(key: string): string | null {
@ -17,13 +17,13 @@ function storageFactory() {
}
removeItem(key: string): void {
delete this.data[key]
delete this.data[key];
}
setItem(key: string, data: string): void {
this.data[key] = data;
}
}
})();
}
export const appConfig: ApplicationConfig = {
@ -37,6 +37,6 @@ export const appConfig: ApplicationConfig = {
{
provide: OAuthStorage,
useFactory: () => localStorage,
}
},
],
};

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',
})
@ -44,9 +43,10 @@ export class AuthService {
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');
@ -61,28 +61,33 @@ export class AuthService {
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);
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);
});
if (isLoggedIn && !this.user) {
this.handleSuccessfulLogin();
}
})
.catch((err) => {
console.error('Error during initial login attempt:', err);
});
}
private setupEventHandling() {
@ -116,30 +121,32 @@ export class AuthService {
// 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
@ -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

View file

@ -35,10 +35,12 @@ export class UserService {
}
public createUser(id: string, username: string): Observable<User> {
return this.http.post<User>('/backend/user', {
authentikId: id,
username: username,
}).pipe(tap((user) => this.currentUserSubject.next(user)));
return this.http
.post<User>('/backend/user', {
authentikId: id,
username: username,
})
.pipe(tap((user) => this.currentUserSubject.next(user)));
}
public getOrCreateUser(profile: any): Observable<User> {
@ -46,7 +48,11 @@ export class UserService {
// Authentik format might differ from Keycloak
// Check different possible locations for the ID and username
const id = profile.info?.sub || profile['sub'];
const username = profile.info?.preferred_username || profile['preferred_username'] || profile['email'] || profile['name'];
const username =
profile.info?.preferred_username ||
profile['preferred_username'] ||
profile['email'] ||
profile['name'];
if (!id || !username) {
console.error('Could not extract user ID or username from profile', profile);

View file

@ -6,13 +6,15 @@ export const httpInterceptor: HttpInterceptorFn = (req, next) => {
const oauthStorage = inject(OAuthStorage);
if (oauthStorage.getItem('access_token')) {
return next(req.clone({
setHeaders: {
'Authorization': 'Bearer ' + oauthStorage.getItem('access_token'),
'Access-Control-Allow-Origin': '*',
'Referrer-Policy': 'no-referrer',
}
}));
return next(
req.clone({
setHeaders: {
Authorization: 'Bearer ' + oauthStorage.getItem('access_token'),
'Access-Control-Allow-Origin': '*',
'Referrer-Policy': 'no-referrer',
},
})
);
} else {
return next(req);
}

View file

@ -1,5 +1,7 @@
export const environment = {
STRIPE_KEY: 'pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG',
STRIPE_KEY:
'pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG',
OAUTH_CLIENT_ID: 'MDqjm1kcWKuZfqHJXjxwAV20i44aT7m4VhhTL3Nm',
OAUTH_CLIENT_SECRET: 'GY2F8te6iAVYt1TNAUVLzWZEXb6JoMNp6chbjqaXNq4gS5xTDL54HqBiAlV1jFKarN28LQ7FUsYX4SbwjfEhZhgeoKuBnZKjR9eiu7RawnGgxIK9ffvUfMkjRxnmiGI5'
OAUTH_CLIENT_SECRET:
'GY2F8te6iAVYt1TNAUVLzWZEXb6JoMNp6chbjqaXNq4gS5xTDL54HqBiAlV1jFKarN28LQ7FUsYX4SbwjfEhZhgeoKuBnZKjR9eiu7RawnGgxIK9ffvUfMkjRxnmiGI5',
};