Changed auth.resource to use the new resource promise and also enhanced the resource promise + docs.
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
/* contains random bits and pieces we neede to make the U6 UI behave */
|
||||
|
||||
Umbraco.Sys.registerNamespace("Umbraco.Application.LegacyHelper");
|
||||
|
||||
(function ($) {
|
||||
|
||||
|
||||
$(document).ready(function () {
|
||||
Umbraco.Application.LegacyHelper.scaleScrollables("body");
|
||||
scaleScrollables("body");
|
||||
|
||||
$(window).bind("resize", function () {
|
||||
Umbraco.Application.LegacyHelper.scaleScrollables("body");
|
||||
scaleScrollables("body");
|
||||
});
|
||||
|
||||
$("body").click(function (event) {
|
||||
@@ -22,21 +20,17 @@ Umbraco.Sys.registerNamespace("Umbraco.Application.LegacyHelper");
|
||||
});
|
||||
});
|
||||
|
||||
/** Static helper class */
|
||||
Umbraco.Application.LegacyHelper = {
|
||||
function scaleScrollables(selector) {
|
||||
$(".umb-scrollable").each(function() {
|
||||
var el = jQuery(this);
|
||||
var totalOffset = 0;
|
||||
var offsety = el.data("offset-y");
|
||||
|
||||
scaleScrollables: function (selector) {
|
||||
$(".umb-scrollable").each(function() {
|
||||
var el = jQuery(this);
|
||||
var totalOffset = 0;
|
||||
var offsety = el.data("offset-y");
|
||||
if (offsety != undefined)
|
||||
totalOffset += offsety;
|
||||
|
||||
if (offsety != undefined)
|
||||
totalOffset += offsety;
|
||||
|
||||
el.height($(window).height() - (el.offset().top + totalOffset));
|
||||
});
|
||||
}
|
||||
};
|
||||
el.height($(window).height() - (el.offset().top + totalOffset));
|
||||
});
|
||||
}
|
||||
|
||||
})(jQuery);
|
||||
@@ -3,7 +3,7 @@
|
||||
* @name umbraco.resources.authResource
|
||||
* @description Loads in data for authentication
|
||||
**/
|
||||
function authResource($q, $http, umbDataFormatter, umbRequestHelper) {
|
||||
function authResource($q, $http, angularHelper) {
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getLoginUrl(username, password) {
|
||||
@@ -15,52 +15,37 @@ function authResource($q, $http, umbDataFormatter, umbRequestHelper) {
|
||||
return Umbraco.Sys.ServerVariables.authenticationApiBaseUrl + "GetCurrentUser";
|
||||
}
|
||||
|
||||
var _currentUser;
|
||||
//var currentUser;
|
||||
|
||||
|
||||
return {
|
||||
currentUser: _currentUser,
|
||||
//currentUser: currentUser,
|
||||
|
||||
/** Logs the user in if the credentials are good */
|
||||
performLogin: function (username, password) {
|
||||
|
||||
var deferred = $q.defer();
|
||||
//send the data
|
||||
$http.post(getLoginUrl(username, password)).
|
||||
success(function (data, status, headers, config) {
|
||||
_currentUser = data;
|
||||
deferred.resolve(data);
|
||||
}).
|
||||
error(function (data, status, headers, config) {
|
||||
_currentUser = data;
|
||||
deferred.reject('Login failed for user ' + username);
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
return angularHelper.resourcePromise(
|
||||
$http.post(getLoginUrl(username, password)),
|
||||
'Login failed for user ' + username);
|
||||
},
|
||||
|
||||
/** Sends a request to the server to check if the current cookie value is valid for the user */
|
||||
isAuthenticated: function () {
|
||||
|
||||
var deferred = $q.defer();
|
||||
//send the data
|
||||
$http.get(getIsAuthUrl()).
|
||||
success(function (data, status, headers, config) {
|
||||
|
||||
//204 - means the current user is not-authorized, the result was empty.
|
||||
if (status === 204) {
|
||||
//if it's unauthorized it just means we are not authenticated so we'll just return null
|
||||
deferred.reject(null);
|
||||
}
|
||||
else {
|
||||
deferred.resolve(data);
|
||||
}
|
||||
}).
|
||||
error(function (data, status, headers, config) {
|
||||
deferred.reject('Server call failed for checking authorization');
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getIsAuthUrl()),
|
||||
{
|
||||
success: function (data, status, headers, config) {
|
||||
//204 - means the current user is not-authorized, the result was empty.
|
||||
if (status === 204) {
|
||||
//if it's unauthorized it just means we are not authenticated so we'll just return null
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return data;
|
||||
}
|
||||
},
|
||||
errorMsg: 'Server call failed for checking authorization'
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name umbraco.services.angularHelper
|
||||
* @name umbraco.services.legacyJsLoader
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* Some angular helper/extension methods
|
||||
* Used to lazy load in any JS dependencies that need to be manually loaded in
|
||||
*/
|
||||
function legacyJsLoader(scriptLoader) {
|
||||
return {
|
||||
@@ -33,23 +33,72 @@ angular.module('umbraco.services').factory('legacyJsLoader', legacyJsLoader);
|
||||
function angularHelper($log, $q) {
|
||||
return {
|
||||
|
||||
resourcePromise: function (httpPromise, errorMsg) {
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name resourcePromise
|
||||
* methodOf umbraco.services.angularHelper
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* This returns a promise with an underlying http call, it is a helper method to reduce
|
||||
* the amount of duplicate code needed to query http resources and automatically handle any
|
||||
* Http errors. See /docs/source/using-promises-resources.md
|
||||
*
|
||||
* @param {object} opts A mixed object which can either be a string representing the error message to be
|
||||
* returned OR an object containing either:
|
||||
* { success: successCallback, errorMsg: errorMessage }
|
||||
* OR
|
||||
* { success: successCallback, error: errorCallback }
|
||||
* In both of the above, the successCallback must accept these parameters: data, status, headers, config
|
||||
* If using the errorCallback it must accept these parameters: data, status, headers, config
|
||||
* The success callback must return the data which will be resolved by the deferred object.
|
||||
* The error callback must return an object containing: {errorMsg: errorMessage, data: originalData }
|
||||
*/
|
||||
resourcePromise: function (httpPromise, opts) {
|
||||
var deferred = $q.defer();
|
||||
|
||||
/** The default success callback used if one is not supplied in the opts */
|
||||
function defaultSuccess(data, status, headers, config) {
|
||||
//when it's successful, just return the data
|
||||
return data;
|
||||
}
|
||||
|
||||
/** The default error callback used if one is not supplied in the opts */
|
||||
function defaultError(data, status, headers, config) {
|
||||
return {
|
||||
errorMsg: (opts.errorMsg ? opts.errorMsg : 'An error occurred!'),
|
||||
data: data
|
||||
};
|
||||
}
|
||||
|
||||
//set the callbacks to the defaults if there aren't any
|
||||
if (!opts.success) {
|
||||
opts.success = defaultSuccess;
|
||||
}
|
||||
if (!opts.error) {
|
||||
opts.error = defaultError;
|
||||
}
|
||||
|
||||
httpPromise.success(function (data, status, headers, config) {
|
||||
|
||||
//invoke the callback
|
||||
var result = opts.success.apply(this, [data, status, headers, config]);
|
||||
|
||||
//when it's successful, just return the data
|
||||
deferred.resolve(data);
|
||||
deferred.resolve(result);
|
||||
|
||||
}).error(function(data, status, headers, config) {
|
||||
|
||||
//invoke the callback
|
||||
var result = opts.error.apply(this, [data, status, headers, config]);
|
||||
|
||||
//when there's an erorr...
|
||||
// TODO: Deal with the error in a global way
|
||||
|
||||
//return an error object including the error message for UI
|
||||
deferred.reject({
|
||||
errorMsg: errorMsg,
|
||||
data: data
|
||||
errorMsg: result.errorMsg,
|
||||
data: result.data
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user