refactor: clean up whitespace and formatting in authentication service

This commit is contained in:
Marek Lesko
2025-11-07 20:21:07 +00:00
parent 89ac32cd07
commit 441b00b510

View File

@@ -27,17 +27,17 @@ export class AuthenticationService {
private getRedirectUri(): string { private getRedirectUri(): string {
// Use the current origin + callback path // Use the current origin + callback path
const origin = window.location.origin; const origin = window.location.origin;
// For development/testing environments, ensure we use the right callback // For development/testing environments, ensure we use the right callback
if (origin.includes('localhost') || origin.includes('127.0.0.1')) { if (origin.includes('localhost') || origin.includes('127.0.0.1')) {
return `${origin}/authentication/callback`; return `${origin}/authentication/callback`;
} }
// For Gitpod/Codespaces or other cloud IDEs // For Gitpod/Codespaces or other cloud IDEs
if (origin.includes('gitpod.io') || origin.includes('github.dev') || origin.includes('codespaces')) { if (origin.includes('gitpod.io') || origin.includes('github.dev') || origin.includes('codespaces')) {
return `${origin}/authentication/callback`; return `${origin}/authentication/callback`;
} }
// Default fallback // Default fallback
return `${origin}/authentication/callback`; return `${origin}/authentication/callback`;
} }
@@ -47,12 +47,12 @@ export class AuthenticationService {
public user$ = this.userSubject.asObservable(); public user$ = this.userSubject.asObservable();
constructor( constructor(
private oauthService: OAuthService, private oauthService: OAuthService,
private router: Router, private router: Router,
private http: HttpClient, private http: HttpClient,
private toastr: ToastrService, private toastr: ToastrService,
private configService: AppConfigService private configService: AppConfigService
) { ) {
this.loadStoredUser(); this.loadStoredUser();
} }
@@ -82,8 +82,8 @@ export class AuthenticationService {
// Ensure computed properties are present (in case they were stored without them) // Ensure computed properties are present (in case they were stored without them)
const enhancedUser: UserProfile = { const enhancedUser: UserProfile = {
...user, ...user,
name: user.name || (user.firstName && user.lastName name: user.name || (user.firstName && user.lastName
? `${user.firstName} ${user.lastName}` ? `${user.firstName} ${user.lastName}`
: user.firstName || user.lastName || user.email), : user.firstName || user.lastName || user.email),
picture: user.picture || user.profilePictureUrl || '/assets/images/profile/user-1.jpg', picture: user.picture || user.profilePictureUrl || '/assets/images/profile/user-1.jpg',
role: user.role || 'Používateľ' role: user.role || 'Používateľ'
@@ -101,13 +101,13 @@ export class AuthenticationService {
// Populate computed properties for template compatibility // Populate computed properties for template compatibility
const enhancedUser: UserProfile = { const enhancedUser: UserProfile = {
...user, ...user,
name: user.firstName && user.lastName name: user.firstName && user.lastName
? `${user.firstName} ${user.lastName}` ? `${user.firstName} ${user.lastName}`
: user.firstName || user.lastName || user.email, : user.firstName || user.lastName || user.email,
picture: user.profilePictureUrl || '/assets/images/profile/user-1.jpg', // Default avatar picture: user.profilePictureUrl || '/assets/images/profile/user-1.jpg', // Default avatar
role: 'Používateľ' // Default role in Slovak role: 'Používateľ' // Default role in Slovak
}; };
localStorage.setItem(USER_KEY, JSON.stringify(enhancedUser)); localStorage.setItem(USER_KEY, JSON.stringify(enhancedUser));
this.profile = enhancedUser; this.profile = enhancedUser;
this.userSubject.next(enhancedUser); this.userSubject.next(enhancedUser);
@@ -148,10 +148,10 @@ export class AuthenticationService {
// Start login flow using discovery document + Authorization Code (PKCE) // Start login flow using discovery document + Authorization Code (PKCE)
startLogin(cfg?: Partial<AuthConfig>): Promise<void> { startLogin(cfg?: Partial<AuthConfig>): Promise<void> {
if (cfg) this.configure(cfg); if (cfg) this.configure(cfg);
console.log('OAuth Config:', this.config); console.log('OAuth Config:', this.config);
console.log('Redirect URI:', this.config.redirectUri); console.log('Redirect URI:', this.config.redirectUri);
return this.oauthService return this.oauthService
.loadDiscoveryDocument() .loadDiscoveryDocument()
.then(() => { .then(() => {
@@ -170,7 +170,7 @@ export class AuthenticationService {
try { try {
// Process OAuth callback to get ID token // Process OAuth callback to get ID token
const isLoggedIn = await this.oauthService.loadDiscoveryDocumentAndTryLogin(); const isLoggedIn = await this.oauthService.loadDiscoveryDocumentAndTryLogin();
if (!isLoggedIn && !this.oauthService.hasValidAccessToken()) { if (!isLoggedIn && !this.oauthService.hasValidAccessToken()) {
throw new Error('No valid token after callback'); throw new Error('No valid token after callback');
} }
@@ -186,10 +186,10 @@ export class AuthenticationService {
// Determine the provider based on the current OAuth configuration // Determine the provider based on the current OAuth configuration
const provider = this.determineProvider(); const provider = this.determineProvider();
// Call our API to authenticate and get custom access token // Call our API to authenticate and get custom access token
const authResponse = await this.authenticateWithApi(idToken, provider, accessToken); const authResponse = await this.authenticateWithApi(idToken, provider, accessToken);
// Save the custom access token and user profile // Save the custom access token and user profile
this.saveCustomToken(authResponse.accessToken); this.saveCustomToken(authResponse.accessToken);
this.saveUser(authResponse.user); this.saveUser(authResponse.user);
@@ -228,10 +228,10 @@ export class AuthenticationService {
...(accessToken && { accessToken }) // Only include accessToken if it exists ...(accessToken && { accessToken }) // Only include accessToken if it exists
}; };
console.log('Authenticating with API:', { console.log('Authenticating with API:', {
provider, provider,
hasIdToken: !!idToken, hasIdToken: !!idToken,
hasAccessToken: !!accessToken hasAccessToken: !!accessToken
}); });
try { try {
@@ -281,7 +281,7 @@ export class AuthenticationService {
if (!providerConfig) { if (!providerConfig) {
throw new Error('Microsoft OAuth configuration not found'); throw new Error('Microsoft OAuth configuration not found');
} }
const microsoftConfig: Partial<AuthConfig> = { const microsoftConfig: Partial<AuthConfig> = {
issuer: providerConfig.issuer, issuer: providerConfig.issuer,
clientId: providerConfig.clientId, clientId: providerConfig.clientId,
@@ -295,15 +295,16 @@ export class AuthenticationService {
if (!providerConfig) { if (!providerConfig) {
throw new Error('Google OAuth configuration not found'); throw new Error('Google OAuth configuration not found');
} }
const googleConfig: Partial<AuthConfig> = { const googleConfig: Partial<AuthConfig> = {
issuer: providerConfig.issuer, issuer: providerConfig.issuer,
clientId: providerConfig.clientId, clientId: providerConfig.clientId,
scope: 'openid profile email', scope: 'openid profile email',
dummyClientSecret: providerConfig.dummyClientSecret,
// Override redirect URI for Google to match what might be registered // Override redirect URI for Google to match what might be registered
redirectUri: `${window.location.origin}/authentication/callback` redirectUri: `${window.location.origin}/authentication/callback`
}; };
console.log('Google OAuth Config:', googleConfig); console.log('Google OAuth Config:', googleConfig);
return this.startLogin(googleConfig); return this.startLogin(googleConfig);
} }
@@ -313,7 +314,7 @@ export class AuthenticationService {
if (!providerConfig) { if (!providerConfig) {
throw new Error('PocketId OAuth configuration not found'); throw new Error('PocketId OAuth configuration not found');
} }
const pocketIdConfig: Partial<AuthConfig> = { const pocketIdConfig: Partial<AuthConfig> = {
issuer: providerConfig.issuer, issuer: providerConfig.issuer,
clientId: providerConfig.clientId, clientId: providerConfig.clientId,
@@ -326,7 +327,7 @@ export class AuthenticationService {
hasValidCustomToken(): boolean { hasValidCustomToken(): boolean {
const token = this.getCustomAccessToken(); const token = this.getCustomAccessToken();
if (!token) return false; if (!token) return false;
try { try {
// Basic JWT expiration check // Basic JWT expiration check
const payload = JSON.parse(atob(token.split('.')[1])); const payload = JSON.parse(atob(token.split('.')[1]));
@@ -377,7 +378,7 @@ export class AuthenticationService {
} }
this.clearAuth(); this.clearAuth();
if (destroyLocalSession) { if (destroyLocalSession) {
this.oauthService.logOut(); this.oauthService.logOut();
} }
@@ -392,7 +393,7 @@ export class AuthenticationService {
localStorage.removeItem(TOKEN_KEY); localStorage.removeItem(TOKEN_KEY);
localStorage.removeItem(USER_KEY); localStorage.removeItem(USER_KEY);
} catch { } } catch { }
this.profile = null; this.profile = null;
this.userSubject.next(null); this.userSubject.next(null);
} }