Skip to content

Commit

Permalink
user handling, starting updating views
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidkahl committed Mar 17, 2012
1 parent 106ca8c commit 09acfe3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
15 changes: 11 additions & 4 deletions auth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ everyauth.everymodule.moduleErrback (err) ->
console.log "Auth ERROR - "+err

everyauth.everymodule.findUserById (userId, callback) ->
users.findById userId, (err,res) ->
users.findByTwitterId userId, (err,res) ->
if res.id
callback null, res
callback null, res.value
else
users.saveById JSON.stringify(userId), {"name":"test"}, (saveErr, saveRes) ->
callback null, saveRes
callback null, null

everyauth.twitter
.consumerKey(config.twitterConsumerKey)
.consumerSecret(config.twitterConsumerSecret)
.findOrCreateUser((session, token, secret, user) ->
users.findByTwitterId user.id, (err,res) ->
if res.id
user=res.value
else
newUser = {id:user.id,name:user.name,twitter:user}
user = newUser
users.saveById JSON.stringify(user.id), user, (saveErr, saveRes) ->
promise = @.Promise().fulfill user

).redirectPath '/'
22 changes: 21 additions & 1 deletion config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ config.sessionDBName = process.env.SPARK_SESSIONDBNAME || 'database_sessions'
config.twitterConsumerKey = process.env.SPARK_TWITTERCONSUMERKEY || 'twitterconsumerkey'
config.twitterConsumerSecret = process.env.SPARK_TWITTERCONSUMERSECRET || 'twitterconsumersecret'

module.exports = config
module.exports = config

# views for the user database
# "all": {
# "map": "function(doc) { emit(doc._id, doc);}"
# },
# "byid": {
# "map": "function(doc) {if(doc._id) emit(doc._id,doc); }"
# },
# "byTwitterId": {
# "map": "function(doc) {if(doc.twitter.id) emit(doc._id,doc); }"
#
# view for scene database
# "all": {
# "map": "function(doc) {\n emit(null, doc);\n}"
# },
# "byid": {
# "map": "function(doc) {if(doc._id){emit(doc._id, doc);}}"
# },
# "allbyid": {
# "map": "function(doc) {if(doc.url){emit(doc._id, null);}}"
3 changes: 0 additions & 3 deletions main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ 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
10 changes: 10 additions & 0 deletions user.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class User
}
@.db = @.connect.database collection

findByTwitterId: (mykey, callback) ->
@.db.view 'spark-user/byTwitterId', {key: JSON.stringify(mykey)}, (err, res) ->
if (err)
callback err, null
else
if res.length>0
callback null, res[0]
else
callback null, res

findById: (mykey, callback) ->
@.db.view 'spark-user/byid', {key: JSON.stringify(mykey)}, (err, res) ->
if (err)
Expand Down
5 changes: 3 additions & 2 deletions views/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ html
p.navbar-text.pull-right.highlightLogin
- if (everyauth.loggedIn)
span= "Logged in as "
img.twitterImage(src=everyauth.twitter.user.profile_image_url)
span= " "+ everyauth.twitter.user.name +" - "
- if (user.twitter)
img.twitterImage(src=user.twitter.profile_image_url)
span= " "+ user.name +" - "
a(href="/logout")= "Logout"
- else
a(href= "/auth/twitter")= "Login with Twitter"
Expand Down

0 comments on commit 09acfe3

Please sign in to comment.