Skip to content
Prev Previous commit
Next Next commit
fix TTA-83: Adding legacy login
  • Loading branch information
Rodrigo Lins committed Aug 5, 2022
commit 24d945eb23e64bc9684f465c52a78e25dd161494
18 changes: 8 additions & 10 deletions src/app/guards/login-guard/login.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LoginService } from '../../modules/login/services/login.service';
import { environment } from 'src/environments/environment';
import { map } from 'rxjs/operators';
import { EnvironmentType } from 'src/environments/enum';
import { of } from 'rxjs';


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

canActivate() {
if (this.isProduction) {
return this.azureAdB2CService.isLogin().pipe(
map(isLogin => {
if (!isLogin) {
this.router.navigate(['login']);
return false;
}
this.azureAdB2CService.setCookies();
return true;
})
);
if (this.azureAdB2CService.isLogin()) {
this.azureAdB2CService.setCookies();
return of(true);
} else {
this.router.navigate(['login']);
return of(false);
}
} else {
return this.loginService.isLogin().pipe(
map(isLogin => {
Expand Down
20 changes: 9 additions & 11 deletions src/app/modules/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ export class LoginComponent implements OnInit {
});
}

login() {
this.azureAdB2CService.isLogin().subscribe(isLogin => {
if (isLogin) {
login(): void {
if (this.azureAdB2CService.isLogin()) {
this.router.navigate(['']);
} else {
this.azureAdB2CService.signIn().subscribe(() => {
this.featureToggleCookiesService.setCookies();
this.azureAdB2CService.setCookies();
this.router.navigate(['']);
} else {
this.azureAdB2CService.signIn().subscribe(() => {
this.featureToggleCookiesService.setCookies();
this.azureAdB2CService.setCookies();
this.router.navigate(['']);
});
}
});
});
}
}

loginWithGoogle() {
Expand Down
32 changes: 3 additions & 29 deletions src/app/modules/login/services/azure.ad.b2c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@ import { UserAgentApplication } from 'msal';
import { from, Observable } from 'rxjs';
import { CookieService } from 'ngx-cookie-service';
import { AUTHORITY, CLIENT_ID, SCOPES } from '../../../../environments/environment';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment.prod';
import { map } from 'rxjs/operators';

@Injectable({
providedIn: 'root',
})
export class AzureAdB2CService {
baseUrl: string;

constructor(
private cookieService?: CookieService,
private http?: HttpClient
) {
this.baseUrl = `${environment.timeTrackerApiUrl}/users`;
}
constructor(private cookieService?: CookieService) { }

msalConfig: any = {
auth: {
Expand All @@ -44,7 +34,7 @@ export class AzureAdB2CService {
logout() {
this.cookieService.deleteAll();
this.msal.logout();
localStorage.clear();
localStorage.removeItem('user');
}

getName(): string {
Expand All @@ -57,8 +47,7 @@ export class AzureAdB2CService {
}

isLogin() {
const token = localStorage.getItem('user');
return this.isValidToken(token);
return this.msal.getAccount() && this.cookieService.check('msal.idtoken') ? true : false;
}

setCookies() {
Expand Down Expand Up @@ -97,19 +86,4 @@ export class AzureAdB2CService {
getUserId(): string {
return this.msal.getAccount().accountIdentifier;
}

isValidToken(token: string) {
const body = { token };
return this.http.post(`${this.baseUrl}/validate-token`, body).pipe(
map((response) => {
const responseString = JSON.stringify(response);
const responseJson = JSON.parse(responseString);
if (responseJson.new_token) {
localStorage.setItem('user', responseJson.new_token);
}
const isValid = responseString !== '{}' && this.msal.getAccount() && this.cookieService.check('msal.idtoken');
return isValid ? true : false;
})
);
}
}
50 changes: 39 additions & 11 deletions src/app/modules/login/services/login.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { GoogleLoginProvider, SocialAuthService } from 'angularx-social-login';
import { CookieService } from 'ngx-cookie-service';
import { UserEnum } from 'src/environments/enum';
import { EnvironmentType, UserEnum } from 'src/environments/enum';
import { environment } from 'src/environments/environment';
import { JwtHelperService } from '@auth0/angular-jwt';
import { map } from 'rxjs/operators';
import { of } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class LoginService {
baseUrl: string;
helper: JwtHelperService;
isLegacyProd: boolean = environment.production === EnvironmentType.TT_PROD_LEGACY;
localStorageKey = this.isLegacyProd ? 'user2' : 'user';

constructor(
private http?: HttpClient,
Expand All @@ -34,31 +37,56 @@ export class LoginService {
}

isLogin() {
const token = this.getLocalStorage('user');
return this.isValidToken(token);
const token = this.getLocalStorage(this.localStorageKey);
if (this.isLegacyProd) {
const user = JSON.parse(token);
return user && this.cookieService.check('idtoken') ? of(true) : of(false);
} else {
return this.isValidToken(token);
}
}

getUserId(): string {
const token = this.getLocalStorage('user');
const user = this.helper.decodeToken(token);
const token = this.getLocalStorage(this.localStorageKey);
let user;
if (this.isLegacyProd) {
user = JSON.parse(token);
} else {
user = this.helper.decodeToken(token);
}
return user[UserEnum.ID];
}

getName(): string {
const token = this.getLocalStorage('user');
const user = this.helper.decodeToken(token);
const token = this.getLocalStorage(this.localStorageKey);
let user;
if (this.isLegacyProd) {
user = JSON.parse(token);
} else {
user = this.helper.decodeToken(token);
}
return user[UserEnum.NAME];
}

getUserEmail(): string {
const token = this.getLocalStorage('user');
const user = this.helper.decodeToken(token);
const token = this.getLocalStorage(this.localStorageKey);
let user;
if (this.isLegacyProd) {
user = JSON.parse(token);
} else {
user = this.helper.decodeToken(token);
}
return user[UserEnum.EMAIL];
}

getUserGroup(): string {
const token = this.getLocalStorage('user');
const user = this.helper.decodeToken(token);
const token = this.getLocalStorage(this.localStorageKey);
let user;
if (this.isLegacyProd) {
user = JSON.parse(token);
} else {
user = this.helper.decodeToken(token);
}
return user[UserEnum.GROUPS];
}

Expand Down
12 changes: 5 additions & 7 deletions src/app/modules/shared/components/user/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ export class UserComponent implements OnInit {

ngOnInit(): void {
if (this.isProduction){
this.azureAdB2CService.isLogin().subscribe(isLogin => {
if (isLogin) {
this.userName = this.azureAdB2CService.getName();
this.userEmail = this.azureAdB2CService.getUserEmail();
this.azureAdB2CService.setTenantId();
}
});
if (this.azureAdB2CService.isLogin()) {
this.userName = this.azureAdB2CService.getName();
this.userEmail = this.azureAdB2CService.getUserEmail();
this.azureAdB2CService.setTenantId();
}
} else {
this.loginService.isLogin().subscribe(isLogin => {
if (isLogin) {
Expand Down
4 changes: 1 addition & 3 deletions src/app/modules/time-clock/pages/time-clock.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export class TimeClockComponent implements OnInit, OnDestroy {

ngOnInit(): void {
if (this.isProduction) {
this.azureAdB2CService.isLogin().subscribe(isLogin => {
this.username = isLogin ? this.azureAdB2CService.getName() : '';
})
this.username = this.azureAdB2CService.isLogin() ? this.azureAdB2CService.getName() : '';
}else{
this.loginService.isLogin().subscribe(isLogin => {
this.username = isLogin ? this.loginService.getName() : '';
Expand Down
18 changes: 13 additions & 5 deletions src/app/modules/user/services/user-info.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@angular/core';
import { JwtHelperService } from '@auth0/angular-jwt';
import { Observable, of } from 'rxjs';
import { GROUPS } from '../../../../environments/environment';
import { EnvironmentType } from 'src/environments/enum';
import { environment, GROUPS } from '../../../../environments/environment';

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

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

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

isMemberOf(groupName: string): Observable<boolean> {
const token = this.loginService.getLocalStorage('user');
const user = this.helper.decodeToken(token);
const {groups = []} = user;
if (groups.includes(groupName)) {
return this.loginService.isValidToken(token);
if (this.isLegacyProduction) {
const user = JSON.parse(token);
const {groups = []} = user;
return of(groups.includes(groupName));
} else {
const user = this.helper.decodeToken(token);
const {groups = []} = user;
if (groups.includes(groupName)) {
return this.loginService.isValidToken(token);
}
}
return of(false);
}
Expand Down