From 763a580c5156ee25670b16a613dcf9f4d76c5c06 Mon Sep 17 00:00:00 2001 From: myoffice-sync Date: Wed, 5 Aug 2026 15:22:54 +0000 Subject: [PATCH] sync build tree from private myoffice --- MyOffice.SPA/src/app/app.config.ts | 27 +++++++---- .../authentication/authentication.module.ts | 7 ++- .../authentication/signin/signin.component.ts | 3 -- .../src/app/core/service/auth.service.ts | 45 +++++++++---------- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/MyOffice.SPA/src/app/app.config.ts b/MyOffice.SPA/src/app/app.config.ts index f04cd15..378de12 100644 --- a/MyOffice.SPA/src/app/app.config.ts +++ b/MyOffice.SPA/src/app/app.config.ts @@ -9,7 +9,7 @@ import { TranslateHttpLoader } from '@ngx-translate/http-loader'; import { LoadingBarRouterModule } from '@ngx-loading-bar/router'; import { LoadingBarHttpClientModule } from '@ngx-loading-bar/http-client'; import { NgScrollbarModule } from 'ngx-scrollbar'; -import { SocialAuthService, SocialLoginModule } from '@abacritt/angularx-social-login'; +import { SocialLoginModule } from '@abacritt/angularx-social-login'; // app import { CoreModule } from './core/core.module'; @@ -17,13 +17,31 @@ import { AppRoutingModule } from './app-routing.module'; import { ErrorInterceptor } from './core/interceptor/error.interceptor'; import { UpdateDateHttpInterceptor } from './core/interceptor/update.date.http.interceptor '; import { ExternalLoginConfig } from './config.external-login'; +import { environment } from '../environments/environment'; export function createTranslateLoader(http: HttpClient) { return new TranslateHttpLoader(http, 'assets/i18n/', '.json'); } +const socialProviders = environment.externalLogins?.google?.clientId + ? [ + importProvidersFrom(SocialLoginModule.initialize(ExternalLoginConfig.getSocialConfig())), + { + provide: 'SocialAuthServiceConfig', + useValue: ExternalLoginConfig.getSocialConfig(), + }, + ] + : [ + // Empty config so providedIn:'root' SocialAuthService (if ever constructed) does not crash. + { + provide: 'SocialAuthServiceConfig', + useValue: ExternalLoginConfig.getSocialConfig(), + }, + ]; + export const appConfig: ApplicationConfig = { providers: [ + ...socialProviders, importProvidersFrom( BrowserAnimationsModule, AppRoutingModule, @@ -38,14 +56,7 @@ export const appConfig: ApplicationConfig = { }, }), CoreModule, - SocialLoginModule, ), - // AuthService is providedIn: 'root' — config must be on the app injector (string token for lib 2.1.x) - { - provide: 'SocialAuthServiceConfig', - useValue: ExternalLoginConfig.getSocialConfig(), - }, - SocialAuthService, { provide: LocationStrategy, useClass: HashLocationStrategy }, { provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }, { provide: HTTP_INTERCEPTORS, useClass: UpdateDateHttpInterceptor, multi: true }, diff --git a/MyOffice.SPA/src/app/authentication/authentication.module.ts b/MyOffice.SPA/src/app/authentication/authentication.module.ts index 0886973..96d6903 100644 --- a/MyOffice.SPA/src/app/authentication/authentication.module.ts +++ b/MyOffice.SPA/src/app/authentication/authentication.module.ts @@ -19,6 +19,11 @@ import { SigninComponent } from './signin/signin.component'; import { SignupComponent } from './signup/signup.component'; import { LockedComponent } from './locked/locked.component'; import { ForgotPasswordComponent } from './forgot-password/forgot-password.component'; +import { environment } from '../../environments/environment'; + +const googleButtonImports = environment.externalLogins?.google?.clientId + ? [GoogleSigninButtonModule] + : []; @NgModule({ imports: [ @@ -30,7 +35,7 @@ import { ForgotPasswordComponent } from './forgot-password/forgot-password.compo MatInputModule, MatIconModule, MatButtonModule, - GoogleSigninButtonModule, + ...googleButtonImports, ], declarations: [ Page500Component, diff --git a/MyOffice.SPA/src/app/authentication/signin/signin.component.ts b/MyOffice.SPA/src/app/authentication/signin/signin.component.ts index 374e5d3..1cc07f6 100644 --- a/MyOffice.SPA/src/app/authentication/signin/signin.component.ts +++ b/MyOffice.SPA/src/app/authentication/signin/signin.component.ts @@ -3,9 +3,6 @@ import { Component, OnInit } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; -// libs -import { SocialAuthService } from '@abacritt/angularx-social-login'; - // app import { AuthService } from 'src/app/core/service/auth.service'; import { UnsubscribeOnDestroyAdapter } from 'src/app/shared/UnsubscribeOnDestroyAdapter'; diff --git a/MyOffice.SPA/src/app/core/service/auth.service.ts b/MyOffice.SPA/src/app/core/service/auth.service.ts index 2e19184..2e53a43 100644 --- a/MyOffice.SPA/src/app/core/service/auth.service.ts +++ b/MyOffice.SPA/src/app/core/service/auth.service.ts @@ -1,5 +1,5 @@ // angular -import { Injectable } from '@angular/core'; +import { Injectable, Injector } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Router } from '@angular/router'; import { ActivatedRoute } from '@angular/router'; @@ -28,6 +28,7 @@ import { UserProfileModel } from '../models/user-profile.model'; import { GeneralResultModel } from '../models/general-result.model'; import { getSafeRedirectUrl } from '../utils/safe-redirect'; import { SubjectExtensions } from '../extensions/general.extensions'; +import { environment } from '../../../environments/environment'; export function authAppInitializerFactory(authService: AuthService): () => Promise { if (window.location.href.indexOf('/silent-refresh.html') !== -1) { @@ -52,17 +53,21 @@ export class AuthService { private route: ActivatedRoute, private oidcHelperService: OidcHelperService, private auth0Service: Auth0Service, - private externalAuthService: SocialAuthService, + private injector: Injector, ) { this.currentUserSubject = new BehaviorSubject({} as UserModel); this.currentUser$ = this.currentUserSubject.asObservable(); - // on external social login - this.externalAuthService.authState.subscribe((user) => { - if (user?.idToken) { - oidcHelperService.loginByExternalLogin('google', user.idToken); - } - }); + // Lazy: SocialAuthService is providedIn:'root' and needs SocialAuthServiceConfig. + // Avoid constructor injection so password login works when Google is not configured. + if (environment.externalLogins?.google?.clientId) { + const social = this.injector.get(SocialAuthService, null); + social?.authState.subscribe((user) => { + if (user?.idToken) { + oidcHelperService.loginByExternalLogin('google', user.idToken); + } + }); + } this.oidcHelperService.isDoneLoading$ .pipe( @@ -155,19 +160,15 @@ export class AuthService { } loginGoogle(redirectUrl?: string): Observable { - return from(this.externalAuthService.signIn(GoogleLoginProvider.PROVIDER_ID)); + const social = this.injector.get(SocialAuthService, null); + if (!social) { + return throwError(() => new Error('Google login is not configured')); + } + return from(social.signIn(GoogleLoginProvider.PROVIDER_ID)); } attachGoogle(): Observable { - var result = new Subject(); - - /*this.externalAuthService.signIn(GoogleLoginProvider.PROVIDER_ID).then(function (resp) { - //console.log('attachGoogle', resp); - //return result.next(resp); - //this.attach(idToken.__raw, result); - });*/ - - return result; + return new Subject(); } loginAuth0(): Observable { @@ -182,14 +183,12 @@ export class AuthService { } subject.next({ success: login.success, error: login.error }); }); - } else { - //subject.next({ success: false, error: { description: 'Login failed' } }); } }); this.auth0Service.getAccessTokenWithPopup().subscribe(popupToken => { if (!popupToken) { - this.auth0Service.getAccessTokenSilently().subscribe(silently => { + this.auth0Service.getAccessTokenSilently().subscribe(() => { }); } }); @@ -203,7 +202,7 @@ export class AuthService { subs.unsubscribe(); this.attach(token?.__raw).subscribe( - (resp) => subject.next({ success: true, data: token?.__raw }), + () => subject.next({ success: true, data: token?.__raw }), (err) => subject.next({ success: false, data: err }) ); } else { @@ -213,7 +212,7 @@ export class AuthService { this.auth0Service.getAccessTokenWithPopup().subscribe(popupToken => { if (!popupToken) { - this.auth0Service.getAccessTokenSilently().subscribe(silently => { + this.auth0Service.getAccessTokenSilently().subscribe(() => { }); } });