11import { Component , OnInit , NgZone } from '@angular/core' ;
2- import { AzureAdB2CService } from './services/azure.ad.b2c.service' ;
32import { Router } from '@angular/router' ;
43import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-toggle-cookies/feature-toggle-cookies.service' ;
54
@@ -20,87 +19,20 @@ declare global {
2019 styleUrls : [ './login.component.scss' ] ,
2120} )
2221export class LoginComponent implements OnInit {
23- isProduction = environment . production === EnvironmentType . TT_PROD_LEGACY ;
2422 cliendId = CLIENT_URL ;
2523 auth2 : any ;
2624
2725
2826 constructor (
29- private azureAdB2CService : AzureAdB2CService ,
3027 private router : Router ,
3128 private featureToggleCookiesService : FeatureToggleCookiesService ,
3229 private loginService ?: LoginService ,
3330 private userService ?: UserService ,
3431 private ngZone ?: NgZone
3532 ) { }
3633
37-
38- googleAuthSDK ( ) {
39- const sdkLoaded = 'googleSDKLoaded' ;
40- const gapi = 'gapi' ;
41-
42- ( window as any ) [ sdkLoaded ] = ( ) => {
43- ( window as any ) [ gapi ] . load ( 'auth2' , ( ) => {
44- this . auth2 = ( window as any ) [ gapi ] . auth2 . init ( {
45- client_id : this . cliendId ,
46- plugin_name : 'login' ,
47- cookiepolicy : 'single_host_origin' ,
48- scope : 'profile email'
49- } ) ;
50- } ) ;
51- } ;
52-
53- ( async ( d , s , id ) => {
54- const keyGoogle = 'src' ;
55- const gjs = d . getElementsByTagName ( s ) [ 1 ] ;
56- let js = gjs ;
57- if ( d . getElementById ( id ) ) { return ; }
58- js = d . createElement ( s ) ; js . id = id ;
59- js [ keyGoogle ] = 'https://accounts.google.com/gsi/client' ;
60- gjs . parentNode . insertBefore ( js , gjs ) ;
61- } ) ( document , 'script' , 'async defer' ) ;
62- }
63-
6434 ngOnInit ( ) {
65-
66- this . googleAuthSDK ( ) ;
67- this . loginService . isLogin ( ) . subscribe ( isLogin => {
68- if ( isLogin ) {
69- this . router . navigate ( [ '' ] ) ;
70- }
71- } ) ;
72-
73- window . handleCredentialResponse = ( response ) => {
74- const { credential = '' } = response ;
75- this . featureToggleCookiesService . setCookies ( ) ;
76- this . loginService . setLocalStorage ( 'idToken' , credential ) ;
77- this . loginService . getUser ( credential ) . subscribe ( ( resp ) => {
78- this . loginService . setCookies ( ) ;
79- const tokenObject = JSON . stringify ( resp ) ;
80- const tokenJson = JSON . parse ( tokenObject ) ;
81- this . loginService . setLocalStorage ( 'user' , tokenJson . token ) ;
82- this . ngZone . run ( ( ) => this . router . navigate ( [ '' ] ) ) ;
83- } ) ;
84- } ;
85- }
86-
87- login ( ) : void {
88- if ( this . azureAdB2CService . isLogin ( ) ) {
89- this . router . navigate ( [ '' ] ) ;
90- } else {
91- this . azureAdB2CService . signIn ( ) . subscribe ( ( ) => {
92- this . featureToggleCookiesService . setCookies ( ) ;
93- this . azureAdB2CService . setCookies ( ) ;
94- const userId = this . azureAdB2CService . getUserId ( ) ;
95- this . userService . loadUser ( userId ) . subscribe ( ( user ) => {
96- const userGroups = {
97- groups : user . groups
98- } ;
99- this . loginService . setLocalStorage ( 'user' , JSON . stringify ( userGroups ) ) ;
100- this . router . navigate ( [ '' ] ) ;
101- } ) ;
102- } ) ;
103- }
35+ this . loginService . fetchAndCheckUserPermissions ( ) ;
10436 }
10537
10638}
0 commit comments