Skip to content

Commit 24d945e

Browse files
author
Rodrigo Lins
committed
fix TTA-83: Adding legacy login
1 parent 1ccac53 commit 24d945e

File tree

7 files changed

+78
-76
lines changed

7 files changed

+78
-76
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LoginService } from '../../modules/login/services/login.service';
55
import { environment } from 'src/environments/environment';
66
import { map } from 'rxjs/operators';
77
import { EnvironmentType } from 'src/environments/enum';
8+
import { of } from 'rxjs';
89

910

1011
@Injectable({
@@ -20,16 +21,13 @@ export class LoginGuard implements CanActivate {
2021

2122
canActivate() {
2223
if (this.isProduction) {
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-
);
24+
if (this.azureAdB2CService.isLogin()) {
25+
this.azureAdB2CService.setCookies();
26+
return of(true);
27+
} else {
28+
this.router.navigate(['login']);
29+
return of(false);
30+
}
3331
} else {
3432
return this.loginService.isLogin().pipe(
3533
map(isLogin => {

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,16 @@ export class LoginComponent implements OnInit {
4141
});
4242
}
4343

44-
login() {
45-
this.azureAdB2CService.isLogin().subscribe(isLogin => {
46-
if (isLogin) {
44+
login(): void {
45+
if (this.azureAdB2CService.isLogin()) {
46+
this.router.navigate(['']);
47+
} else {
48+
this.azureAdB2CService.signIn().subscribe(() => {
49+
this.featureToggleCookiesService.setCookies();
50+
this.azureAdB2CService.setCookies();
4751
this.router.navigate(['']);
48-
} else {
49-
this.azureAdB2CService.signIn().subscribe(() => {
50-
this.featureToggleCookiesService.setCookies();
51-
this.azureAdB2CService.setCookies();
52-
this.router.navigate(['']);
53-
});
54-
}
55-
});
52+
});
53+
}
5654
}
5755

5856
loginWithGoogle() {

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@ import { UserAgentApplication } from 'msal';
33
import { from, Observable } from 'rxjs';
44
import { CookieService } from 'ngx-cookie-service';
55
import { AUTHORITY, CLIENT_ID, SCOPES } from '../../../../environments/environment';
6-
import { HttpClient } from '@angular/common/http';
7-
import { environment } from 'src/environments/environment.prod';
8-
import { map } from 'rxjs/operators';
96

107
@Injectable({
118
providedIn: 'root',
129
})
1310
export class AzureAdB2CService {
14-
baseUrl: string;
15-
16-
constructor(
17-
private cookieService?: CookieService,
18-
private http?: HttpClient
19-
) {
20-
this.baseUrl = `${environment.timeTrackerApiUrl}/users`;
21-
}
11+
constructor(private cookieService?: CookieService) { }
2212

2313
msalConfig: any = {
2414
auth: {
@@ -44,7 +34,7 @@ export class AzureAdB2CService {
4434
logout() {
4535
this.cookieService.deleteAll();
4636
this.msal.logout();
47-
localStorage.clear();
37+
localStorage.removeItem('user');
4838
}
4939

5040
getName(): string {
@@ -57,8 +47,7 @@ export class AzureAdB2CService {
5747
}
5848

5949
isLogin() {
60-
const token = localStorage.getItem('user');
61-
return this.isValidToken(token);
50+
return this.msal.getAccount() && this.cookieService.check('msal.idtoken') ? true : false;
6251
}
6352

6453
setCookies() {
@@ -97,19 +86,4 @@ export class AzureAdB2CService {
9786
getUserId(): string {
9887
return this.msal.getAccount().accountIdentifier;
9988
}
100-
101-
isValidToken(token: string) {
102-
const body = { token };
103-
return this.http.post(`${this.baseUrl}/validate-token`, body).pipe(
104-
map((response) => {
105-
const responseString = JSON.stringify(response);
106-
const responseJson = JSON.parse(responseString);
107-
if (responseJson.new_token) {
108-
localStorage.setItem('user', responseJson.new_token);
109-
}
110-
const isValid = responseString !== '{}' && this.msal.getAccount() && this.cookieService.check('msal.idtoken');
111-
return isValid ? true : false;
112-
})
113-
);
114-
}
11589
}

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33
import { GoogleLoginProvider, SocialAuthService } from 'angularx-social-login';
44
import { CookieService } from 'ngx-cookie-service';
5-
import { UserEnum } from 'src/environments/enum';
5+
import { EnvironmentType, UserEnum } from 'src/environments/enum';
66
import { environment } from 'src/environments/environment';
77
import { JwtHelperService } from '@auth0/angular-jwt';
88
import { map } from 'rxjs/operators';
9+
import { of } from 'rxjs';
910

1011
@Injectable({
1112
providedIn: 'root'
1213
})
1314
export class LoginService {
1415
baseUrl: string;
1516
helper: JwtHelperService;
17+
isLegacyProd: boolean = environment.production === EnvironmentType.TT_PROD_LEGACY;
18+
localStorageKey = this.isLegacyProd ? 'user2' : 'user';
1619

1720
constructor(
1821
private http?: HttpClient,
@@ -34,31 +37,56 @@ export class LoginService {
3437
}
3538

3639
isLogin() {
37-
const token = this.getLocalStorage('user');
38-
return this.isValidToken(token);
40+
const token = this.getLocalStorage(this.localStorageKey);
41+
if (this.isLegacyProd) {
42+
const user = JSON.parse(token);
43+
return user && this.cookieService.check('idtoken') ? of(true) : of(false);
44+
} else {
45+
return this.isValidToken(token);
46+
}
3947
}
4048

4149
getUserId(): string {
42-
const token = this.getLocalStorage('user');
43-
const user = this.helper.decodeToken(token);
50+
const token = this.getLocalStorage(this.localStorageKey);
51+
let user;
52+
if (this.isLegacyProd) {
53+
user = JSON.parse(token);
54+
} else {
55+
user = this.helper.decodeToken(token);
56+
}
4457
return user[UserEnum.ID];
4558
}
4659

4760
getName(): string {
48-
const token = this.getLocalStorage('user');
49-
const user = this.helper.decodeToken(token);
61+
const token = this.getLocalStorage(this.localStorageKey);
62+
let user;
63+
if (this.isLegacyProd) {
64+
user = JSON.parse(token);
65+
} else {
66+
user = this.helper.decodeToken(token);
67+
}
5068
return user[UserEnum.NAME];
5169
}
5270

5371
getUserEmail(): string {
54-
const token = this.getLocalStorage('user');
55-
const user = this.helper.decodeToken(token);
72+
const token = this.getLocalStorage(this.localStorageKey);
73+
let user;
74+
if (this.isLegacyProd) {
75+
user = JSON.parse(token);
76+
} else {
77+
user = this.helper.decodeToken(token);
78+
}
5679
return user[UserEnum.EMAIL];
5780
}
5881

5982
getUserGroup(): string {
60-
const token = this.getLocalStorage('user');
61-
const user = this.helper.decodeToken(token);
83+
const token = this.getLocalStorage(this.localStorageKey);
84+
let user;
85+
if (this.isLegacyProd) {
86+
user = JSON.parse(token);
87+
} else {
88+
user = this.helper.decodeToken(token);
89+
}
6290
return user[UserEnum.GROUPS];
6391
}
6492

src/app/modules/shared/components/user/user.component.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ export class UserComponent implements OnInit {
1818

1919
ngOnInit(): void {
2020
if (this.isProduction){
21-
this.azureAdB2CService.isLogin().subscribe(isLogin => {
22-
if (isLogin) {
23-
this.userName = this.azureAdB2CService.getName();
24-
this.userEmail = this.azureAdB2CService.getUserEmail();
25-
this.azureAdB2CService.setTenantId();
26-
}
27-
});
21+
if (this.azureAdB2CService.isLogin()) {
22+
this.userName = this.azureAdB2CService.getName();
23+
this.userEmail = this.azureAdB2CService.getUserEmail();
24+
this.azureAdB2CService.setTenantId();
25+
}
2826
} else {
2927
this.loginService.isLogin().subscribe(isLogin => {
3028
if (isLogin) {

src/app/modules/time-clock/pages/time-clock.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ export class TimeClockComponent implements OnInit, OnDestroy {
3939

4040
ngOnInit(): void {
4141
if (this.isProduction) {
42-
this.azureAdB2CService.isLogin().subscribe(isLogin => {
43-
this.username = isLogin ? this.azureAdB2CService.getName() : '';
44-
})
42+
this.username = this.azureAdB2CService.isLogin() ? this.azureAdB2CService.getName() : '';
4543
}else{
4644
this.loginService.isLogin().subscribe(isLogin => {
4745
this.username = isLogin ? this.loginService.getName() : '';

src/app/modules/user/services/user-info.service.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Injectable } from '@angular/core';
22
import { JwtHelperService } from '@auth0/angular-jwt';
33
import { Observable, of } from 'rxjs';
4-
import { GROUPS } from '../../../../environments/environment';
4+
import { EnvironmentType } from 'src/environments/enum';
5+
import { environment, GROUPS } from '../../../../environments/environment';
56

67
import { LoginService } from '../../login/services/login.service';
78

@@ -11,17 +12,24 @@ import { LoginService } from '../../login/services/login.service';
1112
})
1213
export class UserInfoService {
1314
helper: JwtHelperService;
15+
isLegacyProduction: boolean = environment.production === EnvironmentType.TT_PROD_LEGACY;
1416

1517
constructor(private loginService: LoginService) {
1618
this.helper = new JwtHelperService();
1719
}
1820

1921
isMemberOf(groupName: string): Observable<boolean> {
2022
const token = this.loginService.getLocalStorage('user');
21-
const user = this.helper.decodeToken(token);
22-
const {groups = []} = user;
23-
if (groups.includes(groupName)) {
24-
return this.loginService.isValidToken(token);
23+
if (this.isLegacyProduction) {
24+
const user = JSON.parse(token);
25+
const {groups = []} = user;
26+
return of(groups.includes(groupName));
27+
} else {
28+
const user = this.helper.decodeToken(token);
29+
const {groups = []} = user;
30+
if (groups.includes(groupName)) {
31+
return this.loginService.isValidToken(token);
32+
}
2533
}
2634
return of(false);
2735
}

0 commit comments

Comments
 (0)