Skip to content

Commit 2646f3a

Browse files
author
Nikolay Dolzhenkov
committed
Pause silent refresh if user has logged out
1 parent 0274cf5 commit 2646f3a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

projects/lib/src/oauth-service.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,33 @@ export class OAuthService extends AuthConfig {
158158

159159
/**
160160
* Will setup up silent refreshing for when the token is
161-
* about to expire.
161+
* about to expire. When the user is logged out via this.logOut method, the
162+
* silent refreshing will pause and not refresh the tokens until the user is
163+
* logged back in via receiving a new token.
162164
* @param params Additional parameter to pass
165+
* @param listenTo Setup automatic refresh of a specific token type
163166
*/
164167
public setupAutomaticSilentRefresh(params: object = {}, listenTo?: 'access_token' | 'id_token' | 'any') {
165-
this.events.pipe(filter(e => e.type === 'token_expires')).subscribe(e => {
166-
const event = e as OAuthInfoEvent;
167-
if ( listenTo == null || listenTo === 'any' || event.info === listenTo ) {
168-
this.silentRefresh(params).catch(_ => {
169-
this.debug('Automatic silent refresh did not work');
170-
});
171-
}
172-
});
168+
let shouldRunSilentRefresh = true;
169+
this.events.pipe(
170+
tap((e) => {
171+
if (e.type === 'token_received') {
172+
shouldRunSilentRefresh = true;
173+
} else if (e.type === 'logout') {
174+
shouldRunSilentRefresh = false;
175+
}
176+
}),
177+
filter(e => e.type === 'token_expires')
178+
).subscribe(e => {
179+
const event = e as OAuthInfoEvent;
180+
if ((listenTo == null || listenTo === 'any' || event.info === listenTo) && shouldRunSilentRefresh) {
181+
this.silentRefresh(params).catch(_ => {
182+
this.debug('Automatic silent refresh did not work');
183+
});
184+
}
185+
});
173186

174-
this.restartRefreshTimerIfStillLoggedIn();
187+
this.restartRefreshTimerIfStillLoggedIn();
175188
}
176189

177190

0 commit comments

Comments
 (0)