Files
habitica/public/js/controllers/authCtrl.js
T
Andrew Bloomgarden 77801e458e Change static module name to habitrpgStatic.
The way the tests are written now, the habitrpg module definition in static.js
was clobbering the one in app.js. In other words, most of the tests weren't
testing what they thought they were testing.
2014-01-15 21:33:57 -08:00

98 lines
3.0 KiB
JavaScript

"use strict";
/*
The authentication controller (login & facebook)
*/
angular.module('authCtrl', [])
.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$location', '$window','API_URL',
function($scope, $rootScope, User, $http, $location, $window, 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;
}
$scope.useUUID = !$scope.useUUID;
};
$scope.logout = function() {
localStorage.clear();
window.location.href = '/logout';
};
runAuth = function(id, token) {
User.authenticate(id, token, function(err) {
$window.location.href = '/';
//$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;
}
$http.post(API_URL + "/api/v2/register", $scope.registerVals).success(function(data, status, headers, config) {
runAuth(data.id, data.apiToken);
}).error(function(data, status, headers, config) {
if (status === 0) {
$window.alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
$window.alert(data.err);
} else {
$window.alert("ERROR: " + status);
}
});
};
function errorAlert(data, status, headers, config) {
if (status === 0) {
$window.alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
$window.alert(data.err);
} else {
$window.alert("ERROR: " + status);
}
}
$scope.auth = function() {
var data = {
username: $scope.loginUsername,
password: $scope.loginPassword
};
if ($scope.useUUID) {
runAuth($scope.loginUsername, $scope.loginPassword);
} else {
$http.post(API_URL + "/api/v2/user/auth/local", data)
.success(function(data, status, headers, config) {
runAuth(data.id, data.token);
}).error(errorAlert);
}
};
$scope.playButtonClick = function(){
if (User.authenticated()) {
window.location.href = '/#/tasks';
} else {
$('#login-modal').modal('show');
}
}
$scope.passwordReset = function(email){
$http.post(API_URL + '/api/v2/user/reset-password', {email:email})
.success(function(){
alert('New password sent.');
})
.error(function(data){
alert(data.err);
});
}
}
]);