Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Last active December 24, 2015 22:09
Show Gist options
  • Save thebigredgeek/6871242 to your computer and use it in GitHub Desktop.
Save thebigredgeek/6871242 to your computer and use it in GitHub Desktop.
$rootScope.$safeApply = function(fn) {
var phase;
phase = this.$root.$$phase;
if (phase === '$apply' || phase === '$digest') {
if (fn) {
fn();
}
} else {
this.$apply(fn);
}
return true;
};
$rootScope.$once = function(event, fn) {
var killListener, self;
self = this;
killListener = self.$on(event, function(event, parameters) {
self.$safeApply(function() {
fn(event, parameters);
killListener();
return true;
});
return true;
});
return killListener;
};
angular.module("angularQuiz").controller("RegisterModalController", RegisterModalController = (function() {
function RegisterModalController($scope, $modalInstance, FBase, FBaseErrCode) {
this.register = __bind(this.register, this);
this.close = __bind(this.close, this);
this.failRegister = __bind(this.failRegister, this);
this.successRegister = __bind(this.successRegister, this);
this.$scope = $scope;
this.$modalInstance = $modalInstance;
this.FBase = FBase;
this.FBaseErrCode = FBaseErrCode;
this.$scope.close = this.close;
this.$scope.register = this.register;
this.$scope.data = {};
this.$scope.data.toggle = false;
this.$scope.data.error = null;
}
RegisterModalController.prototype.successRegister = function(email) {
console.log("Successful Registration");
return this.$modalInstance.close({
email: this.$scope.data.email
});
};
RegisterModalController.prototype.failRegister = function(reason) {
console.log("Failed Registration");
this.$scope.data.error = this.FBaseErrCode.parse(reason.code);
return this.$scope.data.toggle = false;
};
RegisterModalController.prototype.close = function() {
this.$modalInstance.dismiss("user");
return true;
};
RegisterModalController.prototype.register = function(email, password) {
this.$scope.data.toggle = true;
return this.FBase.register(email, password).then(this.successRegister, this.failRegister)["catch"](console.error);
};
return RegisterModalController;
})());
angular.module("angularQuiz").service("FBase", FBase = (function() {
var _auth, _instance, _q, _rootScope, _token, _url;
_url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
_instance = null;
_auth = null;
_rootScope = null;
_q = null;
function FBase(Firebase, angularFireAuth, $rootScope, $q) {
_auth = angularFireAuth;
_rootScope = $rootScope;
_q = $q;
_instance = new Firebase(_url);
this.wake();
true;
}
FBase.prototype.wake = function() {
_instance.auth(_token, function(error, user) {
_auth.initialize(_instance, {
scope: _rootScope,
name: 'user'
});
return true;
});
return true;
};
FBase.prototype.root = function() {
return _instance;
};
FBase.prototype.login = function(email, password) {
var deferral, killFailureListener, killSuccessListener;
deferral = _q.defer();
killSuccessListener = null;
killFailureListener = null;
killSuccessListener = _rootScope.$once("angularFireAuth:login", function(event, user) {
killFailureListener();
deferral.resolve(user);
return true;
});
killFailureListener = _rootScope.$once("angularFireAuth:error", function(event, err) {
killSuccessListener();
deferral.reject(err);
return true;
});
_auth.login("password", {
email: email,
password: password
});
return deferral.promise;
};
FBase.prototype.register = function(email, password) {
var deferral, killFailureListener, killSuccessListener;
deferral = _q.defer();
killSuccessListener = false;
killFailureListener = false;
killSuccessListener = _rootScope.$once("angularFireAuth:login", function(event, user) {
console.log("successful login");
killFailureListener();
deferral.resolve(user);
return true;
});
killFailureListener = _rootScope.$once("angularFireAuth:error", function(event, err) {
console.log("failed login");
killSuccessListener();
deferral.reject(err);
return true;
});
console.log(email, password);
_auth.createUser(email, password);
return deferral.promise;
};
FBase.prototype.logout = function() {
return _auth.logout;
};
return FBase;
})());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment