Skip to content

Instantly share code, notes, and snippets.

@ashnur
Created October 27, 2012 20:42
Show Gist options
  • Select an option

  • Save ashnur/3966146 to your computer and use it in GitHub Desktop.

Select an option

Save ashnur/3966146 to your computer and use it in GitHub Desktop.
client side script
void function(root){
"use strict"
var bean = require('bean')
, sizzle = require('sizzle')
, $ = function(selector, context, results){
results = sizzle(selector, context, results)
if ( results.length === 1 ) {
return results[0]
} else {
return results
}
}
, bonzo = require('bonzo')
, shoe = require('shoe')
, dnode = require('dnode')
, cook = require('cookie-component')
, async = require('async')
, sessid = ''
;
function login(assertion) {
if ( assertion == null ) { return }
var xhr = new XMLHttpRequest()
, data
;
xhr.open("POST", "/auth/browserid", true)
xhr.setRequestHeader("Content-Type", "application/json")
xhr.addEventListener("loadend", function(e) {
try {
data = JSON.parse(this.response)
if ( data.status === "okay" ) {
// the email address the user logged in with
// console.log(data);
} else {
// console.log("Login failed because " + data.reason);
}
} catch ( ex ) {
// oh no, we didn't get valid JSON from the server
}
}, false)
xhr.send(JSON.stringify({ assertion: assertion }))
}
function sessionIsAlive(remote, sid, cb){
remote.auth(sid, function(auth){ cb(auth) })
}
function initAuth(remote){
//do i have a connect.sid cookie?
//does this sid have a live session?
sessid = cook('connect.sid')
sessionIsAlive(remote, sessid, function(auth){
if ( auth ) {
//we are authenticated
bonzo($('#game')).append('ok')
} else {
//we have to log in
bean.on(
bonzo($('#game')).append('<button id="login">login</button>')[0]
, 'click'
, function(){ navigator.id.get(login)}
)
}})
}
function drawLogin(){
}
function drawUser(){
}
function remoteReady(remote){
initAuth(remote)
}
bean.one(window, 'load', function(){
var container = $('#game')
, stream = shoe('/test')
, d = dnode()
;
d.on('remote', remoteReady)
d.pipe(stream).pipe(d);
})
}(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment