Skip to content

Commit e08c025

Browse files
TTA-145-refactor-login-ui-v-2 (#927)
* refactor: TTA-145 Refactor login ui v2 * TTA-145 removing not needed import script * TTA-145 removing not needed tests Co-authored-by: Jimmy Jaramillo <[email protected]>
1 parent 661a9aa commit e08c025

File tree

6 files changed

+70
-79
lines changed

6 files changed

+70
-79
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { ProjectEffects } from './modules/customer-management/components/project
4949
import { TechnologyEffects } from './modules/shared/store/technology.effects';
5050
import { ProjectTypeEffects } from './modules/customer-management/components/projects-type/store/project-type.effects';
5151
import { reducers } from './reducers';
52-
import { CLIENT_URL, environment } from '../environments/environment';
52+
import { environment } from '../environments/environment';
5353
import { EnvironmentType } from '../environments/enum';
5454
import { CustomerComponent } from './modules/customer-management/pages/customer.component';
5555
// tslint:disable-next-line: max-line-length
@@ -91,8 +91,6 @@ import { CalendarComponent } from './modules/time-entries/components/calendar/ca
9191
import { DropdownComponent } from './modules/shared/components/dropdown/dropdown.component';
9292
import { NgSelectModule } from '@ng-select/ng-select';
9393
import { DarkModeComponent } from './modules/shared/components/dark-mode/dark-mode.component';
94-
import { SocialLoginModule, SocialAuthServiceConfig } from 'angularx-social-login';
95-
import { GoogleLoginProvider } from 'angularx-social-login';
9694
import { SearchUserComponent } from './modules/shared/components/search-user/search-user.component';
9795
import { TimeRangeCustomComponent } from './modules/reports/components/time-range-custom/time-range-custom.component';
9896
import { TimeRangeHeaderComponent } from './modules/reports/components/time-range-custom/time-range-header/time-range-header.component';
@@ -201,7 +199,6 @@ const maskConfig: Partial<IConfig> = {
201199
useFactory: adapterFactory,
202200
}),
203201
NgSelectModule,
204-
SocialLoginModule
205202
],
206203
providers: [
207204
{
@@ -211,18 +208,7 @@ const maskConfig: Partial<IConfig> = {
211208
},
212209
DatePipe,
213210
CookieService,
214-
{
215-
provide: 'SocialAuthServiceConfig',
216-
useValue: {
217-
autoLogin: false,
218-
providers: [
219-
{
220-
id: GoogleLoginProvider.PROVIDER_ID,
221-
provider: new GoogleLoginProvider(CLIENT_URL)
222-
}
223-
]
224-
} as SocialAuthServiceConfig,
225-
}
211+
{provide: Window, useValue: window}
226212
],
227213
bootstrap: [AppComponent],
228214
})

src/app/modules/login/login.component.html

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@
77
<h3>Please log in</h3>
88
</div>
99

10-
<div class="login-controls">
11-
<button (click)="isProduction ? login() : loginWithGoogle() " class="btn btn-primary">login</button>
10+
<div class="login-controls" *ngIf="isProduction">
11+
<button (click)="login() " class="btn btn-primary">login</button>
12+
</div>
13+
<div *ngIf="!isProduction">
14+
<div
15+
id="g_id_onload"
16+
[attr.data-client_id]="cliendId"
17+
data-callback="handleCredentialResponse">
18+
</div>
19+
<div
20+
class="g_id_signin"
21+
data-theme="filled_blue"
22+
data-type="standard">
23+
</div>
1224
</div>
13-
1425
</div>

src/app/modules/login/login.component.spec.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,6 @@ describe('LoginComponent', () => {
100100
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
101101
}));
102102

103-
it('should sign up or login with google if is not logged-in into the app Locally', inject([Router], (router: Router) => {
104-
spyOn(loginService, 'isLogin').and.returnValue(of(false));
105-
spyOn(loginService, 'setLocalStorage').and.returnValue();
106-
spyOn(loginService, 'getUser').and.returnValue(of({token: ''}));
107-
spyOn(loginService, 'setCookies').and.returnValue();
108-
spyOn(loginService, 'signIn').and.returnValue();
109-
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());
110-
111-
component.ngOnInit();
112-
component.loginWithGoogle();
113-
114-
expect(loginService.signIn).toHaveBeenCalled();
115-
expect(loginService.setCookies).toHaveBeenCalled();
116-
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
117-
}));
118-
119103
it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => {
120104
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
121105
spyOn(router, 'navigate').and.stub();
@@ -124,11 +108,4 @@ describe('LoginComponent', () => {
124108
expect(router.navigate).toHaveBeenCalledWith(['']);
125109
}));
126110

127-
it('should not sign-up or login with google if is already logged-in into the app Locally', inject([Router], (router: Router) => {
128-
spyOn(loginService, 'isLogin').and.returnValue(of(true));
129-
spyOn(router, 'navigate').and.stub();
130-
component.loginWithGoogle();
131-
expect(loginService.isLogin).toHaveBeenCalled();
132-
expect(router.navigate).toHaveBeenCalledWith(['']);
133-
}));
134111
});
Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,85 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, NgZone } from '@angular/core';
22
import { AzureAdB2CService } from './services/azure.ad.b2c.service';
33
import { Router } from '@angular/router';
44
import { 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';
87
import { EnvironmentType } from 'src/environments/enum';
98
import { LoginService } from './services/login.service';
109
import { 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
})
1722
export 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
}

src/app/modules/login/services/login.service.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,11 @@ describe('LoginService', () => {
114114
});
115115
});
116116

117-
it('should login with social angularx-social-login', () => {
118-
service.signIn();
119-
expect(socialAuthService.signIn).toHaveBeenCalled();
120-
});
121-
122117
it('should logout with social angularx-social-login', () => {
123118
spyOn(cookieService, 'deleteAll').and.returnValue();
124119

125120
service.logout();
126121

127-
expect(socialAuthService.signOut).toHaveBeenCalled();
128122
expect(localStorage.clear).toHaveBeenCalled();
129123
expect(cookieService.deleteAll).toHaveBeenCalled();
130124
});

src/app/modules/login/services/login.service.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3-
import { GoogleLoginProvider, SocialAuthService } from 'angularx-social-login';
43
import { CookieService } from 'ngx-cookie-service';
54
import { EnvironmentType, UserEnum } from 'src/environments/enum';
65
import { environment } from 'src/environments/environment';
@@ -20,18 +19,12 @@ export class LoginService {
2019
constructor(
2120
private http?: HttpClient,
2221
private cookieService?: CookieService,
23-
private socialAuthService?: SocialAuthService
2422
) {
2523
this.baseUrl = `${environment.timeTrackerApiUrl}/users`;
2624
this.helper = new JwtHelperService();
2725
}
2826

29-
signIn() {
30-
this.socialAuthService.signIn(GoogleLoginProvider.PROVIDER_ID);
31-
}
32-
3327
logout() {
34-
this.socialAuthService.signOut();
3528
this.cookieService.deleteAll();
3629
localStorage.clear();
3730
}

0 commit comments

Comments
 (0)