Skip to content

Commit be08f79

Browse files
author
Guido Quezada
committed
TT-15 fix: Keep the user logged in the browser
1 parent 69c9a8f commit be08f79

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"minimist": "^1.2.5",
3939
"moment": "^2.25.3",
4040
"msal": "^1.2.1",
41+
"ngx-cookie-service": "^11.0.2",
4142
"ngx-mask": "^9.1.2",
4243
"ngx-pagination": "^5.0.0",
4344
"ngx-toastr": "^12.0.1",

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { AutocompleteLibModule } from 'angular-ng-autocomplete';
1616

1717
import { AppRoutingModule } from './app-routing.module';
1818
import { AppComponent } from './app.component';
19+
import { CookieService } from 'ngx-cookie-service';
1920
import { NavbarComponent } from './modules/shared/components/navbar/navbar.component';
2021
import { UserComponent } from './modules/shared/components/user/user.component';
2122
import { SidebarComponent } from './modules/shared/components/sidebar/sidebar.component';
@@ -159,7 +160,8 @@ const maskConfig: Partial<IConfig> = {
159160
useClass: InjectTokenInterceptor,
160161
multi: true,
161162
},
162-
DatePipe
163+
DatePipe,
164+
CookieService,
163165
],
164166
bootstrap: [AppComponent],
165167
})

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ export class LoginGuard implements CanActivate {
1111

1212
canActivate() {
1313
if (this.azureAdB2CService.isLogin()) {
14+
this.azureAdB2CService.setCookies();
1415
return true;
1516
} else {
1617
this.router.navigate(['login']);
1718
return false;
1819
}
19-
}
20+
}
2021
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class LoginComponent {
1717
this.router.navigate(['']);
1818
} else {
1919
this.azureAdB2CService.signIn().subscribe(() => {
20+
this.azureAdB2CService.setCookies();
2021
this.router.navigate(['']);
2122
});
2223
}

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import { Injectable } from '@angular/core';
22
import { UserAgentApplication } from 'msal';
33
import { from, Observable } from 'rxjs';
4+
import { CookieService } from 'ngx-cookie-service';
45

56
import { AUTHORITY, CLIENT_ID, SCOPES } from '../../../../environments/environment';
67

78
@Injectable({
89
providedIn: 'root',
910
})
1011
export class AzureAdB2CService {
11-
msalConfig = {
12+
13+
constructor(private cookieService: CookieService) {}
14+
15+
msalConfig: any = {
1216
auth: {
1317
clientId: CLIENT_ID,
1418
authority: AUTHORITY,
1519
validateAuthority: false,
1620
},
21+
cache: {
22+
cacheLocation: 'localStorage',
23+
},
1724
};
1825

1926
tokenRequest = {
@@ -27,6 +34,8 @@ export class AzureAdB2CService {
2734
}
2835

2936
logout() {
37+
this.cookieService.delete('msal.idtoken');
38+
this.cookieService.delete('msal.idtoken');
3039
this.msal.logout();
3140
}
3241

@@ -39,22 +48,32 @@ export class AzureAdB2CService {
3948
}
4049

4150
isLogin() {
42-
return this.msal.getAccount() ? true : false;
51+
return this.msal.getAccount() && this.cookieService.check('msal.idtoken') ? true : false;
52+
}
53+
54+
setCookies() {
55+
this.cookieService.set('msal.client.info', this.getBearerClientInfo(), 2);
56+
this.cookieService.set('msal.idtoken', this.getBearerToken(), 2);
4357
}
4458

4559
setTenantId() {
4660
if (this.msal.getAccount() && this.msal.getAccount().idToken) {
4761
const pathArray = this.msal.getAccount().idToken.iss.split('/');
4862
const tenantId = pathArray[3];
49-
sessionStorage.setItem('tenant_id', tenantId);
63+
localStorage.setItem('tenant_id', tenantId);
5064
}
5165
}
5266

5367
getTenantId(): string {
54-
return sessionStorage.getItem('tenant_id');
68+
return localStorage.getItem('tenant_id');
69+
}
70+
71+
getBearerClientInfo(): string {
72+
return localStorage.getItem('msal.client.info');
5573
}
74+
5675
getBearerToken(): string {
57-
return sessionStorage.getItem('msal.idtoken');
76+
return localStorage.getItem('msal.idtoken');
5877
}
5978

6079
getUserEmail(): string {

0 commit comments

Comments
 (0)