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