Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Issue/147 #193

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 96 additions & 8 deletions dist/stormpath-sdk-angularjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* stormpath-sdk-angularjs
* Copyright Stormpath, Inc. 2016
*
* @version v1.1.1-dev-2016-10-28
* @version v1.1.1-dev-2016-10-31
* @link https://github.com/stormpath/stormpath-sdk-angularjs
* @license Apache-2.0
*/
Expand Down Expand Up @@ -2923,7 +2923,7 @@ angular.module('stormpath')
* Currently, this provider does not have any configuration methods.
*/

angular.module('stormpath.userService',['stormpath.CONFIG'])
angular.module('stormpath.userService',['stormpath.CONFIG', 'stormpath.util'])
.provider('$user', [function $userProvider(){

/**
Expand Down Expand Up @@ -2976,8 +2976,8 @@ angular.module('stormpath.userService',['stormpath.CONFIG'])
};

this.$get = [
'$q','$http','STORMPATH_CONFIG','$rootScope','$spFormEncoder','$spErrorTransformer',
function userServiceFactory($q,$http,STORMPATH_CONFIG,$rootScope,$spFormEncoder,$spErrorTransformer){
'$q','$http','STORMPATH_CONFIG','$rootScope','$spFormEncoder','$spErrorTransformer', '$verifyResponse',
function userServiceFactory($q,$http,STORMPATH_CONFIG,$rootScope,$spFormEncoder,$spErrorTransformer, $verifyResponse){
function UserService(){
this.cachedUserOp = null;

Expand Down Expand Up @@ -3145,6 +3145,13 @@ angular.module('stormpath.userService',['stormpath.CONFIG'])
self.cachedUserOp = op;

$http.get(STORMPATH_CONFIG.getUrl('CURRENT_USER_URI'),{withCredentials:true}).then(function(response){
var responseStatus = $verifyResponse(response);

if (!responseStatus.valid) {
self.currentUser = false;
return op.reject(responseStatus.error);
}

self.cachedUserOp = null;
self.currentUser = new User(response.data.account || response.data);
currentUserEvent(self.currentUser);
Expand Down Expand Up @@ -3454,38 +3461,119 @@ angular.module('stormpath.userService',['stormpath.CONFIG'])
];
}]);

'use strict';

/**
* @ngdoc overview
*
* @name stormpath.util
*
* @description
* This module provides general utility functions.
*/

/**
* @ngdoc object
*
* @name stormpath.util.$verifyResponse
*
* @description
* A factory that creates a {@link stormpath.util.$verifyResponse#$verifyResponse $verifyResponse}
* function.
*/
angular.module('stormpath.util', [])
.factory('$verifyResponse', function() {
/**
* @ngdoc function
*
* @name stormpath.util.$verifyResponse#$verifyResponse
* @methodOf stormpath.util.$verifyResponse
*
* @param {Object} response The response returned from a $http.get() request
* @returns {Object} Object containing the validation result. It has a boolean field `value`
* that is true if the request passes validation, and otherwise false. If validate is false,
* it will return an error field, containing an object with the error message in `retVal.error.message`
*
* @description
* Checks whether the response has a valid format. Currently, to be valid, it
* has to be a well-formed response and have the 'application/json' content type.
*/
return function verifyResponse(response) {
if (!response || typeof response.headers !== 'function') {
return {
valid: false,
error: {
message: 'Invalid response object format'
}
};
}

var contentType = response.headers('Content-Type');

if (!contentType.startsWith('application/json')) {
return {
valid: false,
error: {
message: 'Incorrect current user API endpoint response content type: ' + contentType
}
};
}

return {
valid: true
};
};
});

(function () {
'use strict';

function ViewModelService($http, STORMPATH_CONFIG) {
function ViewModelService($http, $verifyResponse, STORMPATH_CONFIG) {
this.$http = $http;
this.$verifyResponse = $verifyResponse;
this.STORMPATH_CONFIG = STORMPATH_CONFIG;
}

ViewModelService.prototype.getLoginModel = function getLoginModel() {
var self = this;

return this.$http.get(this.STORMPATH_CONFIG.getUrl('AUTHENTICATION_ENDPOINT'), {
headers: {
'Accept': 'application/json'
}
}).then(function (response) {
var responseStatus = self.$verifyResponse(response);

if (!responseStatus.valid) {
throw responseStatus.error;
}

return response.data;
});
};

ViewModelService.prototype.getRegisterModel = function getRegisterModel() {
var self = this;

return this.$http.get(this.STORMPATH_CONFIG.getUrl('REGISTER_URI'), {
headers: {
'Accept': 'application/json'
}
}).then(function (response) {
var responseStatus = self.$verifyResponse(response);

if (!responseStatus.valid) {
throw responseStatus.error;
}

return response.data;
});
};

angular.module('stormpath.viewModelService', [])
angular.module('stormpath.viewModelService', ['stormpath.util'])
.provider('$viewModel', function () {
this.$get = ['$http', 'STORMPATH_CONFIG', function viewModelFactory($http, STORMPATH_CONFIG) {
return new ViewModelService($http, STORMPATH_CONFIG);
this.$get = ['$http', '$verifyResponse', 'STORMPATH_CONFIG', function viewModelFactory($http, $verifyResponse, STORMPATH_CONFIG) {
return new ViewModelService($http, $verifyResponse, STORMPATH_CONFIG);
}];
});
}());
Expand Down
4 changes: 2 additions & 2 deletions dist/stormpath-sdk-angularjs.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stormpath-sdk-angularjs.tpls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* stormpath-sdk-angularjs
* Copyright Stormpath, Inc. 2016
*
* @version v1.1.1-dev-2016-10-28
* @version v1.1.1-dev-2016-10-31
* @link https://github.com/stormpath/stormpath-sdk-angularjs
* @license Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/stormpath-sdk-angularjs.tpls.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/stormpath.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Currently, this provider does not have any configuration methods.
*/

angular.module('stormpath.userService',['stormpath.CONFIG'])
angular.module('stormpath.userService',['stormpath.CONFIG', 'stormpath.util'])
.provider('$user', [function $userProvider(){

/**
Expand Down Expand Up @@ -74,8 +74,8 @@ angular.module('stormpath.userService',['stormpath.CONFIG'])
};

this.$get = [
'$q','$http','STORMPATH_CONFIG','$rootScope','$spFormEncoder','$spErrorTransformer',
function userServiceFactory($q,$http,STORMPATH_CONFIG,$rootScope,$spFormEncoder,$spErrorTransformer){
'$q','$http','STORMPATH_CONFIG','$rootScope','$spFormEncoder','$spErrorTransformer', '$verifyResponse',
function userServiceFactory($q,$http,STORMPATH_CONFIG,$rootScope,$spFormEncoder,$spErrorTransformer, $verifyResponse){
function UserService(){
this.cachedUserOp = null;

Expand Down Expand Up @@ -243,6 +243,13 @@ angular.module('stormpath.userService',['stormpath.CONFIG'])
self.cachedUserOp = op;

$http.get(STORMPATH_CONFIG.getUrl('CURRENT_USER_URI'),{withCredentials:true}).then(function(response){
var responseStatus = $verifyResponse(response);

if (!responseStatus.valid) {
self.currentUser = false;
return op.reject(responseStatus.error);
}

self.cachedUserOp = null;
self.currentUser = new User(response.data.account || response.data);
currentUserEvent(self.currentUser);
Expand Down
63 changes: 63 additions & 0 deletions src/stormpath.verify-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

/**
* @ngdoc overview
*
* @name stormpath.util
*
* @description
* This module provides general utility functions.
*/

/**
* @ngdoc object
*
* @name stormpath.util.$verifyResponse
*
* @description
* A factory that creates a {@link stormpath.util.$verifyResponse#$verifyResponse $verifyResponse}
* function.
*/
angular.module('stormpath.util', [])
.factory('$verifyResponse', function() {
/**
* @ngdoc function
*
* @name stormpath.util.$verifyResponse#$verifyResponse
* @methodOf stormpath.util.$verifyResponse
*
* @param {Object} response The response returned from a $http.get() request
* @returns {Object} Object containing the validation result. It has a boolean field `value`
* that is true if the request passes validation, and otherwise false. If validate is false,
* it will return an error field, containing an object with the error message in `retVal.error.message`
*
* @description
* Checks whether the response has a valid format. Currently, to be valid, it
* has to be a well-formed response and have the 'application/json' content type.
*/
return function verifyResponse(response) {
if (!response || typeof response.headers !== 'function') {
return {
valid: false,
error: {
message: 'Invalid response object format'
}
};
}

var contentType = response.headers('Content-Type');

if (!contentType.startsWith('application/json')) {
return {
valid: false,
error: {
message: 'Incorrect current user API endpoint response content type: ' + contentType
}
};
}

return {
valid: true
};
};
});
25 changes: 21 additions & 4 deletions src/stormpath.view-model.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
(function () {
'use strict';

function ViewModelService($http, STORMPATH_CONFIG) {
function ViewModelService($http, $verifyResponse, STORMPATH_CONFIG) {
this.$http = $http;
this.$verifyResponse = $verifyResponse;
this.STORMPATH_CONFIG = STORMPATH_CONFIG;
}

ViewModelService.prototype.getLoginModel = function getLoginModel() {
var self = this;

return this.$http.get(this.STORMPATH_CONFIG.getUrl('AUTHENTICATION_ENDPOINT'), {
headers: {
'Accept': 'application/json'
}
}).then(function (response) {
var responseStatus = self.$verifyResponse(response);

if (!responseStatus.valid) {
throw responseStatus.error;
}

return response.data;
});
};

ViewModelService.prototype.getRegisterModel = function getRegisterModel() {
var self = this;

return this.$http.get(this.STORMPATH_CONFIG.getUrl('REGISTER_URI'), {
headers: {
'Accept': 'application/json'
}
}).then(function (response) {
var responseStatus = self.$verifyResponse(response);

if (!responseStatus.valid) {
throw responseStatus.error;
}

return response.data;
});
};

angular.module('stormpath.viewModelService', [])
angular.module('stormpath.viewModelService', ['stormpath.util'])
.provider('$viewModel', function () {
this.$get = ['$http', 'STORMPATH_CONFIG', function viewModelFactory($http, STORMPATH_CONFIG) {
return new ViewModelService($http, STORMPATH_CONFIG);
this.$get = ['$http', '$verifyResponse', 'STORMPATH_CONFIG', function viewModelFactory($http, $verifyResponse, STORMPATH_CONFIG) {
return new ViewModelService($http, $verifyResponse, STORMPATH_CONFIG);
}];
});
}());