Files
meanTorrent/modules/users/client/controllers/authentication.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

92 lines
2.5 KiB
JavaScript

(function () {
'use strict';
angular
.module('users')
.controller('AuthenticationController', AuthenticationController);
AuthenticationController.$inject = ['$scope', '$state', 'UsersService', '$location', '$window', 'Authentication', 'PasswordValidator'];
function AuthenticationController($scope, $state, UsersService, $location, $window, Authentication, PasswordValidator) {
var vm = this;
vm.authentication = Authentication;
vm.getPopoverMsg = PasswordValidator.getPopoverMsg;
vm.signup = signup;
vm.signin = signin;
vm.callOauthProvider = callOauthProvider;
// Get an eventual error defined in the URL query string:
vm.error = $location.search().err;
// If user is signed in then redirect back home
if (vm.authentication.user) {
$location.path('/');
}
function signup(isValid) {
vm.error = null;
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
return false;
}
UsersService.userSignup(vm.credentials)
.then(onUserSignupSuccess)
.catch(onUserSignupError);
}
function signin(isValid) {
vm.error = null;
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
return false;
}
UsersService.userSignin(vm.credentials)
.then(onUserSigninSuccess)
.catch(onUserSigninError);
}
// OAuth provider request
function callOauthProvider(url) {
if ($state.previous && $state.previous.href) {
url += '?redirect_to=' + encodeURIComponent($state.previous.href);
}
// 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;
}
}
}());