This commit is contained in:
Radek Stepan 2014-09-04 21:23:59 -07:00
parent 450e7f04fb
commit f8c30b8754
4 changed files with 127 additions and 29 deletions

95
NOTES.md Normal file
View File

@ -0,0 +1,95 @@
#Notes
##Write
Access a child from a root (db) reference:
rootRef.child('users/mchen/name');
Save a new user to the db:
var usersRef = ref.child("users");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
Flatten all data otherwise we are retrieving all children.
Check if we have a member of a group which could be used to check if we have a GitHub user stored in the db:
// see if Mary is in the 'alpha' group
var ref = new Firebase("https://docs-examples.firebaseio.com/web/org/users/mchen/groups/alpha");
ref.once('value', function(snap) {
var result = snap.val() === null? 'is not' : 'is';
console.log('Mary ' + result + ' a member of alpha group');
});
##Read
The following should get triggered everytime we add a new repo to our list, updating our local (Ractive) ref. It gets called for every existing member too.
// Get a reference to our posts
var postsRef = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Retrieve new posts as they are added to Firebase
postsRef.on('child_added', function (snapshot) {
var newPost = snapshot.val();
console.log("Author: " + newPost.author);
console.log("Title: " + newPost.title);
});
Changes can be monitored like the following. This should work even in offline mode so we should not be changing our local state but Firebase state which calls us back with the changes.
// Get a reference to our posts
var postsRef = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Get the data on a post that has changed
postsRef.on('child_changed', function (snapshot) {
var changedPost = snapshot.val();
console.log('The updated post title is ' + changedPost.title);
});
When we remove a repo:
// Get a reference to our posts
var postsRef = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Get the data on a post that has been removed
postsRef.on('child_removed', function (snapshot) {
var deletedPost = snapshot.val();
console.log('The blog post titled' + deletedPost.title + ' has been deleted');
});
##Security
Write new users, do not update them, but allow delete.
// we can write as long as old data or new data does not exist
// in other words, if this is a delete or a create, but not an update
".write": "!data.exists() || !newData.exists()"
Accessing dynamic paths in the rules can be done using a `$` prefix. This serves as a wild card, and stores the value of that key for use inside the rules declarations:
{
"rules": {
"rooms": {
// this rule applies to any child of /rooms/, the key for each room id
// is stored inside $room_id variable for reference
"$room_id": {
"topic": {
// the room's topic can be changed if the room id has "public" in it
".write": "$room_id.contains('public')"
}
}
}
}
}
[User-based rules](https://www.firebase.com/docs/web/guide/user-security.html).

View File

@ -22450,37 +22450,38 @@ if (typeof exports !== 'undefined') {
// firebase.coffee // firebase.coffee
root.require.register('burnchart/src/modules/firebase.js', function(exports, require, module) { root.require.register('burnchart/src/modules/firebase.js', function(exports, require, module) {
var FB, authCb, config, user; var Class, config, user;
config = require('../models/config'); config = require('../models/config');
user = require('./user'); user = require('./user');
authCb = function() {}; Class = (function() {
function Class() {
FB = (function() { var _this = this;
function FB() {
this.client = new Firebase("https://" + config.firebase + ".firebaseio.com"); this.client = new Firebase("https://" + config.firebase + ".firebaseio.com");
this.auth = new FirebaseSimpleLogin(this.client, function(err, obj) { this.auth = new FirebaseSimpleLogin(this.client, function(err, obj) {
if (err || !obj) { if (err || !obj) {
return authCb(err); return _this.authCb(err);
} }
return user.set(obj); return user.set(obj);
}); });
} }
FB.prototype.login = function(cb) { Class.prototype.authCb = function() {};
Class.prototype.login = function(cb) {
if (!this.client) { if (!this.client) {
return cb('Client is not setup'); return cb('Client is not setup');
} }
authCb = cb; this.authCb = cb;
return this.auth.login(config.provider, { return this.auth.login(config.provider, {
'rememberMe': true, 'rememberMe': true,
'scope': 'public_repo' 'scope': 'public_repo'
}); });
}; };
FB.prototype.logout = function() { Class.prototype.logout = function() {
var _ref; var _ref;
if ((_ref = this.auth) != null) { if ((_ref = this.auth) != null) {
_ref.logout; _ref.logout;
@ -22488,11 +22489,11 @@ if (typeof exports !== 'undefined') {
return user.reset(); return user.reset();
}; };
return FB; return Class;
})(); })();
module.exports = new FB(); module.exports = new Class();
}); });

View File

@ -129,37 +129,38 @@
// firebase.coffee // firebase.coffee
root.require.register('burnchart/src/modules/firebase.js', function(exports, require, module) { root.require.register('burnchart/src/modules/firebase.js', function(exports, require, module) {
var FB, authCb, config, user; var Class, config, user;
config = require('../models/config'); config = require('../models/config');
user = require('./user'); user = require('./user');
authCb = function() {}; Class = (function() {
function Class() {
FB = (function() { var _this = this;
function FB() {
this.client = new Firebase("https://" + config.firebase + ".firebaseio.com"); this.client = new Firebase("https://" + config.firebase + ".firebaseio.com");
this.auth = new FirebaseSimpleLogin(this.client, function(err, obj) { this.auth = new FirebaseSimpleLogin(this.client, function(err, obj) {
if (err || !obj) { if (err || !obj) {
return authCb(err); return _this.authCb(err);
} }
return user.set(obj); return user.set(obj);
}); });
} }
FB.prototype.login = function(cb) { Class.prototype.authCb = function() {};
Class.prototype.login = function(cb) {
if (!this.client) { if (!this.client) {
return cb('Client is not setup'); return cb('Client is not setup');
} }
authCb = cb; this.authCb = cb;
return this.auth.login(config.provider, { return this.auth.login(config.provider, {
'rememberMe': true, 'rememberMe': true,
'scope': 'public_repo' 'scope': 'public_repo'
}); });
}; };
FB.prototype.logout = function() { Class.prototype.logout = function() {
var _ref; var _ref;
if ((_ref = this.auth) != null) { if ((_ref = this.auth) != null) {
_ref.logout; _ref.logout;
@ -167,11 +168,11 @@
return user.reset(); return user.reset();
}; };
return FB; return Class;
})(); })();
module.exports = new FB(); module.exports = new Class();
}); });

View File

@ -2,27 +2,28 @@ config = require '../models/config'
user = require './user' user = require './user'
# Default "silent" callback for auth. # Default "silent" callback for auth.
authCb = -> class Class
class FB
constructor: -> constructor: ->
# Setup a new client. # Setup a new client.
@client = new Firebase "https://#{config.firebase}.firebaseio.com" @client = new Firebase "https://#{config.firebase}.firebaseio.com"
# Check if we have a user in session. # Check if we have a user in session.
@auth = new FirebaseSimpleLogin @client, (err, obj) -> @auth = new FirebaseSimpleLogin @client, (err, obj) =>
return authCb err if err or not obj return @authCb err if err or not obj
# Save user. # Save user.
user.set obj user.set obj
# Default "blank" callback.
authCb: ->
# Login a user. # Login a user.
login: (cb) -> login: (cb) ->
return cb 'Client is not setup' unless @client return cb 'Client is not setup' unless @client
# Override the default auth callback. # Override the default auth callback.
authCb = cb @authCb = cb
# Login. # Login.
@auth.login config.provider, @auth.login config.provider,
@ -34,4 +35,4 @@ class FB
@auth?.logout @auth?.logout
do user.reset do user.reset
module.exports = new FB() module.exports = new Class()