Skip to content

Commit b29418f

Browse files
authored
fix(auth): fix isAuthenticatedOrRefresh to not refresh token with no value (#708)
1 parent 0e05034 commit b29418f

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/framework/auth/services/auth.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class NbAuthService {
5151
return this.getToken()
5252
.pipe(
5353
switchMap(token => {
54-
if (!token.isValid()) {
54+
if (token.getValue() && !token.isValid()) {
5555
return this.refreshToken(token.getOwnerStrategyName(), token)
5656
.pipe(
5757
switchMap(res => {

src/framework/auth/services/auth.spec.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ describe('auth-service', () => {
3131
const resp200 = new HttpResponse<Object>({body: {}, status: 200});
3232

3333
const testToken = nbAuthCreateToken(NbAuthSimpleToken, testTokenValue, ownerStrategyName);
34-
const emptyToken = nbAuthCreateToken(NbAuthSimpleToken, null, ownerStrategyName);
34+
const invalidToken = nbAuthCreateToken(NbAuthSimpleToken, testTokenValue, ownerStrategyName);
35+
const emptyToken = nbAuthCreateToken(NbAuthSimpleToken, null, null);
3536

3637
const failResult = new NbAuthResult(false,
3738
resp401,
@@ -162,13 +163,17 @@ describe('auth-service', () => {
162163

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

166+
spyOn(invalidToken, 'isValid')
167+
.and
168+
.returnValue(false);
169+
165170
const spy = spyOn(dummyAuthStrategy, 'refreshToken')
166171
.and
167172
.returnValue(observableOf(successResult));
168173

169174
spyOn(tokenService, 'get')
170175
.and
171-
.returnValues(observableOf(emptyToken), observableOf(testToken));
176+
.returnValues(observableOf(invalidToken), observableOf(testToken));
172177

173178
authService.isAuthenticatedOrRefresh()
174179
.pipe(first())
@@ -181,13 +186,33 @@ describe('auth-service', () => {
181186
);
182187

183188
it('isAuthenticatedOrRefresh, token invalid, strategy refreshToken called, returns false', (done) => {
189+
190+
spyOn(invalidToken, 'isValid')
191+
.and
192+
.returnValue(false);
193+
184194
const spy = spyOn(dummyAuthStrategy, 'refreshToken')
185195
.and
186196
.returnValue(observableOf(failResult));
187197

188198
spyOn(tokenService, 'get')
189199
.and
190-
.returnValues(observableOf(emptyToken), observableOf(emptyToken));
200+
.returnValues(observableOf(invalidToken), observableOf(invalidToken));
201+
202+
authService.isAuthenticatedOrRefresh()
203+
.pipe(first())
204+
.subscribe((isAuth: boolean) => {
205+
expect(spy).toHaveBeenCalled();
206+
expect(isAuth).toBeFalsy();
207+
done();
208+
});
209+
},
210+
);
211+
212+
it('isAuthenticatedOrRefresh, token doesn\'t exist, strategy refreshToken called, returns false', (done) => {
213+
const spy = spyOn(tokenService, 'get')
214+
.and
215+
.returnValue(observableOf(emptyToken));
191216

192217
authService.isAuthenticatedOrRefresh()
193218
.pipe(first())

0 commit comments

Comments
 (0)