Created
January 11, 2012 04:40
-
-
Save ryancole/1593058 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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