Skip to content

Commit

Permalink
Add methods to get/set token property
Browse files Browse the repository at this point in the history
  • Loading branch information
João Granado committed Jun 1, 2015
1 parent a6a0977 commit 03d03d1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/providers/oauth-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function OAuthProvider() {
*/

isAuthenticated() {
return !!OAuthToken.token;
return !!OAuthToken.getToken();
}

/**
Expand Down Expand Up @@ -136,7 +136,7 @@ function OAuthProvider() {
}, options);

return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
OAuthToken.token = response.data;
OAuthToken.setToken(response.data);

return response;
});
Expand Down Expand Up @@ -167,7 +167,7 @@ function OAuthProvider() {
};

return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
OAuthToken.token = response.data;
OAuthToken.setToken(response.data);

return response;
});
Expand Down
10 changes: 5 additions & 5 deletions src/providers/oauth-token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ function OAuthTokenProvider() {
* Set token.
*/

set token(data) {
setToken(data) {
return ipCookie(config.name, data, config.options);
}

/**
* Get token.
*/

get token() {
getToken() {
return ipCookie(config.name);
}

Expand All @@ -65,7 +65,7 @@ function OAuthTokenProvider() {
*/

getAccessToken() {
return this.token ? this.token.access_token : undefined;
return this.getToken() ? this.getToken().access_token : undefined;
}

/**
Expand All @@ -85,15 +85,15 @@ function OAuthTokenProvider() {
*/

getRefreshToken() {
return this.token ? this.token.refresh_token : undefined;
return this.getToken() ? this.getToken().refresh_token : undefined;
}

/**
* Get tokenType.
*/

getTokenType() {
return this.token ? this.token.token_type : undefined;
return this.getToken() ? this.getToken().token_type : undefined;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/interceptors/oauth-interceptor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('oauthInterceptor', function() {
}));

it('should inject `Authorization` header if `token` exists', inject(function($http, $httpBackend, OAuthToken) {
OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

$httpBackend.expectGET('https://website.com', function(headers) {
headers.should.have.property('Authorization');
Expand Down
18 changes: 9 additions & 9 deletions test/unit/providers/oauth-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('OAuthProvider', function() {

describe('isAuthenticated()', function() {
it('should be true when there is a stored `token` cookie', inject(function(OAuth, OAuthToken) {
OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

OAuth.isAuthenticated().should.be.true;
}));
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('OAuthProvider', function() {
username: 'foo',
password: 'bar'
}).then(function(response) {
OAuthToken.token.should.eql(response.data);
OAuthToken.getToken().should.eql(response.data);
}).catch(function() {
should.fail();
});
Expand All @@ -277,7 +277,7 @@ describe('OAuthProvider', function() {
it('should call `queryString.stringify`', inject(function(OAuth, OAuthToken) {
sinon.spy(queryString, 'stringify');

OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

OAuth.getRefreshToken();

Expand Down Expand Up @@ -310,7 +310,7 @@ describe('OAuthProvider', function() {
}));

it('should return an error if `refresh_token` is invalid', inject(function($httpBackend, OAuth, OAuthToken) {
OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

$httpBackend.expectPOST(defaults.baseUrl + defaults.grantPath, queryString.stringify(data))
.respond(400, { error: 'invalid_grant' });
Expand All @@ -329,7 +329,7 @@ describe('OAuthProvider', function() {
}));

it('should retrieve and store `refresh_token` if request is successful', inject(function($httpBackend, OAuth, OAuthToken) {
OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

$httpBackend.expectPOST(defaults.baseUrl + defaults.grantPath, queryString.stringify(data))
.respond({ token_type: 'bearer', access_token: 'qux', expires_in: 3600, refresh_token: 'biz' });
Expand All @@ -356,7 +356,7 @@ describe('OAuthProvider', function() {
it('should call `queryString.stringify`', inject(function(OAuth, OAuthToken) {
sinon.spy(queryString, 'stringify');

OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

OAuth.revokeToken();

Expand All @@ -371,7 +371,7 @@ describe('OAuthProvider', function() {
it('should call `queryString.stringify` with `access_token` if `refresh_token` is not available', inject(function(OAuth, OAuthToken) {
sinon.spy(queryString, 'stringify');

OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600 };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600 });

OAuth.revokeToken();

Expand Down Expand Up @@ -404,7 +404,7 @@ describe('OAuthProvider', function() {
}));

it('should revoke and remove `token` if request is successful', inject(function($httpBackend, OAuth, OAuthToken) {
OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

var data = queryString.stringify({
token: 'bar'
Expand All @@ -414,7 +414,7 @@ describe('OAuthProvider', function() {
.respond(200);

OAuth.revokeToken().then(function() {
(undefined === OAuthToken.token).should.be.true;
(undefined === OAuthToken.getToken()).should.be.true;
}).catch(function() {
should.fail();
});
Expand Down
10 changes: 5 additions & 5 deletions test/unit/providers/oauth-token-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('OAuthTokenProvider', function() {
angular.mock.module('angular-oauth2', 'angular-oauth2.test');

angular.mock.inject(function(OAuthToken) {
OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });
});

});
Expand All @@ -65,9 +65,9 @@ describe('OAuthTokenProvider', function() {
}));

it('setToken()', inject(function(OAuthToken) {
OAuthToken.token = { token_type: 'bearer', access_token: 'qux', expires_in: 3600, refresh_token: 'biz' };
OAuthToken.setToken({ token_type: 'bearer', access_token: 'qux', expires_in: 3600, refresh_token: 'biz' });

OAuthToken.token.should.eql({
OAuthToken.getToken().should.eql({
token_type: 'bearer',
access_token: 'qux',
expires_in: 3600,
Expand All @@ -76,7 +76,7 @@ describe('OAuthTokenProvider', function() {
}));

it('getToken()', inject(function(OAuthToken) {
OAuthToken.token.should.eql({
OAuthToken.getToken().should.eql({
token_type: 'bearer',
access_token: 'foo',
expires_in: 3600,
Expand All @@ -91,7 +91,7 @@ describe('OAuthTokenProvider', function() {
it('removeToken()', inject(function(OAuthToken) {
OAuthToken.removeToken();

(undefined === OAuthToken.token).should.true;
(undefined === OAuthToken.getToken()).should.true;
}));
});
});

0 comments on commit 03d03d1

Please sign in to comment.