cobuild-angular-stack
Version:
Base stack angular sass jade gulp
373 lines (324 loc) • 15.8 kB
JavaScript
(function () {
'use strict';
angular.module('uniko.authSatellizer')
.controller('SignupCtrl', SignupCtrl)
.controller('SignupOnboardingCtrl', SignupOnboardingCtrl);
SignupCtrl.$inject = ['$scope', 'Auth', '$uibModalInstance', 'toastr', '$localStorage', '$analytics', '$auth', '$translate','lodash', 'Utils', 'CoupleAccount'];
function SignupCtrl($scope, Auth, $uibModalInstance, toastr, $localStorage, $analytics, $auth, $translate, lodash, Utils, CoupleAccount) {
var SgupCtrl = this;
$scope.validPhone = /^\d{10}$/;
$scope.validEmail = /^[\w\+-_]+@\w+\.(\w{2,5})+(\.\w{2,5})?$/;
SgupCtrl.dismiss = function () {
$uibModalInstance.dismiss();
};
$scope.close = function () {
$uibModalInstance.dismiss();
};
$scope.showPassword = false;
$scope.togglePassword = function () {
$scope.showPassword = !$scope.showPassword;
};
$scope.$watch('user.confirmPassword', function (newVal) {
$scope.signupForm.password.$setValidity('match', newVal === $scope.user.password);
});
$scope.$watch('user.email', function () {
$scope.signupForm.email.$setValidity('exists', true);
});
$scope.signup = function () {
var user = lodash.clone($scope.user);
delete user.accept;
Auth.register(user.email, user.password)
.then(function (dataRegister) {
getMixpanel().identify(user.id);
getMixpanel().people.set({
'distinct_id': dataRegister.id,
'$email': user.email,
'$created': dataRegister.createdAt,
"Adds to Cart": 0,
"Lifetime Acumulated": 0,
"RSVP": 0,
"Messages": 0
});
$analytics.eventTrack('Signup', {'Email Address': user.email, 'User ID': dataRegister.fakeid});
$analytics.eventTrack('Account Creation', {'Date': dataRegister.createdAt});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Signup',
eventLabel: 'Cuenta Creada'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Mail Signup',
eventLabel: 'Cuenta creada con email'
});
Auth.login(user.email, user.password).then(function (result) {
var dataLogin = result.user
$localStorage.newAccount = true;
Utils.list().then(function (response) {
var list = response.data;
var aux = lodash.find(list, {name: 'is2FA'}).value.is2FA;
if (aux) {
if(!dataLogin.is2FA){
CoupleAccount
.prototype$sendAuthyToken({id: dataLogin.id}, {phone: dataLogin.phoneNumber1})
.$promise
.then(function (response) {
$scope.$emit('Account::login', dataLogin);
})
.catch(function(err){
console.log.bind(console)
$scope.$emit('Account::login', dataLogin);
});
}else{
$scope.$emit('Account::login', dataLogin);
}
} else {
$scope.$emit('Account::login', dataLogin);
}
});
})
.catch(function (response) {
if (response.status === 422) {
$scope.signupForm.email.$setValidity('exists', false);
//toastr.error(response);
}
});;
})
.catch(function (response) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
if (response.status === 422) {
$scope.signupForm.email.$setValidity('exists', false);
//toastr.error(response);
}
});
};
$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);
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Signup',
eventLabel: 'Cuenta Creada'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Facebook Signup',
eventLabel: 'Cuenta creada con Facebook'
});
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(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
if (response.status === 409) {
return toastr.error('La cuenta de Facebook que intentas asociar ya se encuentra asociada a otra cuenta');
}
toastr.error(response.data.message);
});
};
ga('send', {
hitType: 'event',
eventCategory: 'display',
eventAction: 'Crea tu Cuenta',
eventLabel: 'Crea tu Cuenta'
});
}
SignupOnboardingCtrl.$inject = ['$scope', 'toastr', '$localStorage', '$auth', '$translate', 'Onboarding', 'CoupleAccount','Auth', 'Utils', 'lodash', 'instapageServices'];
function SignupOnboardingCtrl($scope, toastr, $localStorage, $auth, $translate, Onboarding, CoupleAccount,Auth, Utils,lodash, instapageServices) {
var stepCtrl = this;
$scope.validPhone = /^\d{10}$/;
$scope.validEmail = /^[\w\+-_]+@\w+\.(\w{2,5})+(\.\w{2,5})?$/;
$scope.user = {
accept : true
};
$scope.showPassword = false;
$scope.togglePassword = function () {
$scope.showPassword = !$scope.showPassword;
};
setInterval(function() {
var imgs = $(".loader-image:visible");
if(!$(".loading-init").hasClass('hidden')) {
if(imgs.length == 5) {
$(".loader-image").addClass("hidden")
} else {
$("#loader_"+(imgs.length + 1)).removeClass("hidden")
}
}
}, 500);
$scope.$watch('user.email', function () {
$scope.signupForm.email.$setValidity('exists', true);
});
$scope.signup = function (onboarding, step, user) {
$(".loading-init").removeClass("hidden");
CoupleAccount
.getCurrent()
.$promise
.then(function (couple) {
couple.email = user.email;
if(!$scope.loginWithFacebook){
couple.password = user.password;
}
couple.phoneNumber1 = user.phoneNumber1;
couple.tempAccount = false;
Utils.list().then(function (response) {
var list = response.data;
var aux = lodash.find(list, {name: 'is2FA'}).value.is2FA;
if (aux) {
if(!couple.is2FA){
CoupleAccount
.prototype$sendAuthyToken({id: couple.id}, {phone: couple.phoneNumber1})
.$promise
.then(function (response) {
})
.catch(function() {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
});
}
}
});
return CoupleAccount.updateMainAttributes(couple, function () {
if(!$scope.loginWithFacebook){
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Signup',
eventLabel: 'Cuenta Creada'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Mail Signup',
eventLabel: 'Cuenta creada con email'
});
}
return Onboarding.end(onboarding.id);
}, function () {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
$scope.signupForm.email.$setValidity('exists', false);
});
});
};
$scope.signupInstapage = function (user) {
$(".loading-init").removeClass("hidden");
instapageServices.account.email = user.email;
if(!$scope.loginWithFacebook){
instapageServices.account.password = user.password;
}
instapageServices.account.phoneNumber1 = user.phoneNumber1;
instapageServices.account.tempAccount = false;
return CoupleAccount.create(instapageServices.account, function (account) {
if(!$scope.loginWithFacebook){
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Signup',
eventLabel: 'Cuenta Creada'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Mail Signup',
eventLabel: 'Cuenta creada con email'
});
}
Auth.login(user.email, user.password)
.then(function (result) {
window.open(window.location.origin);
$scope.$emit('Account::login', user);
})
.catch(function (error) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
if (error.status === 401) {
$translate('invalid-credentials').then(function (translation) {
toastr.error(translation);
});
}
});
}, function () {
$scope.signupForm.email.$setValidity('exists', false);
});
};
$scope.authenticate = function (onboarding, step, provider) {
$(".loading-init").removeClass("hidden");
$auth
.authenticate(provider, {overWriteData: true})
.then(function (response) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Signup',
eventLabel: 'Cuenta Creada'
});
ga('send', {
hitType: 'event',
eventCategory: 'submit',
eventAction: 'Facebook Signup',
eventLabel: 'Cuenta creada con Facebook'
});
var token = response.data.token;
token.user = response.data.user;
Auth.setAuthData(token);
$scope.user.email = response.data.user.email;
$scope.loginWithFacebook = true;
//return Onboarding.end(onboarding.id);
})
.catch(function (response) {
if(!$(".loading-init").hasClass('hidden')) {
$(".loading-init").addClass("hidden");
}
if (response.status === 422 || response.status === 409) {
return toastr.error('La cuenta de Facebook que intentas asociar ya se encuentra asociada a otra cuenta');
}
});
};
ga('send', {
hitType: 'event',
eventCategory: 'display',
eventAction: 'Crea tu Cuenta',
eventLabel: 'Crea tu Cuenta'
});
}
})();