Skip to content

Commit 59f65d2

Browse files
committed
fix: multiplying calls to token endpoint in code flow
1 parent 8d152c2 commit 59f65d2

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/lib/src/oauth-service.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
102102
protected accessTokenTimeoutSubscription: Subscription;
103103
protected idTokenTimeoutSubscription: Subscription;
104104
protected tokenReceivedSubscription: Subscription;
105+
protected automaticRefreshSubscription: Subscription;
105106
protected sessionCheckEventListener: EventListener;
106107
protected jwksUri: string;
107108
protected sessionCheckTimer: any;
@@ -222,7 +223,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
222223
noPrompt = true
223224
): void {
224225
let shouldRunSilentRefresh = true;
225-
this.events
226+
this.clearAutomaticRefreshTimer();
227+
this.automaticRefreshSubscription = this.events
226228
.pipe(
227229
tap(e => {
228230
if (e.type === 'token_received') {
@@ -448,6 +450,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
448450
public stopAutomaticRefresh() {
449451
this.clearAccessTokenTimer();
450452
this.clearIdTokenTimer();
453+
this.clearAutomaticRefreshTimer();
451454
}
452455

453456
protected clearAccessTokenTimer(): void {
@@ -462,6 +465,12 @@ export class OAuthService extends AuthConfig implements OnDestroy {
462465
}
463466
}
464467

468+
protected clearAutomaticRefreshTimer(): void {
469+
if (this.automaticRefreshSubscription) {
470+
this.automaticRefreshSubscription.unsubscribe();
471+
}
472+
}
473+
465474
protected calcTimeout(storedAt: number, expiration: number): number {
466475
const now = Date.now();
467476
const delta =

projects/sample/src/app/home/home.component.html

+8
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,13 @@ <h2>Further Actions</h2>
109109
<button class="btn btn-default" (click)="loadUserProfile()">
110110
Load User Profile
111111
</button>
112+
113+
<button class="btn btn-default" (click)="startAutomaticRefresh()">
114+
Start automatic refresh
115+
</button>
116+
117+
<button class="btn btn-default" (click)="stopAutomaticRefresh()">
118+
Stop automatic refresh
119+
</button>
112120
</div>
113121
</div>

projects/sample/src/app/home/home.component.ts

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ export class HomeComponent implements OnInit {
8686
this.oauthService.loadUserProfile().then(up => (this.userProfile = up));
8787
}
8888

89+
startAutomaticRefresh(): void {
90+
this.oauthService.setupAutomaticSilentRefresh();
91+
}
92+
93+
stopAutomaticRefresh(): void {
94+
this.oauthService.stopAutomaticRefresh();
95+
}
96+
8997
get givenName() {
9098
var claims = this.oauthService.getIdentityClaims();
9199
if (!claims) return null;

0 commit comments

Comments
 (0)