Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var fs = require('fs');
var argv = require('yargs').argv;
var streamToPromise = require('stream-to-promise');
var path = require('path');
var os = require('os');

Expand Down Expand Up @@ -36,6 +35,16 @@ function bundle () {
.pipe(plugins.footer(fs.readFileSync('./helpers/globals.js')));
}

var bundlePath;
gulp.task('bundle-smoke', function () {
var name = Date.now() + '-mockfirebase.js';
var dir = os.tmpdir();
bundlePath = path.join(dir, name);
return bundle()
.pipe(plugins.rename(name))
.pipe(gulp.dest(dir));
});

gulp.task('bundle', function () {
return bundle().pipe(gulp.dest('./browser'));
});
Expand Down Expand Up @@ -76,28 +85,20 @@ gulp.task('karma', function () {
});
});

gulp.task('smoke', function () {
var name = Date.now() + '-mockfirebase.js';
var dir = os.tmpdir();
var bundlePath = path.join(dir, name);
return streamToPromise(bundle()
.pipe(plugins.rename(name))
.pipe(gulp.dest(dir)))
.then(function () {
return require('karma-as-promised').server.start({
frameworks: ['mocha', 'chai'],
browsers: ['PhantomJS'],
client: {
args: ['--grep', argv.grep]
},
files: [
bundlePath,
'test/smoke/globals.js'
],
autoWatch: false,
singleRun: true
});
});
gulp.task('smoke', ['bundle-smoke'], function () {
return require('karma-as-promised').server.start({
frameworks: ['mocha', 'chai'],
browsers: ['PhantomJS'],
client: {
args: ['--grep', argv.grep]
},
files: [
bundlePath,
'test/smoke/globals.js'
],
autoWatch: false,
singleRun: true
});
});

gulp.task('lint', function () {
Expand Down
33 changes: 33 additions & 0 deletions helpers/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,50 @@
window.MockFirebase = window.mockfirebase.MockFirebase;
window.MockFirebaseSimpleLogin = window.mockfirebase.MockFirebaseSimpleLogin;

window.mockfirebase.MockFirebaseSdk = {
database: function() {
return {
ref: function(path) {
return new window.mockfirebase.MockFirebase(path);
},
refFromURL: function(url) {
return new window.mockfirebase.MockFirebase(url);
}
};
},
auth: function() {
var auth = new window.mockfirebase.MockFirebase();
delete auth.ref;
return auth;
}
};
window.mockfirebase.MockFirebaseSdk.auth.GoogleAuthProvider = function() {
this.providerId = "google.com";
};
window.mockfirebase.MockFirebaseSdk.auth.TwitterAuthProvider = function() {
this.providerId = "twitter.com";
};
window.mockfirebase.MockFirebaseSdk.auth.FacebookAuthProvider = function() {
this.providerId = "facebook.com";
};
window.mockfirebase.MockFirebaseSdk.auth.GithubAuthProvider = function() {
this.providerId = "github.com";
};

var originals = false;
window.MockFirebase.override = function () {
originals = {
firebasesdk: window.firebase,
firebase: window.Firebase,
login: window.FirebaseSimpleLogin
};
window.firebase = window.mockfirebase.MockFirebaseSdk;
window.Firebase = window.mockfirebase.MockFirebase;
window.FirebaseSimpleLogin = window.mockfirebase.MockFirebaseSimpleLogin;
};
window.MockFirebase.restore = function () {
if (!originals) return;
window.firebase = originals.firebasesdk;
window.Firebase = originals.firebase;
window.FirebaseSimpleLogin = originals.login;
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"browserify": "^6.3.2",
"browserify-shim": "^3.8.0",
"firebase-auto-ids": "~1.1.0",
"lodash": "~2.4.1"
"lodash": "~2.4.1",
"rsvp": "^3.2.1"
},
"devDependencies": {
"chai": "~1.9.1",
Expand All @@ -55,7 +56,6 @@
"semver": "~3.0.1",
"sinon": "~1.10.3",
"sinon-chai": "~2.5.0",
"stream-to-promise": "~1.0.4",
"vinyl-buffer": "1.0.0",
"vinyl-source-stream": "^1.0.0",
"yargs": "~1.3.1"
Expand Down
123 changes: 111 additions & 12 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

var _ = require('lodash');
var format = require('util').format;
var rsvp = require('rsvp');

function FirebaseAuth () {
this.currentUser = null;
this._auth = {
userData: null,
listeners: [],
completionListeners: [],
users: [],
Expand All @@ -15,13 +16,34 @@ function FirebaseAuth () {

FirebaseAuth.prototype.changeAuthState = function (userData) {
this._defer('changeAuthState', _.toArray(arguments), function() {
if (!_.isEqual(this._auth.userData, userData)) {
this._auth.userData = _.isObject(userData) ? userData : null;
if (!_.isEqual(this.currentUser, userData)) {
this.currentUser = _.isObject(userData) ? userData : null;
this._triggerAuthEvent();
}
});
};

FirebaseAuth.prototype.onAuthStateChanged = function (callback) {
var self = this;
var currentUser = this.currentUser;
this._auth.listeners.push({fn: callback});

defer();
return destroy;

function destroy() {
self.offAuth(callback);
}

function defer() {
self._defer('onAuthStateChanged', _.toArray(arguments), function() {
if (!_.isEqual(self.currentUser, currentUser)) {
self._triggerAuthEvent();
}
});
}
};

FirebaseAuth.prototype.getEmailUser = function (email) {
var users = this._auth.users;
return users.hasOwnProperty(email) ? _.clone(users[email]) : null;
Expand All @@ -46,12 +68,66 @@ Object.keys(authMethods)
};
});

var signinMethods = {
signInWithCustomToken: function(authToken) {
return {
isAnonymous: false
};
},
signInAnonymously: function() {
return {
isAnonymous: true
};
},
signInWithEmailAndPassword: function(email, password) {
return {
isAnonymous: false,
email: email
};
},
signInWithPopup: function(provider) {
return {
isAnonymous: false,
providerData: [provider]
};
},
signInWithRedirect: function(provider) {
return {
isAnonymous: false,
providerData: [provider]
};
},
signInWithCredential: function(credential) {
return {
isAnonymous: false
};
}
};

Object.keys(signinMethods)
.forEach(function (method) {
var getUser = signinMethods[method];
FirebaseAuth.prototype[method] = function () {
var self = this;
var user = getUser.apply(this, arguments);
var promise = new rsvp.Promise(function(resolve, reject) {
self._authEvent(method, function(err) {
if (err) reject(err);
self.currentUser = user;
resolve(user);
self._triggerAuthEvent();
}, true);
});
return promise;
};
});

FirebaseAuth.prototype.auth = function (token, callback) {
console.warn('FIREBASE WARNING: FirebaseRef.auth() being deprecated. Please use FirebaseRef.authWithCustomToken() instead.');
this._authEvent('auth', callback);
};

FirebaseAuth.prototype._authEvent = function (method, callback) {
FirebaseAuth.prototype._authEvent = function (method, callback, defercallback) {
var err = this._nextErr(method);
if (!callback) return;
if (err) {
Expand All @@ -62,26 +138,33 @@ FirebaseAuth.prototype._authEvent = function (method, callback) {
});
}
else {
// if there is no error, then we just add our callback to the listener
// stack and wait for the next changeAuthState() call.
this._auth.completionListeners.push({fn: callback});
if (defercallback) {
this._defer(method, _.toArray(arguments), function() {
callback();
});
} else {
// if there is no error, then we just add our callback to the listener
// stack and wait for the next changeAuthState() call.
this._auth.completionListeners.push({fn: callback});
}
}
};

FirebaseAuth.prototype._triggerAuthEvent = function () {
var completionListeners = this._auth.completionListeners;
this._auth.completionListeners = [];
var user = this._auth.userData;
var user = this.currentUser;
completionListeners.forEach(function (parts) {
parts.fn.call(parts.context, null, _.cloneDeep(user));
});
this._auth.listeners.forEach(function (parts) {
var listeners = _.cloneDeep(this._auth.listeners);
listeners.forEach(function (parts) {
parts.fn.call(parts.context, _.cloneDeep(user));
});
};

FirebaseAuth.prototype.getAuth = function () {
return this._auth.userData;
return this.currentUser;
};

FirebaseAuth.prototype.onAuth = function (onComplete, context) {
Expand All @@ -102,12 +185,28 @@ FirebaseAuth.prototype.offAuth = function (onComplete, context) {
};

FirebaseAuth.prototype.unauth = function () {
if (this._auth.userData !== null) {
this._auth.userData = null;
if (this.currentUser !== null) {
this.currentUser = null;
this._triggerAuthEvent();
}
};

FirebaseAuth.prototype.signOut = function () {
var self = this, updateuser = this.currentUser !== null;
var promise = new rsvp.Promise(function(resolve, reject) {
self._authEvent('signOut', function(err) {
if (err) reject(err);
self.currentUser = null;
resolve();

if (updateuser) {
self._triggerAuthEvent();
}
}, true);
});
return promise;
};

FirebaseAuth.prototype.createUser = function (credentials, onComplete) {
validateCredentials('createUser', credentials, [
'email',
Expand Down
9 changes: 3 additions & 6 deletions src/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Auth = require('./auth');
var validate = require('./validators');

function MockFirebase (path, data, parent, name) {
this.ref = this;
this.path = path || 'Mock://';
this.errs = {};
this.priority = null;
Expand Down Expand Up @@ -198,10 +199,6 @@ MockFirebase.prototype.name = function () {
return this.key.apply(this, arguments);
};

MockFirebase.prototype.ref = function () {
return this;
};

MockFirebase.prototype.parent = function () {
return this.parentRef;
};
Expand Down Expand Up @@ -299,7 +296,7 @@ MockFirebase.prototype.off = function (event, callback, context) {
});
}
else {
this._events[event] = [];
this._events[event] = [];
}
}
};
Expand Down Expand Up @@ -376,7 +373,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
keysToChange.forEach(function(key) {
var childData = unparsedData[key];
if (utils.isServerTimestamp(childData)) {
childData = getServerTime();
childData = getServerTime();
}
this._updateOrAdd(key, childData, events);
}, this);
Expand Down
Loading