Skip to content

Commit 24fd0c1

Browse files
Do not crash when getTokenSilently returns null (#458)
1 parent e711e3c commit 24fd0c1

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

projects/auth0-angular/src/lib/auth.service.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,20 @@ describe('AuthService', () => {
776776
});
777777
});
778778

779+
it('should null when nothing in cache', (done) => {
780+
((auth0Client.getTokenSilently as unknown) as jest.SpyInstance).mockResolvedValue(
781+
null
782+
);
783+
784+
const service = createService();
785+
service
786+
.getAccessTokenSilently()
787+
.subscribe((token) => {
788+
expect(token).toBeNull();
789+
done();
790+
});
791+
});
792+
779793
it('should record errors in the error$ observable', (done) => {
780794
const errorObj = new Error('An error has occured');
781795

projects/auth0-angular/src/lib/auth.service.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import { LogoutOptions, RedirectLoginOptions } from './interfaces';
4141
providedIn: 'root',
4242
})
4343
export class AuthService<TAppState extends AppState = AppState>
44-
implements OnDestroy {
44+
implements OnDestroy
45+
{
4546
private appStateSubject$ = new ReplaySubject<TAppState>(1);
4647

4748
// https://stackoverflow.com/a/41177163
@@ -242,11 +243,13 @@ export class AuthService<TAppState extends AppState = AppState>
242243
? client.getTokenSilently({ ...options, detailedResponse: true })
243244
: client.getTokenSilently(options)
244245
),
245-
tap((token) =>
246-
this.authState.setAccessToken(
247-
typeof token === 'string' ? token : token.access_token
248-
)
249-
),
246+
tap((token) => {
247+
if (token) {
248+
this.authState.setAccessToken(
249+
typeof token === 'string' ? token : token.access_token
250+
);
251+
}
252+
}),
250253
catchError((error) => {
251254
this.authState.setError(error);
252255
this.authState.refresh();

0 commit comments

Comments
 (0)