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 (#708)
  • Loading branch information
nnixaa authored Sep 12, 2018
1 parent 0e05034 commit b29418f
Show file tree
Hide file tree
Showing 2 changed files with 29 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.getValue() && !token.isValid()) {
return this.refreshToken(token.getOwnerStrategyName(), token)
.pipe(
switchMap(res => {
Expand Down
31 changes: 28 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, testTokenValue, ownerStrategyName);
const emptyToken = nbAuthCreateToken(NbAuthSimpleToken, null, null);

const failResult = new NbAuthResult(false,
resp401,
Expand Down Expand Up @@ -162,13 +163,17 @@ describe('auth-service', () => {

it('isAuthenticatedOrRefresh, token invalid, strategy refreshToken called, returns true', (done) => {

spyOn(invalidToken, 'isValid')
.and
.returnValue(false);

const spy = spyOn(dummyAuthStrategy, 'refreshToken')
.and
.returnValue(observableOf(successResult));

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

authService.isAuthenticatedOrRefresh()
.pipe(first())
Expand All @@ -181,13 +186,33 @@ describe('auth-service', () => {
);

it('isAuthenticatedOrRefresh, token invalid, strategy refreshToken called, returns false', (done) => {

spyOn(invalidToken, 'isValid')
.and
.returnValue(false);

const spy = spyOn(dummyAuthStrategy, 'refreshToken')
.and
.returnValue(observableOf(failResult));

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 b29418f

Please sign in to comment.