#1398 some more hackery to handling facebook auth. I really don't like

this client-side return approach, but it's probably the only thing we
 can do sans a stateful server, which we're trying to avoi
This commit is contained in:
Tyler Renelle 2013-09-05 22:33:31 -04:00
parent 512e891dc3
commit 336c147dcb
4 changed files with 144 additions and 142 deletions

View file

@ -6,93 +6,81 @@
habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$location', 'API_URL',
function($scope, $rootScope, User, $http, $location, API_URL) {
var runAuth;
var showedFacebookMessage;
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/v1/register", $scope.registerVals).success(function(data, status, headers, config) {
runAuth(data.id, data.apiToken);
}).error(function(data, status, headers, config) {
if (status === 0) {
alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
alert(data.err);
} else {
alert("ERROR: " + status);
}
});
};
function errorAlert(data, status, headers, config) {
if (status === 0) {
alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
alert(data.err);
} else {
alert("ERROR: " + status);
}
}
$scope.auth = function() {
var data = {
username: $scope.loginUsername,
password: $scope.loginPassword
$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;
};
if ($scope.useUUID) {
runAuth($scope.loginUsername, $scope.loginPassword);
} else {
$http.post(API_URL + "/api/v1/user/auth/local", data)
.success(function(data, status, headers, config) {
runAuth(data.id, data.token);
}).error(errorAlert);
$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/v1/register", $scope.registerVals).success(function(data, status, headers, config) {
runAuth(data.id, data.apiToken);
}).error(function(data, status, headers, config) {
if (status === 0) {
alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
alert(data.err);
} else {
alert("ERROR: " + status);
}
});
};
function errorAlert(data, status, headers, config) {
if (status === 0) {
alert("Server not currently reachable, try again later");
} else if (!!data && !!data.err) {
alert(data.err);
} else {
alert("ERROR: " + status);
}
}
};
$scope.facebookAuth = function(){
$http.get('/auth/facebook')
.success(function(data){
runAuth(data.id, data.token);
}).error(errorAlert);
}
$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/v1/user/auth/local", data)
.success(function(data, status, headers, config) {
runAuth(data.id, data.token);
}).error(errorAlert);
}
};
if ($location.path() == '/facebook-callback') {
var search = $location.search();
if (search.err) return alert(search.err);
runAuth(search._id, search.apiToken);
}
$scope.playButtonClick = function(){
if (User.authenticated()) {
window.location.href = '/#/tasks';
} else {
$('#login-modal').modal('show');
$scope.playButtonClick = function(){
if (User.authenticated()) {
window.location.href = '/#/tasks';
} else {
$('#login-modal').modal('show');
}
}
}
}]);
]);

View file

@ -5,61 +5,62 @@
habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http',
function($scope, $rootScope, $location, User, $http) {
$rootScope.modals = {};
$rootScope.User = User;
$rootScope.user = User.user;
$rootScope.settings = User.settings;
$rootScope.flash = {errors: [], warnings: []};
$rootScope.modals = {};
$rootScope.User = User;
$rootScope.user = User.user;
$rootScope.settings = User.settings;
$rootScope.flash = {errors: [], warnings: []};
$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
/*
FIXME this is dangerous, organize helpers.coffee better, so we can group them by which controller needs them,
and then simply _.defaults($scope, Helpers.user) kinda thing
*/
_.defaults($rootScope, window.habitrpgShared.algos);
_.defaults($rootScope, window.habitrpgShared.helpers);
$rootScope.set = User.set;
$rootScope.authenticated = User.authenticated;
$rootScope.dismissAlert = function() {
$rootScope.modals.newStuff = false;
$rootScope.set('flags.newStuff',false);
}
$rootScope.notPorted = function(){
alert("This feature is not yet ported from the original site.");
}
$rootScope.showStripe = function() {
var disableAds = User.user.flags.ads == 'hide' ? '' : 'Disable Ads, ';
StripeCheckout.open({
key: window.env.STRIPE_PUB_KEY,
address: false,
amount: 500,
name: "Checkout",
description: "Buy 20 Gems, " + disableAds + "Support the Developers",
panelLabel: "Checkout",
token: function(data) {
$scope.$apply(function(){
$http.post("/api/v1/user/buy-gems", data)
.success(function() {
window.location.href = "/";
}).error(function(err) {
alert(err);
});
})
$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
});
} else {
this.$apply(fn);
}
};
/*
FIXME this is dangerous, organize helpers.coffee better, so we can group them by which controller needs them,
and then simply _.defaults($scope, Helpers.user) kinda thing
*/
_.defaults($rootScope, window.habitrpgShared.algos);
_.defaults($rootScope, window.habitrpgShared.helpers);
$rootScope.set = User.set;
$rootScope.authenticated = User.authenticated;
$rootScope.dismissAlert = function() {
$rootScope.modals.newStuff = false;
$rootScope.set('flags.newStuff',false);
}
$rootScope.notPorted = function(){
alert("This feature is not yet ported from the original site.");
}
$rootScope.showStripe = function() {
var disableAds = User.user.flags.ads == 'hide' ? '' : 'Disable Ads, ';
StripeCheckout.open({
key: window.env.STRIPE_PUB_KEY,
address: false,
amount: 500,
name: "Checkout",
description: "Buy 20 Gems, " + disableAds + "Support the Developers",
panelLabel: "Checkout",
token: function(data) {
$scope.$apply(function(){
$http.post("/api/v1/user/buy-gems", data)
.success(function() {
window.location.href = "/";
}).error(function(err) {
alert(err);
});
})
}
});
}
}
}]);
]);

View file

@ -169,7 +169,7 @@ angular.module('userServices', []).
//if settings were saved while fetch was in process reset the flag.
settings.fetching = false;
//create and load if not
//create and load if not
} else {
localStorage.setItem(STORAGE_SETTINGS_ID, JSON.stringify(defaultSettings));
_.extend(settings, defaultSettings);
@ -177,7 +177,19 @@ angular.module('userServices', []).
//If user does not have ApiID that forward him to settings.
if (!settings.auth.apiId || !settings.auth.apiToken) {
//$location.path("/login");
//var search = $location.search(); // FIXME this should be working, but it's returning an empty object when at a root url /?_id=...
var search = $location.search(window.location.search.substring(1)).$$search; // so we use this fugly hack instead
if (search.err) return alert(search.err);
if (search._id && search.apiToken) {
userServices.authenticate(search._id, search.apiToken, function(){
window.location.href='/';
});
} else {
if (window.location.pathname.indexOf('/static') !== 0){
localStorage.clear();
window.location.href = '/logout';
}
}
} else {
userServices.authenticate(settings.auth.apiId, settings.auth.apiToken)
}

View file

@ -208,9 +208,10 @@ api.setupPassport = function(router) {
User.findOne({'auth.facebook.id':req.user.id}, function(err, user){
if (err || !user) {
if (!err) err = "New Facebook registrations aren't yet supported, only existing Facebook users. Help us code this!";
return res.redirect('/#/facebook-callback?err=' + err);
return res.redirect('/static/front?err=' + err);
}
res.redirect('/#/facebook-callback?_id='+user._id+'&apiToken='+user.apiToken);
req.session.userId = user._id;
res.redirect('/static/front?_id='+user._id+'&apiToken='+user.apiToken);
})
});