Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Deprecated $http success/error promise methods (#1508)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mleanos authored Sep 17, 2016
1 parent acc111e commit fa13804
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
.module('users')
.controller('AuthenticationController', AuthenticationController);

AuthenticationController.$inject = ['$scope', '$state', '$http', '$location', '$window', 'Authentication', 'PasswordValidator'];
AuthenticationController.$inject = ['$scope', '$state', 'UsersService', '$location', '$window', 'Authentication', 'PasswordValidator'];

function AuthenticationController($scope, $state, $http, $location, $window, Authentication, PasswordValidator) {
function AuthenticationController($scope, $state, UsersService, $location, $window, Authentication, PasswordValidator) {
var vm = this;

vm.authentication = Authentication;
Expand All @@ -33,15 +33,9 @@
return false;
}

$http.post('/api/auth/signup', vm.credentials).success(function (response) {
// If successful we assign the response to the global user model
vm.authentication.user = response;

// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}).error(function (response) {
vm.error = response.message;
});
UsersService.userSignup(vm.credentials)
.then(onUserSignupSuccess)
.catch(onUserSignupError);
}

function signin(isValid) {
Expand All @@ -53,15 +47,9 @@
return false;
}

$http.post('/api/auth/signin', vm.credentials).success(function (response) {
// If successful we assign the response to the global user model
vm.authentication.user = response;

// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}).error(function (response) {
vm.error = response.message;
});
UsersService.userSignin(vm.credentials)
.then(onUserSigninSuccess)
.catch(onUserSigninError);
}

// OAuth provider request
Expand All @@ -73,5 +61,31 @@
// Effectively call OAuth authentication route:
$window.location.href = url;
}

// Authentication Callbacks

function onUserSignupSuccess(response) {
// If successful we assign the response to the global user model
vm.authentication.user = response;

// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}

function onUserSignupError(response) {
vm.error = response.data.message;
}

function onUserSigninSuccess(response) {
// If successful we assign the response to the global user model
vm.authentication.user = response;

// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}

function onUserSigninError(response) {
vm.error = response.data.message;
}
}
}());
56 changes: 34 additions & 22 deletions modules/users/client/controllers/password.client.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
.module('users')
.controller('PasswordController', PasswordController);

PasswordController.$inject = ['$scope', '$stateParams', '$http', '$location', 'Authentication', 'PasswordValidator'];
PasswordController.$inject = ['$scope', '$stateParams', 'UsersService', '$location', 'Authentication', 'PasswordValidator'];

function PasswordController($scope, $stateParams, $http, $location, Authentication, PasswordValidator) {
function PasswordController($scope, $stateParams, UsersService, $location, Authentication, PasswordValidator) {
var vm = this;

vm.resetUserPassword = resetUserPassword;
Expand All @@ -30,16 +30,9 @@
return false;
}

$http.post('/api/auth/forgot', vm.credentials).success(function (response) {
// Show user success message and clear form
vm.credentials = null;
vm.success = response.message;

}).error(function (response) {
// Show user error message and clear form
vm.credentials = null;
vm.error = response.message;
});
UsersService.requestPasswordReset(vm.credentials)
.then(onRequestPasswordResetSuccess)
.catch(onRequestPasswordResetError);
}

// Change user password
Expand All @@ -52,18 +45,37 @@
return false;
}

$http.post('/api/auth/reset/' + $stateParams.token, vm.passwordDetails).success(function (response) {
// If successful show success message and clear form
vm.passwordDetails = null;
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;
}

// Attach user profile
Authentication.user = response;
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');
}

// And redirect to the index page
$location.path('/password/reset/success');
}).error(function (response) {
vm.error = response.message;
});
function onResetPasswordError(response) {
vm.error = response.data.message;
}
}
}());
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
.module('users')
.controller('ChangePasswordController', ChangePasswordController);

ChangePasswordController.$inject = ['$scope', '$http', 'Authentication', 'PasswordValidator'];
ChangePasswordController.$inject = ['$scope', '$http', 'Authentication', 'UsersService', 'PasswordValidator'];

function ChangePasswordController($scope, $http, Authentication, PasswordValidator) {
function ChangePasswordController($scope, $http, Authentication, UsersService, PasswordValidator) {
var vm = this;

vm.user = Authentication.user;
Expand All @@ -24,14 +24,20 @@
return false;
}

$http.post('/api/users/password', vm.passwordDetails).success(function (response) {
// If successful show success message and clear form
$scope.$broadcast('show-errors-reset', 'vm.passwordForm');
vm.success = true;
vm.passwordDetails = null;
}).error(function (response) {
vm.error = response.message;
});
UsersService.changePassword(vm.passwordDetails)
.then(onChangePasswordSuccess)
.catch(onChangePasswordError);
}

function onChangePasswordSuccess(response) {
// If successful show success message and clear form
$scope.$broadcast('show-errors-reset', 'vm.passwordForm');
vm.success = true;
vm.passwordDetails = null;
}

function onChangePasswordError(response) {
vm.error = response.data.message;
}
}
}());
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
.module('users')
.controller('SocialAccountsController', SocialAccountsController);

