Skip to content

Instantly share code, notes, and snippets.

View mitrakmt's full-sized avatar

Michael Mitrakos mitrakmt

View GitHub Profile
<!doctype html>
<html>
<html lang=“en”>
<head>
<meta charset="UTF-8">
<title>About</title>
<!-- [if lt IE 9]>
<script src="dist/html5shiv.js"></script>
<![endif]-->
@mitrakmt
mitrakmt / firebase_promise_wrapper.js
Last active September 2, 2015 04:38 — forked from katowulf/firebase_promise_wrapper.js
Example of promise contracts for Firebase (using jQuery.Deferred)
/*
* Promise wrapper for Firebase
*
* Requires jQuery and underscore.js
*************************************/
(function ($) {
"use strict";
var undefined;
var FIREBASE_URL = 'https://YOURINSTANCE.firebaseio.com';
@mitrakmt
mitrakmt / firebase_detect_data.js
Last active September 2, 2015 04:39 — forked from anantn/firebase_detect_data.js
Firebase: Detecting if data exists. This snippet detects if a user ID is already taken
function go() {
var userId = prompt('Username?', 'Guest');
checkIfUserExists(userId);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userExistsCallback(userId, exists) {
if (exists) {
alert('user ' + userId + ' exists!');
@mitrakmt
mitrakmt / using_an_index.js
Last active September 2, 2015 04:39 — forked from katowulf/1_using_queries.js
Methods to search for user accounts by email address in Firebase
/***************************************************
* Assuming you can't use priorities (e.g. they are already being used for something else)
* You can store the email addresses in an index and use that to match them to user ids
***************************************************/
var fb = new Firebase(URL);
/**
* Looks up a user id by email address and invokes callback with the id or null if not found
* @return {Object|null} the object contains the key/value hash for one user
@mitrakmt
mitrakmt / data-structure.js
Last active September 2, 2015 04:40 — forked from sararob/data-structure.js
Role-based security in Firebase
/*
This example shows how you can use your data structure as a basis for
your Firebase security rules to implement role-based security. We store
each user by their Twitter uid, and use the following simplistic approach
for user roles:
0 - GUEST
10 - USER
20 - MODERATOR
@mitrakmt
mitrakmt / firebase_first_item.js
Last active September 2, 2015 04:40 — forked from anantn/firebase_first_item.js
Firebase: Get the first item in a list. This snippet retrieves only the first item in a list.
function makeList(ref) {
var fruits = ["banana", "apple", "grape", "orange"];
for (var i = 0; i < fruits.length; i++) {
ref.push(fruits[i]);
}
}
function getFirstFromList(ref, cb) {
ref.startAt().limit(1).once("child_added", function(snapshot) {
cb(snapshot.val());
@mitrakmt
mitrakmt / 1_query_timestamp.js
Last active September 2, 2015 04:40 — forked from katowulf/1_query_timestamp.js
Get only new items from Firebase
// assumes you add a timestamp field to each record (see Firebase.ServerValue.TIMESTAMP)
// pros: fast and done server-side (less bandwidth, faster response), simple
// cons: a few bytes on each record for the timestamp
var ref = new Firebase(...);
ref.orderByChild('timestamp').startAt(Date.now()).on('child_added', function(snapshot) {
console.log('new record', snap.key());
});
@mitrakmt
mitrakmt / jwtparser.js
Last active September 2, 2015 04:41 — forked from katowulf/jwtparser.js
Deconstruct a JWT token for Firebase
// Helper function to extract claims from a JWT. Does *not* verify the
// validity of the token.
// credits: https://github.com/firebase/angularFire/blob/master/angularFire.js#L370
// polyfill window.atob() for IE8: https://github.com/davidchambers/Base64.js
// or really fast Base64 by Fred Palmer: https://code.google.com/p/javascriptbase64/
function deconstructJWT(token) {
var segments = token.split(".");
if (!segments instanceof Array || segments.length !== 3) {
throw new Error("Invalid JWT");
}
@mitrakmt
mitrakmt / firebase_create.js
Last active September 2, 2015 04:41 — forked from anantn/firebase_create.js
Firebase: Creating data if it doesn't exist. This snippet creates a user only if it doesn't already exist.
function go() {
var userId = prompt('Username?', 'Guest');
var userData = { name: userId };
tryCreateUser(userId, userData);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userCreated(userId, success) {
if (!success) {
@mitrakmt
mitrakmt / firebase_player_assignment.js
Last active September 2, 2015 04:41 — forked from anantn/firebase_player_assignment.js
Firebase: Assigning players in a multiplayer game. This snippet assigns you a spot in a game if the game isn't full yet.
function go() {
var userId = prompt('Username?', 'Guest');
// Consider adding '/<unique id>' if you have multiple games.
var gameRef = new Firebase(GAME_LOCATION);
assignPlayerNumberAndPlayGame(userId, gameRef);
};
// The maximum number of players. If there are already
// NUM_PLAYERS assigned, users won't be able to join the game.
var NUM_PLAYERS = 4;