Skip to content

Commit

Permalink
extended everyauth stuff, user database
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidkahl committed Mar 16, 2012
1 parent ddaae5a commit 106ca8c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
14 changes: 10 additions & 4 deletions auth.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
config = require './config.coffee'
User = require('./user.coffee').User
users = new User config.mainDBHost, config.mainDBPort, 'spark-user'

everyauth = module.exports = require 'everyauth'
everyauth.debug = false
everyauth.everymodule.moduleErrback (err) ->
console.log "Auth ERROR - "+err

everyauth.everymodule.findUserById (userId, callback) ->
callback null, "moo"
# callback has the signature, function (err, user) {...}
everyauth.everymodule.findUserById (userId, callback) ->
users.findById userId, (err,res) ->
if res.id
callback null, res
else
users.saveById JSON.stringify(userId), {"name":"test"}, (saveErr, saveRes) ->
callback null, saveRes

everyauth.twitter
.consumerKey(config.twitterConsumerKey)
.consumerSecret(config.twitterConsumerSecret)
.findOrCreateUser((session, token, secret, user) ->
promise = @.Promise().fulfill user
promise = @.Promise().fulfill user
).redirectPath '/'
4 changes: 4 additions & 0 deletions main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Articler = require('./articler.coffee').Articler
scene = new Articler config.mainDBHost, config.mainDBPort, config.mainDB

app.get '/', (req, res) ->
console.log req.user
if req.user is []
console.log "Push new user to db!"
else "No need to push to db!"
scene.findAll (err, docs) ->
threeDocs = []
for tempDoc in [1..3]
Expand Down
31 changes: 31 additions & 0 deletions user.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# User Class - Handles all user functions
config = require './config.coffee'
cradle = require 'cradle'

class User
constructor: (host, port, collection) ->
@.connect = new cradle.Connection host, port, {
secure: false
auth: {
username: config.mainDBUser
password: config.mainDBPass
}
cache: true
raw: false
}
@.db = @.connect.database collection

findById: (mykey, callback) ->
@.db.view 'spark-user/byid', {key: JSON.stringify(mykey)}, (err, res) ->
if (err)
callback err, null
else
if res.length>0
callback null, res[0]
else
callback null, res
saveById: (id, data, callback) ->
@.db.save id, data, (err, res) ->
callback err, res

exports.User = User
2 changes: 1 addition & 1 deletion views/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ html
a(href= "http://coffeescript.org/")="Coffee-Script"
|,
a(href= "https://github.com/tomgallacher/gzippo")="gzippo"
| and
| and
a(href= "http://twitter.github.com/bootstrap/")="Twitter Bootstrap"
|.
br
Expand Down

0 comments on commit 106ca8c

Please sign in to comment.