SocialAccountsController.$inject = ['$scope', '$http', 'Authentication'];
SocialAccountsController.$inject = ['$scope', 'UsersService', 'Authentication'];

function SocialAccountsController($scope, $http, Authentication) {
function SocialAccountsController($scope, UsersService, Authentication) {
var vm = this;

vm.user = Authentication.user;
Expand All @@ -29,17 +29,19 @@
function removeUserSocialAccount(provider) {
vm.success = vm.error = null;

$http.delete('/api/users/accounts', {
params: {
provider: provider
}
}).success(function (response) {
// If successful show success message and clear form
vm.success = true;
vm.user = Authentication.user = response;
}).error(function (response) {
vm.error = response.message;
});
UsersService.removeSocialAccount(provider)
.then(onRemoveSocialAccountSuccess)
.catch(onRemoveSocialAccountError);
}

function onRemoveSocialAccountSuccess(response) {
// If successful show success message and clear form
vm.success = true;
vm.user = Authentication.user = response;
}

function onRemoveSocialAccountError(response) {
vm.error = response.message;
}
}
}());
56 changes: 55 additions & 1 deletion modules/users/client/services/users.client.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,65 @@
UsersService.$inject = ['$resource'];

function UsersService($resource) {
return $resource('api/users', {}, {
var Users = $resource('api/users', {}, {
update: {
method: 'PUT'
},
updatePassword: {
method: 'POST',
url: 'api/users/password'
},
deleteProvider: {
method: 'DELETE',
url: 'api/users/accounts',
params: {
provider: '@provider'
}
},
sendPasswordResetToken: {
method: 'POST',
url: 'api/auth/forgot'
},
resetPasswordWithToken: {
method: 'POST',
url: 'api/auth/reset/:token'
},
signup: {
method: 'POST',
url: 'api/auth/signup'
},
signin: {
method: 'POST',
url: 'api/auth/signin'
}
});

angular.extend(Users, {
changePassword: function (passwordDetails) {
return this.updatePassword(passwordDetails).$promise;
},
removeSocialAccount: function (provider) {
return this.deleteProvider({
provider: provider // api expects provider as a querystring parameter
}).$promise;
},
requestPasswordReset: function (credentials) {
return this.sendPasswordResetToken(credentials).$promise;
},
resetPassword: function (token, passwordDetails) {
return this.resetPasswordWithToken({
token: token // api expects token as a parameter (i.e. /:token)
}, passwordDetails).$promise;
},
userSignup: function (credentials) {
return this.signup(credentials).$promise;
},
userSignin: function (credentials) {
return this.signin(credentials).$promise;
}
});

return Users;
}

// TODO this should be Users service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
describe('$scope.signin()', function () {
it('should login with a correct user and password', function () {
// Test expected GET request
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');
$httpBackend.when('POST', 'api/auth/signin').respond(200, { username: 'Fred' });

scope.vm.signin(true);
$httpBackend.flush();

// Test scope value
expect(scope.vm.authentication.user).toEqual('Fred');
expect(scope.vm.authentication.user.username).toEqual('Fred');
expect($location.url()).toEqual('/');
});

Expand All @@ -75,7 +75,7 @@
spyOn($state, 'go');

// Test expected GET request
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');
$httpBackend.when('POST', 'api/auth/signin').respond(200, 'Fred');

scope.vm.signin(true);
$httpBackend.flush();
Expand All @@ -88,7 +88,7 @@

it('should fail to log in with nothing', function () {
// Test expected POST request
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
$httpBackend.expectPOST('api/auth/signin').respond(400, {
'message': 'Missing credentials'
});

Expand All @@ -101,11 +101,11 @@

it('should fail to log in with wrong credentials', function () {
// Foo/Bar combo assumed to not exist
scope.vm.authentication.user = 'Foo';
scope.vm.authentication.user = { usersname: 'Foo' };
scope.vm.credentials = 'Bar';

// Test expected POST request
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
$httpBackend.expectPOST('api/auth/signin').respond(400, {
'message': 'Unknown user'
});

Expand All @@ -121,20 +121,20 @@
it('should register with correct data', function () {
// Test expected GET request
scope.vm.authentication.user = 'Fred';
$httpBackend.when('POST', '/api/auth/signup').respond(200, 'Fred');
$httpBackend.when('POST', 'api/auth/signup').respond(200, { username: 'Fred' });

scope.vm.signup(true);
$httpBackend.flush();

// test scope value
expect(scope.vm.authentication.user).toBe('Fred');
expect(scope.vm.authentication.user.username).toBe('Fred');
expect(scope.vm.error).toEqual(null);
expect($location.url()).toBe('/');
});

it('should fail to register with duplicate Username', function () {
// Test expected POST request
$httpBackend.when('POST', '/api/auth/signup').respond(400, {
$httpBackend.when('POST', 'api/auth/signup').respond(400, {
'message': 'Username already exists'
});

Expand Down
Loading

0 comments on commit fa13804

Please sign in to comment.