Skip to content

Commit 9df64a8

Browse files
committed
Fix path creation
1 parent a99b748 commit 9df64a8

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Configuration options can be passed as `Angular2TokenOptions` via `.init()`.
112112
```javascript
113113
constructor(private _tokenService: Angular2TokenService) {
114114
this._tokenService.init({
115+
apiBase: null,
115116
apiPath: null,
116117
117118
signInPath: 'auth/sign_in',

src/angular2-token.service.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class Angular2TokenService implements CanActivate {
102102

103103
let defaultOptions: Angular2TokenOptions = {
104104
apiPath: null,
105-
apiBase: '',
105+
apiBase: null,
106106

107107
signInPath: 'auth/sign_in',
108108
signInRedirect: null,
@@ -528,16 +528,21 @@ export class Angular2TokenService implements CanActivate {
528528

529529
private _constructUserPath(): string {
530530
if (this._currentUserType == null)
531-
return '/';
531+
return '';
532532
else
533533
return this._currentUserType.path + '/';
534534
}
535535

536536
private _constructApiPath(): string {
537-
if (this._options.apiPath == null)
538-
return this._options.apiBase + '/';
539-
else
540-
return this._options.apiBase + '/' + this._options.apiPath + '/';
537+
let constructedPath = '';
538+
539+
if (this._options.apiBase != null)
540+
constructedPath += this._options.apiBase + '/';
541+
542+
if (this._options.apiPath != null)
543+
constructedPath += this._options.apiPath + '/';
544+
545+
return constructedPath;
541546
}
542547

543548
private _getOAuthPath(oAuthType: string): string {

src/angular2-token.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ActivatedRoute, RouterOutletMap, RouterState, Router } from '@angular/r
55
import { RouterTestingModule } from '@angular/router/testing';
66

77
import { Angular2TokenService } from './angular2-token.service';
8-
import {
8+
import {
99
SignInData,
1010
RegisterData
1111
} from './angular2-token.model';
@@ -22,6 +22,7 @@ describe('Angular2TokenService', () => {
2222
let emptyHeaders = new Headers({
2323
'content-Type': 'application/json'
2424
});
25+
2526
let tokenHeaders = new Headers({
2627
'content-Type': 'application/json',
2728
'token-type': tokenType,
@@ -181,25 +182,25 @@ describe('Angular2TokenService', () => {
181182
}));
182183

183184
it('validateToken should call signOut when it returns status 401', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
184-
185+
185186
mockBackend.connections.subscribe(
186187
c => c.mockError(new Response(new ResponseOptions({ status: 401, headers: new Headers() })))
187188
);
188189

189190
spyOn(tokenService, 'signOut');
190-
191+
191192
tokenService.init({ apiPath: 'myapi', signOutFailedValidate: true });
192193
tokenService.validateToken().subscribe(res => null, err => expect(tokenService.signOut).toHaveBeenCalled());
193194
}));
194195

195196
it('validateToken should not call signOut when it returns status 401', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
196-
197+
197198
mockBackend.connections.subscribe(
198199
c => c.mockError(new Response(new ResponseOptions({ status: 401, headers: new Headers() })))
199200
);
200201

201202
spyOn(tokenService, 'signOut');
202-
203+
203204
tokenService.init({ apiPath: 'myapi', signOutFailedValidate: false });
204205
tokenService.validateToken().subscribe(res => null, err => expect(tokenService.signOut).not.toHaveBeenCalled());
205206
}));

0 commit comments

Comments
 (0)