Files
meanTorrent/modules/users/client/controllers/settings.client.controller.js

72 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-03-26 01:30:58 +02:00
'use strict';
2014-04-02 19:34:32 +03:00
angular.module('users').controller('SettingsController', ['$scope', '$http', '$location', 'Users', 'Authentication',
2014-05-20 18:22:38 +03:00
function($scope, $http, $location, Users, Authentication) {
$scope.user = Authentication.user;
2014-03-26 01:30:58 +02:00
2014-05-20 18:22:38 +03:00
// If user is not signed in then redirect back home
if (!$scope.user) $location.path('/');
2014-03-26 01:30:58 +02:00
2014-05-20 18:22:38 +03:00
// Check if there are additional accounts
$scope.hasConnectedAdditionalSocialAccounts = function(provider) {
for (var i in $scope.user.additionalProvidersData) {
return true;
}
2014-04-16 02:00:23 +03:00
2014-05-20 18:22:38 +03:00
return false;
};
2014-04-16 02:00:23 +03:00
2014-05-20 18:22:38 +03:00
// Check if provider is already in use with current user
$scope.isConnectedSocialAccount = function(provider) {
return $scope.user.provider === provider || ($scope.user.additionalProvidersData && $scope.user.additionalProvidersData[provider]);
};
2014-04-16 02:00:23 +03:00
2014-05-20 18:22:38 +03:00
// Remove a user social account
$scope.removeUserSocialAccount = function(provider) {
$scope.success = $scope.error = null;
2014-04-16 02:00:23 +03:00
2014-11-10 23:12:33 +02:00
$http.delete('/api/users/accounts', {
2014-05-20 18:22:38 +03:00
params: {
provider: provider
}
}).success(function(response) {
// If successful show success message and clear form
$scope.success = true;
$scope.user = Authentication.user = response;
}).error(function(response) {
$scope.error = response.message;
});
};
2014-03-26 01:30:58 +02:00
2014-05-20 18:22:38 +03:00
// Update a user profile
2014-06-05 22:32:32 -07:00
$scope.updateUserProfile = function(isValid) {
2014-11-10 23:12:33 +02:00
if (isValid){
2014-06-05 22:32:32 -07:00
$scope.success = $scope.error = null;
var user = new Users($scope.user);
2014-11-10 23:12:33 +02:00
2014-06-05 22:32:32 -07:00
user.$update(function(response) {
$scope.success = true;
Authentication.user = response;
}, function(response) {
$scope.error = response.data.message;
});
} else {
$scope.submitted = true;
}
2014-05-20 18:22:38 +03:00
};
2014-03-26 01:30:58 +02:00
2014-05-20 18:22:38 +03:00
// Change user password
$scope.changeUserPassword = function() {
$scope.success = $scope.error = null;
2014-11-10 23:12:33 +02:00
$http.post('/api/users/password', $scope.passwordDetails).success(function(response) {
2014-05-20 18:22:38 +03:00
// If successful show success message and clear form
$scope.success = true;
$scope.passwordDetails = null;
}).error(function(response) {
$scope.error = response.message;
});
};
}
2014-11-10 23:12:33 +02:00
]);