Skip to content

Commit

Permalink
fix(auth): fix isAuthenticatedOrRefresh to not refresh token with n…
Browse files Browse the repository at this point in the history
…o value
  • Loading branch information
nnixaa committed Sep 11, 2018
1 parent 2edd9b3 commit b64537b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/framework/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class NbAuthService {
return this.getToken()
.pipe(
switchMap(token => {
if (!token.isValid()) {
if (!token.isValid() && token.getValue()) {
return this.refreshToken(token.getOwnerStrategyName(), token)
.pipe(
switchMap(res => {
Expand Down
22 changes: 19 additions & 3 deletions src/framework/auth/services/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('auth-service', () => {
const resp200 = new HttpResponse<Object>({body: {}, status: 200});

const testToken = nbAuthCreateToken(NbAuthSimpleToken, testTokenValue, ownerStrategyName);
const emptyToken = nbAuthCreateToken(NbAuthSimpleToken, null, ownerStrategyName);
const invalidToken = nbAuthCreateToken(NbAuthSimpleToken, null, ownerStrategyName);
const emptyToken = nbAuthCreateToken(NbAuthSimpleToken, null, null);

const failResult = new NbAuthResult(false,
resp401,
Expand Down Expand Up @@ -168,7 +169,7 @@ describe('auth-service', () => {

spyOn(tokenService, 'get')
.and
.returnValues(observableOf(emptyToken), observableOf(testToken));
.returnValues(observableOf(invalidToken), observableOf(testToken));

authService.isAuthenticatedOrRefresh()
.pipe(first())
Expand All @@ -187,7 +188,22 @@ describe('auth-service', () => {

spyOn(tokenService, 'get')
.and
.returnValues(observableOf(emptyToken), observableOf(emptyToken));
.returnValues(observableOf(invalidToken), observableOf(invalidToken));

authService.isAuthenticatedOrRefresh()
.pipe(first())
.subscribe((isAuth: boolean) => {
expect(spy).toHaveBeenCalled();
expect(isAuth).toBeFalsy();
done();
});
},
);

it('isAuthenticatedOrRefresh, token doesn\'t exist, strategy refreshToken called, returns false', (done) => {
const spy = spyOn(tokenService, 'get')
.and
.returnValue(observableOf(emptyToken));

authService.isAuthenticatedOrRefresh()
.pipe(first())
Expand Down

0 comments on commit b64537b

Please sign in to comment.