11import { Injectable } from '@angular/core' ;
22import { UserAgentApplication } from 'msal' ;
33import { from , Observable } from 'rxjs' ;
4+ import { CookieService } from 'ngx-cookie-service' ;
45
56import { AUTHORITY , CLIENT_ID , SCOPES } from '../../../../environments/environment' ;
67
78@Injectable ( {
89 providedIn : 'root' ,
910} )
1011export 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