Skip to content

Commit a5f6b0d

Browse files
author
Reihtw
authored
Merge 1ccac53 into c21ed5a
2 parents c21ed5a + 1ccac53 commit a5f6b0d

19 files changed

+33274
-381
lines changed

package-lock.json

Lines changed: 33009 additions & 207 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@angular/platform-browser": "10.2.2",
2727
"@angular/platform-browser-dynamic": "10.2.2",
2828
"@angular/router": "10.2.2",
29+
"@auth0/angular-jwt": "^5.0.2",
2930
"@azure/app-configuration": "1.1.0",
3031
"@azure/identity": "1.1.0",
3132
"@ng-select/ng-select": "7.2.0",

src/app/app.module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { ActivityEffects } from './modules/activities-management/store/activity-
4848
import { ProjectEffects } from './modules/customer-management/components/projects/components/store/project.effects';
4949
import { TechnologyEffects } from './modules/shared/store/technology.effects';
5050
import { ProjectTypeEffects } from './modules/customer-management/components/projects-type/store/project-type.effects';
51-
import { reducers, metaReducers } from './reducers';
51+
import { reducers } from './reducers';
5252
import { CLIENT_URL, environment } from '../environments/environment';
5353
import { EnvironmentType } from '../environments/enum';
5454
import { CustomerComponent } from './modules/customer-management/pages/customer.component';
@@ -179,7 +179,6 @@ const maskConfig: Partial<IConfig> = {
179179
MatIconModule,
180180
MatListModule,
181181
StoreModule.forRoot(reducers, {
182-
metaReducers,
183182
}),
184183
environment.production === EnvironmentType.TT_DEV
185184
? StoreDevtoolsModule.instrument({

src/app/guards/login-guard/login.guard.spec.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LoginGuard } from './login.guard';
77
import { LoginService } from '../../modules/login/services/login.service';
88
import { HttpClientTestingModule } from '@angular/common/http/testing';
99
import { SocialAuthService } from 'angularx-social-login';
10+
import { of } from 'rxjs';
1011

1112

1213
describe('LoginGuard', () => {
@@ -15,13 +16,13 @@ describe('LoginGuard', () => {
1516
let azureAdB2CService: AzureAdB2CService;
1617
const azureAdB2CServiceStub = {
1718
isLogin() {
18-
return true;
19+
return of(true);
1920
}
2021
};
2122
let loginService: LoginService;
2223
const loginServiceStub = {
2324
isLogin() {
24-
return true;
25+
return of(true);
2526
}
2627
};
2728
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['']);
@@ -45,37 +46,41 @@ describe('LoginGuard', () => {
4546

4647
it('can activate the route when user is logged-in on Production', () => {
4748
loginGuard.isProduction = true;
48-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
49-
const canActivate = loginGuard.canActivate();
49+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
50+
loginGuard.canActivate().subscribe(isLogin => {
51+
expect(isLogin).toEqual(true);
52+
});
5053
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
51-
expect(canActivate).toEqual(true);
5254
});
5355

5456
it('can activate the route when user is logged-in Locally', () => {
5557
loginGuard.isProduction = false;
56-
spyOn(loginService, 'isLogin').and.returnValue(true);
57-
const canActivate = loginGuard.canActivate();
58+
spyOn(loginService, 'isLogin').and.returnValue(of(true));
59+
loginGuard.canActivate().subscribe(isLogin => {
60+
expect(isLogin).toEqual(true);
61+
});
5862
expect(loginService.isLogin).toHaveBeenCalled();
59-
expect(canActivate).toEqual(true);
6063
});
6164

6265
it('can not active the route and is redirected to login if user is not logged-in on Production', inject([Router], (router: Router) => {
6366
loginGuard.isProduction = true;
64-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
67+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
6568
spyOn(router, 'navigate').and.stub();
66-
const canActivate = loginGuard.canActivate();
69+
loginGuard.canActivate().subscribe(isLogin => {
70+
expect(isLogin).toEqual(false);
71+
});
6772
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
68-
expect(canActivate).toEqual(false);
6973
expect(router.navigate).toHaveBeenCalledWith(['login']);
7074
}));
7175

7276
it('can not active the route and is redirected to login if user is not logged-in Locally', inject([Router], (router: Router) => {
7377
loginGuard.isProduction = false;
74-
spyOn(loginService, 'isLogin').and.returnValue(false);
78+
spyOn(loginService, 'isLogin').and.returnValue(of(false));
7579
spyOn(router, 'navigate').and.stub();
76-
const canActivate = loginGuard.canActivate();
80+
loginGuard.canActivate().subscribe(isLogin => {
81+
expect(isLogin).toEqual(false);
82+
});
7783
expect(loginService.isLogin).toHaveBeenCalled();
78-
expect(canActivate).toEqual(false);
7984
expect(router.navigate).toHaveBeenCalledWith(['login']);
8085
}));
8186

src/app/guards/login-guard/login.guard.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Router, CanActivate } from '@angular/router';
33
import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service';
44
import { LoginService } from '../../modules/login/services/login.service';
55
import { environment } from 'src/environments/environment';
6+
import { map } from 'rxjs/operators';
67
import { EnvironmentType } from 'src/environments/enum';
78

89

@@ -19,21 +20,27 @@ export class LoginGuard implements CanActivate {
1920

2021
canActivate() {
2122
if (this.isProduction) {
22-
if (this.azureAdB2CService.isLogin()) {
23-
this.azureAdB2CService.setCookies();
24-
return true;
25-
} else {
26-
this.router.navigate(['login']);
27-
return false;
28-
}
23+
return this.azureAdB2CService.isLogin().pipe(
24+
map(isLogin => {
25+
if (!isLogin) {
26+
this.router.navigate(['login']);
27+
return false;
28+
}
29+
this.azureAdB2CService.setCookies();
30+
return true;
31+
})
32+
);
2933
} else {
30-
if (this.loginService.isLogin()) {
31-
this.loginService.setCookies();
32-
return true;
33-
} else {
34-
this.router.navigate(['login']);
35-
return false;
36-
}
34+
return this.loginService.isLogin().pipe(
35+
map(isLogin => {
36+
if (!isLogin) {
37+
this.router.navigate(['login']);
38+
return false;
39+
}
40+
this.loginService.setCookies();
41+
return true;
42+
})
43+
);
3744
}
3845
}
3946
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('LoginComponent', () => {
1818

1919
const azureAdB2CServiceStub = {
2020
isLogin() {
21-
return true;
21+
return of(true);
2222
},
2323
signIn() {
2424
return of();
@@ -29,7 +29,7 @@ describe('LoginComponent', () => {
2929

3030
const loginServiceStub = {
3131
isLogin() {
32-
return true;
32+
return of(true);
3333
},
3434
signIn() {
3535
return of();
@@ -86,7 +86,7 @@ describe('LoginComponent', () => {
8686
});
8787

8888
it('should sign up or login with google if is not logged-in into the app on Production', inject([Router], (router: Router) => {
89-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
89+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
9090
spyOn(azureAdB2CService, 'setCookies').and.returnValue();
9191
spyOn(azureAdB2CService, 'signIn').and.returnValue(of(() => {}));
9292
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());
@@ -99,7 +99,7 @@ describe('LoginComponent', () => {
9999
}));
100100

101101
it('should sign up or login with google if is not logged-in into the app Locally', inject([Router], (router: Router) => {
102-
spyOn(loginService, 'isLogin').and.returnValue(false);
102+
spyOn(loginService, 'isLogin').and.returnValue(of(false));
103103
spyOn(loginService, 'setLocalStorage').and.returnValue();
104104
spyOn(loginService, 'getUser').and.returnValue(of(() => {}));
105105
spyOn(loginService, 'setCookies').and.returnValue();
@@ -115,15 +115,15 @@ describe('LoginComponent', () => {
115115
}));
116116

117117
it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => {
118-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
118+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
119119
spyOn(router, 'navigate').and.stub();
120120
component.login();
121121
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
122122
expect(router.navigate).toHaveBeenCalledWith(['']);
123123
}));
124124

125125
it('should not sign-up or login with google if is already logged-in into the app Locally', inject([Router], (router: Router) => {
126-
spyOn(loginService, 'isLogin').and.returnValue(true);
126+
spyOn(loginService, 'isLogin').and.returnValue(of(true));
127127
spyOn(router, 'navigate').and.stub();
128128
component.loginWithGoogle();
129129
expect(loginService.isLogin).toHaveBeenCalled();

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SocialAuthService, SocialUser } from 'angularx-social-login';
77
import { environment } from 'src/environments/environment';
88
import { EnvironmentType } from 'src/environments/enum';
99
import { LoginService } from './services/login.service';
10+
1011
@Component({
1112
selector: 'app-login',
1213
templateUrl: './login.component.html',
@@ -31,29 +32,36 @@ export class LoginComponent implements OnInit {
3132
this.loginService.setLocalStorage('idToken', user.idToken);
3233
this.loginService.getUser(user.idToken).subscribe((response) => {
3334
this.loginService.setCookies();
34-
this.loginService.setLocalStorage('user2', JSON.stringify(response));
35+
const tokenObject = JSON.stringify(response);
36+
const tokenJson = JSON.parse(tokenObject);
37+
this.loginService.setLocalStorage('user', tokenJson.token);
3538
this.router.navigate(['']);
3639
});
3740
}
3841
});
3942
}
4043

41-
login(): void {
42-
if (this.azureAdB2CService.isLogin()) {
43-
this.router.navigate(['']);
44-
} else {
45-
this.azureAdB2CService.signIn().subscribe(() => {
46-
this.featureToggleCookiesService.setCookies();
47-
this.azureAdB2CService.setCookies();
44+
login() {
45+
this.azureAdB2CService.isLogin().subscribe(isLogin => {
46+
if (isLogin) {
4847
this.router.navigate(['']);
49-
});
50-
}
48+
} else {
49+
this.azureAdB2CService.signIn().subscribe(() => {
50+
this.featureToggleCookiesService.setCookies();
51+
this.azureAdB2CService.setCookies();
52+
this.router.navigate(['']);
53+
});
54+
}
55+
});
5156
}
57+
5258
loginWithGoogle() {
53-
if (this.loginService.isLogin()) {
54-
this.router.navigate(['']);
55-
} else {
56-
this.loginService.signIn();
57-
}
59+
this.loginService.isLogin().subscribe(isLogin => {
60+
if (isLogin) {
61+
this.router.navigate(['']);
62+
} else {
63+
this.loginService.signIn();
64+
}
65+
});
5866
}
5967
}

src/app/modules/login/services/azure.ad.b2c.service.spec.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { HttpClientTestingModule } from '@angular/common/http/testing';
12
import { inject, TestBed } from '@angular/core/testing';
23
import { Account, UserAgentApplication } from 'msal';
34
import { AzureAdB2CService } from './azure.ad.b2c.service';
45
import { CookieService } from 'ngx-cookie-service';
6+
import { of } from 'rxjs';
7+
58

69
describe('AzureAdB2CService', () => {
710
let service: AzureAdB2CService;
@@ -10,7 +13,7 @@ describe('AzureAdB2CService', () => {
1013

1114
beforeEach(() => {
1215
TestBed.configureTestingModule({
13-
imports: [],
16+
imports: [HttpClientTestingModule],
1417
});
1518
service = TestBed.inject(AzureAdB2CService);
1619
cookieService = TestBed.inject(CookieService);
@@ -46,12 +49,12 @@ describe('AzureAdB2CService', () => {
4649
it('on logout should call msal logout and verify if user localStorage is removed', () => {
4750
spyOn(UserAgentApplication.prototype, 'logout').and.returnValue();
4851
spyOn(cookieService, 'deleteAll');
49-
spyOn(localStorage, 'removeItem').withArgs('user');
52+
spyOn(localStorage, 'clear');
5053

5154
service.logout();
5255

5356
expect(cookieService.deleteAll).toHaveBeenCalled();
54-
expect(localStorage.removeItem).toHaveBeenCalledWith('user');
57+
expect(localStorage.clear).toHaveBeenCalled();
5558
expect(UserAgentApplication.prototype.logout).toHaveBeenCalled();
5659
});
5760

@@ -84,56 +87,54 @@ describe('AzureAdB2CService', () => {
8487
});
8588

8689
it('isLogin returns true if UserAgentApplication has a defined Account and token cookie exist', () => {
87-
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
8890
spyOn(cookieService, 'check').and.returnValue(true);
91+
spyOn(service, 'isValidToken').and.returnValue(of(true));
8992

90-
const isLogin = service.isLogin();
91-
92-
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
93-
expect(cookieService.check).toHaveBeenCalled();
94-
expect(isLogin).toEqual(true);
93+
service.isLogin().subscribe(isLogin => {
94+
expect(isLogin).toEqual(true);
95+
});
9596
});
9697

9798
it('isLogin returns false if UserAgentApplication has a defined Account and token cookie does not exist', () => {
98-
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
99-
spyOn(cookieService, 'check').and.returnValue(false);
99+
spyOn(service, 'isValidToken').and.returnValue(of(false));
100100

101-
const isLogin = service.isLogin();
102101

103-
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
104-
expect(cookieService.check).toHaveBeenCalled();
105-
expect(isLogin).toEqual(false);
102+
service.isLogin().subscribe(isLogin => {
103+
expect(isLogin).toEqual(false);
104+
});
106105
});
107106

108107
it('isLogin returns false if UserAgentApplication has a null value for Account', () => {
109108
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
109+
spyOn(service, 'isValidToken').and.returnValue(of(false));
110110

111-
const isLogin = service.isLogin();
112-
113-
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
114-
expect(isLogin).toEqual(false);
111+
service.isLogin().subscribe(isLogin => {
112+
expect(isLogin).toEqual(false);
113+
});
115114
});
116115

117116
it('setTenantId should save a tenantId in local storage', () => {
118117
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
119-
spyOn(cookieService, 'check').and.returnValue(true);
118+
spyOn(service, 'isValidToken').and.returnValue(of(true));
120119
spyOn(localStorage, 'setItem').withArgs('tenant_id', '12345');
121120

122-
const isLogin = service.isLogin();
121+
service.isLogin().subscribe(isLogin => {
122+
expect(isLogin).toEqual(true);
123+
});
124+
123125
service.setTenantId();
124126

125127
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
126-
expect(cookieService.check).toHaveBeenCalled();
127-
expect(isLogin).toEqual(true);
128128
expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
129129
});
130130

131131
it('setTenantId should not save tenantId if login is false ', () => {
132-
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
132+
spyOn(service, 'isValidToken').and.returnValue(of(false));
133133
spyOn(localStorage, 'setItem');
134-
const isLogin = service.isLogin();
135-
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
136-
expect(isLogin).toEqual(false);
134+
135+
service.isLogin().subscribe(isLogin => {
136+
expect(isLogin).toEqual(false);
137+
});
137138
expect(localStorage.setItem).not.toHaveBeenCalled();
138139
});
139140

0 commit comments

Comments
 (0)