Skip to content

Commit

Permalink
Do not crash when getTokenSilently returns null (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck committed Jul 24, 2023
1 parent e711e3c commit 24fd0c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 14 additions & 0 deletions projects/auth0-angular/src/lib/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,20 @@ describe('AuthService', () => {
});
});

it('should null when nothing in cache', (done) => {
((auth0Client.getTokenSilently as unknown) as jest.SpyInstance).mockResolvedValue(
null
);

const service = createService();
service
.getAccessTokenSilently()
.subscribe((token) => {
expect(token).toBeNull();
done();
});
});

it('should record errors in the error$ observable', (done) => {
const errorObj = new Error('An error has occured');

Expand Down
15 changes: 9 additions & 6 deletions projects/auth0-angular/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import { LogoutOptions, RedirectLoginOptions } from './interfaces';
providedIn: 'root',
})
export class AuthService<TAppState extends AppState = AppState>
implements OnDestroy {
implements OnDestroy
{
private appStateSubject$ = new ReplaySubject<TAppState>(1);

// https://stackoverflow.com/a/41177163
Expand Down Expand Up @@ -242,11 +243,13 @@ export class AuthService<TAppState extends AppState = AppState>
? client.getTokenSilently({ ...options, detailedResponse: true })
: client.getTokenSilently(options)
),
tap((token) =>
this.authState.setAccessToken(
typeof token === 'string' ? token : token.access_token
)
),
tap((token) => {
if (token) {
this.authState.setAccessToken(
typeof token === 'string' ? token : token.access_token
);
}
}),
catchError((error) => {
this.authState.setError(error);
this.authState.refresh();
Expand Down

0 comments on commit 24fd0c1

Please sign in to comment.