Skip to content

Instantly share code, notes, and snippets.

@ggoodman
Forked from ryancole/account.coffee
Created January 11, 2012 05:01
Show Gist options
  • Save ggoodman/1593128 to your computer and use it in GitHub Desktop.
Save ggoodman/1593128 to your computer and use it in GitHub Desktop.
bcrypt = require 'bcrypt'
# define the user db object
class Account
constructor: (@db) ->
@account = @db.collection 'accounts'
get: (spec, callback) ->
@account.findOne spec, (err, account) ->
callback err, account
authenticate: (spec, callback) ->
# get data for this username
@get username: spec.username, (err, account) ->
if account
# check the password
bcrypt.compare spec.password, account.password, (err, res) ->
if res
# update account spec
delete account.password
callback err, account
create: (spec, callback) ->
self = @
# make sure this username is not taken
@get username: spec.username, (err, account) ->
if not account
# has the password
bcrypt.genSalt 10, (err, salt) ->
bcrypt.hash spec.password, salt, (err, hash) ->
# update account spec
spec.password = hash
# put account into database
self.account.insert spec, (err, account) ->
# update account
delete account.password
callback err, account
# export the db object
module.exports = Account
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment