Skip to content

Instantly share code, notes, and snippets.

@ryancole
Created January 11, 2012 04:40
Show Gist options
  • Save ryancole/1593058 to your computer and use it in GitHub Desktop.
Save ryancole/1593058 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) ->
# 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
@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