UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

121 lines (105 loc) 4.96 kB
(function () { 'use strict'; angular.module('uniko.authSatellizer') .controller('LoginCtrl', LoginCtrl); LoginCtrl.$inject = ['$scope', 'Auth', '$location', 'toastr', '$state', '$translate', '$auth', '$uibModal', '$uibModalInstance', 'Utils', 'lodash', 'CoupleAccount']; function LoginCtrl($scope, Auth, $location, toastr, $state, $translate, $auth, $uibModal, $uibModalInstance, Utils, lodash, CoupleAccount) { var loginCtrl = this; $scope.showPassword = false; $scope.login = function () { var user = $scope.user; Auth.login(user.email, user.password) .then(function (result) { var user = result.user; getMixpanel().identify(user.id); Utils.list().then(function (response) { var list = response.data; var aux = lodash.find(list, {name: 'is2FA'}).value.is2FA; if (aux) { if(!user.is2FA){ CoupleAccount .prototype$sendAuthyToken({id: user.id}, {phone: user.phoneNumber1}) .$promise .then(function (response) { $scope.$emit('Account::login', user); }) .catch(function(err){ console.log.bind(console) $scope.$emit('Account::login', user); }); } else { $scope.$emit('Account::login', user); } }else { $scope.$emit('Account::login', user); } }); }) .catch(function (error) { if (error.status === 401) { $translate('invalid-credentials').then(function (translation) { toastr.error(translation); }); } }); }; $scope.authenticate = function (provider) { $auth .authenticate(provider, {}) .then(function (response) { ; var user = response.data.user; var token = response.data.token; token.user = user; Auth.setAuthData(token); getMixpanel().identify(user.id); Utils.list().then(function (response) { var list = response.data; var aux = lodash.find(list, {name: 'is2FA'}).value.is2FA; if (aux) { if(!user.is2FA){ CoupleAccount .prototype$sendAuthyToken({id: user.id}, {phone: user.phoneNumber1}) .$promise .then(function (response) { $scope.$emit('Account::login', user); }) .catch(function(err){ console.log.bind(console) $scope.$emit('Account::login', user); }); } else { $scope.$emit('Account::login', user); } }else { $scope.$emit('Account::login', user); } }); }) .catch(function (response) { if (response.status === 409) { return toastr.error('La cuenta de Facebook que intentas asociar ya se encuentra asociada a otra cuenta'); } else if(response.status === 410 && response.data.error.facebook) { return $scope.signup("facebook"); } toastr.error(response.data.message); }); }; loginCtrl.dismiss = function () { $uibModalInstance.dismiss(); }; $scope.close = function () { $uibModalInstance.dismiss(); }; $scope.signup = function (facebook) { $uibModalInstance.dismiss(); $state.go('start-onboarding', {onboardingId: 'start-new-account-1', facebook: facebook}); }; $scope.forgot = function () { $uibModalInstance.dismiss(); $state.go('forgot'); }; $scope.togglePassword = function () { $scope.showPassword = !$scope.showPassword; }; } })();