Skip to content

Commit 04f38dd

Browse files
author
NEXUS\Anthony.Giretti
committed
keep user information alive if the page is reloaded (if token is still valid)
1 parent 160ac3c commit 04f38dd

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

front-end/src/app/auth-callback/auth-callback.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class AuthCallbackComponent implements OnInit {
1818

1919
setTimeout(() => {this._zone.run(
2020
() => this._router.navigate(['/protected'])
21-
);},200);
21+
);}, 200);
2222

2323
}
2424
}

front-end/src/app/services/auth.service.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ export class AuthService {
1717

1818
constructor(private _adal: Adal5Service) {
1919
this._adal.init(this._config);
20+
21+
if (this.isLoggedIn()) {
22+
console.log(`Rechargement de la page, c'est le meme token et il expire dans ${this._expireIn()} secondes`);
23+
}
2024
}
2125

2226
public isLoggedIn(): boolean {
23-
return this._adal.userInfo.authenticated;
27+
return this._adal.userInfo && this._adal.userInfo.authenticated;
2428
}
2529

2630
public signOut(): void {
@@ -32,7 +36,7 @@ export class AuthService {
3236
}
3337

3438
public getName(): string {
35-
return this._user.profile.name;
39+
return this._adal.userInfo.profile.name;
3640
}
3741

3842
public getToken(): string {
@@ -41,10 +45,10 @@ export class AuthService {
4145

4246
public completeAuthentication(): void {
4347
this._adal.handleWindowCallback();
44-
this._adal.getUser().subscribe(user => {
45-
this._user = user;
46-
console.log(this._adal.userInfo);
47-
var expireIn = user.profile.exp - new Date().getTime();
48-
});
48+
console.log(`Authentification OK et le token expire dans ${this._expireIn()} secondes`);
49+
}
50+
51+
private _expireIn(): number {
52+
return Math.round(this._adal.userInfo.profile.exp - new Date().getTime() / 1000);
4953
}
5054
}

0 commit comments

Comments
 (0)