Created
August 31, 2011 20:39
-
-
Save thegoleffect/1184654 to your computer and use it in GitHub Desktop.
dnode auth working example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="/dnode.js" type="text/javascript"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> | |
<script type="text/javascript"> | |
DNode({ | |
session : function (session) { | |
$('#auth :visible').slideUp(); | |
$('#user').text(session.user); | |
$('#content').show(); | |
session.says('cat', $('#cat').text); | |
session.says('dog', $('#dog').text); | |
} | |
}).connect(); | |
$(window).load(function () { | |
$('#auth').submit(function (ev) { | |
ev.preventDefault(); | |
remote.session( | |
$(this.elements.user).val(), | |
$(this.elements.pass).val() | |
) | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<form id="auth" style="display: none"> | |
<input type="text" name="user"> | |
<input type="text" name="pass"> | |
<input type="submit" value="sign in"> | |
</form> | |
<div style="display: none"> | |
Signed in as <span id="user"> | |
</div> | |
<div id="content" style="display: none"> | |
<div> | |
The cat says <span id="cat">?</span>. | |
</div> | |
<div> | |
The dog says <span id="dog">?</span>. | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var connect = require('connect'); | |
var webserver = connect.createServer(); | |
webserver.use(connect.staticProvider(__dirname)); | |
var auth = require('dnode-auth'); | |
var dnode = require('dnode'); | |
dnode(function (client, conn) { | |
this.session = auth(function (user, pass, cb) { | |
if (user === 'whiskers' && pass === 'meow!') { | |
cb(null, session(user)); | |
} | |
}); | |
}).listen(webserver); | |
function session (user) { | |
return { | |
says : function (animal, cb) { | |
cb({ | |
cat : 'meow', | |
dog : 'woof', | |
}[animal]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment