From 106ca8c99f1ad4d8004949c5e3cb634f3d27a3f8 Mon Sep 17 00:00:00 2001 From: Arvid Kahl Date: Fri, 16 Mar 2012 22:37:06 +0100 Subject: [PATCH] extended everyauth stuff, user database --- auth.coffee | 14 ++++++++++---- main.coffee | 4 ++++ user.coffee | 31 +++++++++++++++++++++++++++++++ views/layout.jade | 2 +- 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 user.coffee diff --git a/auth.coffee b/auth.coffee index 16dad4a..b830c20 100644 --- a/auth.coffee +++ b/auth.coffee @@ -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 '/' \ No newline at end of file diff --git a/main.coffee b/main.coffee index 50f5b3d..70c16d2 100644 --- a/main.coffee +++ b/main.coffee @@ -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] diff --git a/user.coffee b/user.coffee new file mode 100644 index 0000000..5a29779 --- /dev/null +++ b/user.coffee @@ -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 \ No newline at end of file diff --git a/views/layout.jade b/views/layout.jade index 55c957b..aa188ab 100644 --- a/views/layout.jade +++ b/views/layout.jade @@ -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