1- import { Component , OnInit } from '@angular/core' ;
1+ import { Component , OnInit , NgZone } from '@angular/core' ;
22import { AzureAdB2CService } from './services/azure.ad.b2c.service' ;
33import { Router } from '@angular/router' ;
44import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-toggle-cookies/feature-toggle-cookies.service' ;
55
6- import { SocialAuthService , SocialUser } from 'angularx-social-login' ;
7- import { environment } from 'src/environments/environment' ;
6+ import { environment , CLIENT_URL } from 'src/environments/environment' ;
87import { EnvironmentType } from 'src/environments/enum' ;
98import { LoginService } from './services/login.service' ;
109import { UserService } from '../user/services/user.service' ;
1110
11+ declare global {
12+ interface Window {
13+ handleCredentialResponse : ( response : any ) => void ;
14+ }
15+ }
16+
1217@Component ( {
1318 selector : 'app-login' ,
1419 templateUrl : './login.component.html' ,
1520 styleUrls : [ './login.component.scss' ] ,
1621} )
1722export class LoginComponent implements OnInit {
18- socialUser : SocialUser ;
1923 isProduction = environment . production === EnvironmentType . TT_PROD_LEGACY ;
24+ cliendId = CLIENT_URL ;
25+ auth2 : any ;
26+
2027
2128 constructor (
2229 private azureAdB2CService : AzureAdB2CService ,
2330 private router : Router ,
2431 private featureToggleCookiesService : FeatureToggleCookiesService ,
25- private socialAuthService : SocialAuthService ,
2632 private loginService ?: LoginService ,
27- private userService ?: UserService
33+ private userService ?: UserService ,
34+ private ngZone ?: NgZone
2835 ) { }
2936
37+
38+ googleAuthSDK ( ) {
39+
40+ ( < any > window ) [ 'googleSDKLoaded' ] = ( ) => {
41+ ( < any > window ) [ 'gapi' ] . load ( 'auth2' , ( ) => {
42+ this . auth2 = ( < any > window ) [ 'gapi' ] . auth2 . init ( {
43+ client_id : this . cliendId ,
44+ plugin_name :'login' ,
45+ cookiepolicy : 'single_host_origin' ,
46+ scope : 'profile email'
47+ } ) ;
48+ } ) ;
49+ }
50+ ( function ( d , s , id ) {
51+ var js , gjs = d . getElementsByTagName ( s ) [ 1 ] ;
52+ if ( d . getElementById ( id ) ) { return ; }
53+ js = d . createElement ( s ) ; js . id = id ;
54+ js . src = "https://accounts.google.com/gsi/client" ;
55+ gjs . parentNode . insertBefore ( js , gjs ) ;
56+ } ( document , 'script' , 'async defer' ) ) ;
57+ }
58+
3059 ngOnInit ( ) {
31- this . socialAuthService . authState . subscribe ( ( user ) => {
32- if ( user != null ) {
33- this . featureToggleCookiesService . setCookies ( ) ;
34- this . loginService . setLocalStorage ( 'idToken' , user . idToken ) ;
35- this . loginService . getUser ( user . idToken ) . subscribe ( ( response ) => {
60+
61+ this . googleAuthSDK ( ) ;
62+ if ( this . isProduction && this . azureAdB2CService . isLogin ( ) ) {
63+ this . router . navigate ( [ '' ] ) ;
64+ } else {
65+ this . loginService . isLogin ( ) . subscribe ( isLogin => {
66+ if ( isLogin ) {
67+ this . router . navigate ( [ '' ] ) ;
68+ }
69+ } ) ;
70+ }
71+ window . handleCredentialResponse = ( response ) => {
72+ const { credential = '' } = response ;
73+ this . featureToggleCookiesService . setCookies ( ) ;
74+ this . loginService . setLocalStorage ( 'idToken' , credential ) ;
75+ this . loginService . getUser ( credential ) . subscribe ( ( response ) => {
3676 this . loginService . setCookies ( ) ;
3777 const tokenObject = JSON . stringify ( response ) ;
3878 const tokenJson = JSON . parse ( tokenObject ) ;
3979 this . loginService . setLocalStorage ( 'user' , tokenJson . token ) ;
40- this . router . navigate ( [ '' ] ) ;
41- } ) ;
42- }
43- } ) ;
80+ this . ngZone . run ( ( ) => this . router . navigate ( [ '' ] ) ) ;
81+ } ) ;
82+ }
4483 }
4584
4685 login ( ) : void {
@@ -62,13 +101,4 @@ export class LoginComponent implements OnInit {
62101 }
63102 }
64103
65- loginWithGoogle ( ) {
66- this . loginService . isLogin ( ) . subscribe ( isLogin => {
67- if ( isLogin ) {
68- this . router . navigate ( [ '' ] ) ;
69- } else {
70- this . loginService . signIn ( ) ;
71- }
72- } ) ;
73- }
74104}
0 commit comments