Files
meanTorrent/modules/users/client/controllers/password.client.controller.js
Michael Leanos fa138045e6 Deprecated $http success/error promise methods (#1508)
Replaces the $http service calls with promise based methods
of the client-side UsersService for the following:
  Users Change Password
  Users Manage Social Accounts
  Users Password Forgot
  Users Password Reset
  Users Signup
  Users Signin

Modifies tests to reflect changes.

Closes #1479
2016-09-17 12:05:21 -07:00

82 lines
2.3 KiB
JavaScript

(function () {
'use strict';
angular
.module('users')
.controller('PasswordController', PasswordController);
PasswordController.$inject = ['$scope', '$stateParams', 'UsersService', '$location', 'Authentication', 'PasswordValidator'];
function PasswordController($scope, $stateParams, UsersService, $location, Authentication, PasswordValidator) {
var vm = this;
vm.resetUserPassword = resetUserPassword;
vm.askForPasswordReset = askForPasswordReset;
vm.authentication = Authentication;
vm.getPopoverMsg = PasswordValidator.getPopoverMsg;
// If user is signed in then redirect back home
if (vm.authentication.user) {
$location.path('/');
}
// Submit forgotten password account id
function askForPasswordReset(isValid) {
vm.success = vm.error = null;
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.forgotPasswordForm');
return false;
}
UsersService.requestPasswordReset(vm.credentials)
.then(onRequestPasswordResetSuccess)
.catch(onRequestPasswordResetError);
}
// Change user password
function resetUserPassword(isValid) {
vm.success = vm.error = null;
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.resetPasswordForm');
return false;
}
UsersService.resetPassword($stateParams.token, vm.passwordDetails)
.then(onResetPasswordSuccess)
.catch(onResetPasswordError);
}
// Password Reset Callbacks
function onRequestPasswordResetSuccess(response) {
// Show user success message and clear form
vm.credentials = null;
vm.success = response.message;
}
function onRequestPasswordResetError(response) {
// Show user error message and clear form
vm.credentials = null;
vm.error = response.data.message;
}
function onResetPasswordSuccess(response) {
// If successful show success message and clear form
vm.passwordDetails = null;
// Attach user profile
Authentication.user = response;
// And redirect to the index page
$location.path('/password/reset/success');
}
function onResetPasswordError(response) {
vm.error = response.data.message;
}
}
}());