Skip to content

Commit

Permalink
Starting work on user authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
destos committed Nov 20, 2011
1 parent e183dd7 commit 1e3f15c
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -4,4 +4,5 @@ node_modules
npm-debug.log
docs/
.monitor
publicsrc/bootstrap
publicsrc/bootstrap
config/pats-mbp.yaml
14 changes: 14 additions & 0 deletions config/default.yaml
@@ -0,0 +1,14 @@
debug: false
server:
uri: "localhost"
port: 9000
auth:
twitter:
# consumerKey: "changeme"
# consumerSecret: "changeme"
facebook:
# appId: "changeme"
# appSecret: "changeme"
github:
# appId: false
# appSecret: false
70 changes: 70 additions & 0 deletions lib/auth/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions lib/server.js
@@ -1,10 +1,12 @@
(function() {
var Steps, app, express, path, public, publicsrc, root, steps;
var Steps, app, auth, config, express, path, public, publicsrc, root, steps;

express = require('express');

path = require('path');

config = require('config');

app = module.exports = express.createServer();

root = path.resolve(__dirname + "/../");
Expand All @@ -13,6 +15,8 @@

publicsrc = root + '/publicsrc';

auth = require('./auth')(app, config);

app.configure(function() {
app.set('views', path.resolve(__dirname + '/../views'));
app.set('view engine', 'jade');
Expand All @@ -26,6 +30,8 @@
secret: 'hgk83kc0qdm298xn',
store: new express.session.MemoryStore
}));
app.use(auth.middleware.auth());
app.use(auth.middleware.normalizeUserData());
app.use(express.compiler({
src: publicsrc,
dest: public,
Expand All @@ -46,6 +52,12 @@
return app.use(express.errorHandler());
});

app.dynamicHelpers({
session: function(req, res) {
return req.session;
}
});

app.get('/', function(req, res) {
console.log('Connect.sid ', req.cookies['connect.sid']);
return res.render('home', {
Expand All @@ -60,11 +72,12 @@
steps.createServer(app);

process.on('uncaughtException', function(err) {
console.log('\u0007');
console.error(err);
return console.log(err.stack);
});

app.listen(process.env.PORT || 9000);
app.listen(process.env.PORT || config.server.port || 9000);

console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

Expand Down
92 changes: 92 additions & 0 deletions libsrc/auth/index.coffee
@@ -0,0 +1,92 @@
everyauth = require 'everyauth'
https = require 'https'

module.exports = ( app, config ) ->

everyauth.debug = config.debug
external = config.auth

everyauth.everymodule.handleLogout (req, res) ->
delete req.session.user
req.logout()
# res.redirect()
res.writeHead(303, { 'Location': @logoutRedirectPath() })
res.end()

# Facebook
if (external?.facebook)
everyauth.facebook.appId(external.facebook.appId)
.appSecret(external.facebook.appSecret)
.findOrCreateUser (session, accessToken, accessTokenExtra, facebookUserMetaData) ->
true
.redirectPath('/')


# Twitter
if (external?.twitter)
everyauth.twitter
.myHostname(config.server.uri)
.consumerKey(external.twitter.consumerKey)
.consumerSecret(external.twitter.consumerSecret)
.findOrCreateUser (session, accessToken, accessSecret, twitterUser) ->
# actually create user here
true
.redirectPath('/')

# Github
if (external?.github)
everyauth.github
.myHostname(config.server.uri)
.appId(external.github.appId)
.appSecret(external.github.appSecret)
.findOrCreateUser (session, accessToken, accessTokenExtra, githubUser) ->
true
.redirectPath('/')

everyauth.helpExpress(app)

# Fetch and format data so we have an easy object with user data to work with.
normalizeUserData = ->
return (req, res, next) ->
if ( !req.session?.user && req.session?.auth?.loggedIn)
# possibly se a switch here
user = {}
if (req.session.auth.github)
user.image = 'http://1.gravatar.com/avatar/'+req.session.auth.github.user.gravatar_id+'?s=48'
user.name = req.session.auth.github.user.name
user.id = 'github-'+req.session.auth.github.user.id

if (req.session.auth.twitter)
user.image = req.session.auth.twitter.user.profile_image_url
user.name = req.session.auth.twitter.user.name
user.id = 'twitter-'+req.session.auth.twitter.user.id_str

if (req.session.auth.facebook)
user.image = req.session.auth.facebook.user.picture
user.name = req.session.auth.facebook.user.name
user.id = 'facebook-'+req.session.auth.facebook.user.id

# // Need to fetch the users image...
# https.get({
# 'host': 'graph.facebook.com'
# 'path': '/me/picture?access_token='+req.session.auth.facebook.accessToken
# }, (response) ->
# user.image = response.headers.location
# req.session.user = user
# next()
# .on 'error', (e) ->
# req.session.user = user
# next()

return

req.session.user = user

next()

return {
'middleware': {
'auth': everyauth.middleware
'normalizeUserData': normalizeUserData
}
}
19 changes: 18 additions & 1 deletion libsrc/server.coffee
@@ -1,12 +1,19 @@
# Server requirements
express = require 'express'
path = require 'path'
config = require 'config'

# Create server
app = module.exports = express.createServer()

root = path.resolve __dirname + "/../"

# Static directories
public = root + '/public'
publicsrc = root + '/publicsrc'

auth = require('./auth')(app,config)

app.configure( ->
app.set 'views', path.resolve __dirname + '/../views'
app.set 'view engine', 'jade'
Expand All @@ -18,6 +25,10 @@ app.configure( ->
secret: 'hgk83kc0qdm298xn'
store: new express.session.MemoryStore
})

app.use auth.middleware.auth()
app.use auth.middleware.normalizeUserData()

app.use express.compiler(
src: publicsrc
dest: public
Expand All @@ -32,6 +43,11 @@ app.configure 'development', ->

app.configure 'production', ->
app.use express.errorHandler()

app.dynamicHelpers {
session: (req, res) ->
return req.session;
}

app.get '/', (req, res) ->
console.log 'Connect.sid ', req.cookies['connect.sid']
Expand All @@ -46,9 +62,10 @@ steps.createServer app

# Catch uncaught exceptions
process.on 'uncaughtException', (err) ->
console.log('\u0007') #ringy dingy
console.error err
console.log err.stack

app.listen(process.env.PORT || 9000)
app.listen( process.env.PORT || config.server.port || 9000 )
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env)

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -17,13 +17,15 @@
"node": "~0.6.0"
},
"dependencies": {
"config": "*",
"express": "2.5.1",
"coffee-script": "1.1.3",
"jade": "0.17.0",
"nodemon": "*",
"skull.io": "0.2.0",
"underscore": "*",
"less": "*"
"less": "*",
"everyauth": "0.2.23"
},
"devDependencies": {
"expresso": "*"
Expand Down
17 changes: 13 additions & 4 deletions public/css/basics.css
Expand Up @@ -37,10 +37,6 @@ body {
padding: 20px 20px 10px;
margin: -20px -20px 20px;
}
/* Styles you shouldn't keep as they are for displaying this base example only */
.content .span10, .content .span4 {
min-height: 500px;
}
/* Give a quick and non-cross-browser friendly divider */
.content .span4 {
margin-left: 0;
Expand All @@ -50,3 +46,16 @@ body {
.topbar .btn {
border: 0;
}
.topbar ul li.account a.user-photo img {
border: medium none;
cursor: pointer;
display: inline-block;
height: 20px;
margin-right: 6px;
margin-top: -3px;
vertical-align: middle;
width: 20px;
-webkit-box-shadow: 0px 1px 0px white;
-moz-box-shadow: 0px 1px 0px white;
box-shadow: 0px 1px 0px white;
}
27 changes: 19 additions & 8 deletions publicsrc/css/basics.less
@@ -1,3 +1,9 @@
.boxshadow(@x: 0px, @y: 1px, @blur: 2px, @color: rgba(0,0,0,.15) ){
-webkit-box-shadow: @x @y @blur @color;
-moz-box-shadow: @x @y @blur @color;
box-shadow: @x @y @blur @color;
}

/* Override some defaults */
html, body {
background-color: #eee;
Expand All @@ -20,9 +26,7 @@ body {
-webkit-border-radius: 0 0 6px 6px;
-moz-border-radius: 0 0 6px 6px;
border-radius: 0 0 6px 6px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
box-shadow: 0 1px 2px rgba(0,0,0,.15);
.boxshadow(0, 1px, 2px, rgba(0,0,0,.15))
}

/* Page header tweaks */
Expand All @@ -32,11 +36,6 @@ body {
margin: -20px -20px 20px;
}

/* Styles you shouldn't keep as they are for displaying this base example only */
.content .span10,
.content .span4 {
min-height: 500px;
}
/* Give a quick and non-cross-browser friendly divider */
.content .span4 {
margin-left: 0;
Expand All @@ -46,4 +45,16 @@ body {

.topbar .btn {
border: 0;
}

.topbar ul li.account a.user-photo img {
border: medium none;
cursor: pointer;
display: inline-block;
height: 20px;
margin-right: 6px;
margin-top: -3px;
vertical-align: middle;
width: 20px;
.boxshadow(0px,1px,0px,white)
}

0 comments on commit 1e3f15c

Please sign in to comment.