2013-08-28 00:07:28 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
The authentication controller (login & facebook)
|
|
|
|
|
*/
|
|
|
|
|
|
2014-01-12 23:00:47 +00:00
|
|
|
angular.module('authCtrl', [])
|
2014-09-16 18:52:32 +00:00
|
|
|
.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$location', '$window','ApiUrlService', '$modal',
|
|
|
|
|
function($scope, $rootScope, User, $http, $location, $window, ApiUrlService, $modal) {
|
2014-01-12 23:00:47 +00:00
|
|
|
var runAuth;
|
|
|
|
|
var showedFacebookMessage;
|
2013-09-02 05:08:28 +00:00
|
|
|
|
2014-01-12 23:00:47 +00:00
|
|
|
$scope.useUUID = false;
|
|
|
|
|
$scope.toggleUUID = function() {
|
|
|
|
|
if (showedFacebookMessage === false) {
|
2014-02-05 22:43:49 +00:00
|
|
|
alert(window.env.t('untilNoFace'));
|
2014-01-12 23:00:47 +00:00
|
|
|
showedFacebookMessage = true;
|
|
|
|
|
}
|
|
|
|
|
$scope.useUUID = !$scope.useUUID;
|
|
|
|
|
};
|
2013-09-02 05:08:28 +00:00
|
|
|
|
2014-01-12 23:00:47 +00:00
|
|
|
$scope.logout = function() {
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
window.location.href = '/logout';
|
|
|
|
|
};
|
2013-09-04 03:15:43 +00:00
|
|
|
|
2014-01-12 23:00:47 +00:00
|
|
|
runAuth = function(id, token) {
|
|
|
|
|
User.authenticate(id, token, function(err) {
|
|
|
|
|
$window.location.href = '/';
|
2013-09-06 02:33:31 +00:00
|
|
|
});
|
2014-01-12 23:00:47 +00:00
|
|
|
};
|
2013-09-02 05:08:28 +00:00
|
|
|
|
2014-01-29 20:28:11 +00:00
|
|
|
function errorAlert(data, status, headers, config) {
|
|
|
|
|
if (status === 0) {
|
2014-02-06 16:36:33 +00:00
|
|
|
$window.alert(window.env.t('noReachServer'));
|
2014-01-29 20:28:11 +00:00
|
|
|
} else if (!!data && !!data.err) {
|
|
|
|
|
$window.alert(data.err);
|
|
|
|
|
} else {
|
2014-02-11 18:30:55 +00:00
|
|
|
$window.alert(window.env.t('errorUpCase') + ' ' + status);
|
2014-01-29 20:28:11 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-12 23:00:47 +00:00
|
|
|
$scope.register = function() {
|
|
|
|
|
/*TODO highlight invalid inputs
|
|
|
|
|
we have this as a workaround for https://github.com/HabitRPG/habitrpg-mobile/issues/64
|
|
|
|
|
*/
|
|
|
|
|
if ($scope.registrationForm.$invalid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-09-16 18:52:32 +00:00
|
|
|
var url = ApiUrlService.get() + "/api/v2/register";
|
2014-08-03 16:23:13 +00:00
|
|
|
if($rootScope.selectedLanguage) url = url + '?lang=' + $rootScope.selectedLanguage.code;
|
|
|
|
|
$http.post(url, $scope.registerVals).success(function(data, status, headers, config) {
|
2014-01-12 23:00:47 +00:00
|
|
|
runAuth(data.id, data.apiToken);
|
2014-01-29 20:28:11 +00:00
|
|
|
}).error(errorAlert);
|
2014-01-12 23:00:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.auth = function() {
|
|
|
|
|
var data = {
|
2014-01-28 18:10:07 +00:00
|
|
|
username: $scope.loginUsername || $('#login-tab input[name="username"]').val(),
|
|
|
|
|
password: $scope.loginPassword || $('#login-tab input[name="password"]').val()
|
2014-01-12 23:00:47 +00:00
|
|
|
};
|
|
|
|
|
if ($scope.useUUID) {
|
|
|
|
|
runAuth($scope.loginUsername, $scope.loginPassword);
|
|
|
|
|
} else {
|
2014-09-16 18:52:32 +00:00
|
|
|
$http.post(ApiUrlService.get() + "/api/v2/user/auth/local", data)
|
2014-01-12 23:00:47 +00:00
|
|
|
.success(function(data, status, headers, config) {
|
|
|
|
|
runAuth(data.id, data.token);
|
|
|
|
|
}).error(errorAlert);
|
|
|
|
|
}
|
2013-09-06 02:33:31 +00:00
|
|
|
};
|
2013-09-04 03:40:27 +00:00
|
|
|
|
2014-01-12 23:00:47 +00:00
|
|
|
$scope.playButtonClick = function(){
|
2014-02-10 17:39:01 +00:00
|
|
|
window.ga && ga('send', 'event', 'button', 'click', 'Play');
|
2014-01-12 23:00:47 +00:00
|
|
|
if (User.authenticated()) {
|
|
|
|
|
window.location.href = '/#/tasks';
|
|
|
|
|
} else {
|
2014-02-04 21:21:12 +00:00
|
|
|
$modal.open({
|
|
|
|
|
templateUrl: 'modals/login.html'
|
|
|
|
|
// Using controller: 'AuthCtrl' it causes problems
|
|
|
|
|
});
|
2014-01-12 23:00:47 +00:00
|
|
|
}
|
2014-08-09 09:57:17 +00:00
|
|
|
};
|
2013-09-06 13:33:23 +00:00
|
|
|
|
2014-01-12 23:00:47 +00:00
|
|
|
$scope.passwordReset = function(email){
|
2014-09-16 18:52:32 +00:00
|
|
|
$http.post(ApiUrlService.get() + '/api/v2/user/reset-password', {email:email})
|
2014-01-12 23:00:47 +00:00
|
|
|
.success(function(){
|
2014-02-05 22:43:49 +00:00
|
|
|
alert(window.env.t('newPassSent'));
|
2014-01-12 23:00:47 +00:00
|
|
|
})
|
|
|
|
|
.error(function(data){
|
|
|
|
|
alert(data.err);
|
|
|
|
|
});
|
2014-08-09 09:57:17 +00:00
|
|
|
};
|
2014-04-17 00:20:30 +00:00
|
|
|
|
|
|
|
|
$scope.expandMenu = function(menu) {
|
|
|
|
|
$scope._expandedMenu = ($scope._expandedMenu == menu) ? null : menu;
|
2014-08-09 09:57:17 +00:00
|
|
|
};
|
2014-10-04 09:56:56 +00:00
|
|
|
|
2014-10-04 11:57:30 +00:00
|
|
|
function selectNotificationValue(mysteryValue, invitationValue, unallocatedValue, messageValue, noneValue) {
|
2014-10-04 09:56:56 +00:00
|
|
|
var user = $scope.user;
|
|
|
|
|
if (user.purchased.plan.mysteryItems.length) {
|
|
|
|
|
return mysteryValue;
|
|
|
|
|
} else if ((user.invitations.party && user.invitations.party.id) || user.invitations.guilds.length > 0) {
|
|
|
|
|
return invitationValue;
|
2014-10-04 11:57:30 +00:00
|
|
|
} else if (user.flags.classSelected && !(user.preferences && user.preferences.disableClasses) && user.stats.points) {
|
|
|
|
|
return unallocatedValue;
|
2014-10-04 09:56:56 +00:00
|
|
|
} else if (!(_.isEmpty(user.newMessages))) {
|
|
|
|
|
return messageValue;
|
|
|
|
|
} else {
|
|
|
|
|
return noneValue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.iconClasses = function() {
|
|
|
|
|
return selectNotificationValue(
|
|
|
|
|
"glyphicon-gift",
|
|
|
|
|
"glyphicon-user",
|
2014-10-04 11:57:30 +00:00
|
|
|
"glyphicon-plus-sign",
|
2014-10-04 09:56:56 +00:00
|
|
|
"glyphicon-comment",
|
|
|
|
|
"glyphicon-comment inactive");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.hasNoNotifications = function() {
|
2014-10-04 11:57:30 +00:00
|
|
|
return selectNotificationValue(false, false, false, false, true);
|
2014-10-04 09:56:56 +00:00
|
|
|
}
|
2013-09-06 13:33:23 +00:00
|
|
|
}
|
2013-09-06 02:33:31 +00:00
|
|
|
]);
|