habitica-self-host/assets/js/controllers/authCtrl.js

69 lines
2.2 KiB
JavaScript
Raw Normal View History

"use strict";
/*
The authentication controller (login & facebook)
*/
habitrpg.controller("AuthCtrl", function($scope, $rootScope, Facebook, LocalAuth, User, $http, $location, API_URL) {
var runAuth;
var showedFacebookMessage;
$scope.useUUID = false;
$scope.toggleUUID = function() {
if (showedFacebookMessage === false) {
alert("Until we add Facebook, use your UUID and API Token to log in (found at https://habitrpg.com > Options > Settings).");
showedFacebookMessage = true;
}
return $scope.useUUID = !$scope.useUUID;
};
$scope.logout = function() {
localStorage.clear();
return location.reload();
};
runAuth = function(id, token) {
return User.authenticate(id, token, function(err) {
return $rootScope.modals.login = false;
});
};
$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;
}
return $http.post(API_URL + "/api/v1/register", $scope.registerVals).success(function(data, status, headers, config) {
return runAuth(data.id, data.apiToken);
}).error(function(data, status, headers, config) {
if (status === 0) {
return alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
return alert(data.err);
} else {
return alert("ERROR: " + status);
}
});
};
return $scope.auth = function() {
var data;
data = {
username: $scope.loginUsername,
password: $scope.loginPassword
};
if ($scope.useUUID) {
return runAuth($scope.loginUsername, $scope.loginPassword);
} else {
return $http.post(API_URL + "/api/v1/user/auth/local", data).success(function(data, status, headers, config) {
return runAuth(data.id, data.token);
}).error(function(data, status, headers, config) {
if (status === 0) {
return alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
return alert(data.err);
} else {
return alert("ERROR: " + status);
}
});
}
};
});