Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
311 additions
and
42 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
var schema = require("linkfilter/model/schema"), | ||
users = require("linkfilter/model/users"), | ||
bcrypt = require("bcrypt"), | ||
async = require("async"); | ||
|
||
var credentials = schema.define('credentials', { | ||
method: { type: String, length: 25, index: true }, | ||
secret: { type: String, length: 100 } | ||
}); | ||
|
||
users.hasMany(credentials, { as: 'credentials', foreignKey: 'users_id' }); | ||
credentials.belongsTo(users, { as: 'user', foreignKey: 'users_id' }); | ||
|
||
credentials.findByMethodSecret = function(method, secret, cb) { | ||
async.waterfall([ | ||
function(cb) { | ||
bcrypt.hash(secret, 10, cb); | ||
}, | ||
|
||
function(hash, cb) { | ||
credentials.all({ 'where': { 'method': method, 'secret': hash } }, cb); | ||
} | ||
], | ||
|
||
cb); | ||
}; | ||
|
||
credentials.ensureUser = function(username, email, method, secret, cb) { | ||
console.log("in ensure user"); | ||
async.waterfall([ | ||
function(cb) { | ||
credentials.findByMethodSecret(method, secret, cb); | ||
}, | ||
|
||
function(credentials, cb) { | ||
if (credentials.length == 0) { | ||
users.create({ 'username':username, 'email':email }, cb); | ||
} | ||
else if (credentials.length == 1) { | ||
credentials[0].user(cb); | ||
} | ||
else { | ||
throw new Error("More than one credential match"); | ||
} | ||
} | ||
], | ||
|
||
cb); | ||
}; | ||
|
||
module.exports = credentials; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var schema = require("./schema"), | ||
users = require("./users"), | ||
async = require("async"); | ||
|
||
var accounts = schema.define('external_accounts', { | ||
site: { type: String, length: 25, index: true }, | ||
external_id: { type: String, length: 100, index: true }, | ||
details: { type: schema.Text } | ||
}); | ||
|
||
users.hasMany(accounts, { as: 'accounts', foreignKey: 'users_id' }); | ||
accounts.belongsTo(users, { as: 'user', foreignKey: 'users_id' }); | ||
|
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var schema = require("./schema"), | ||
async = require("async"), | ||
bcrypt = require("bcrypt"); | ||
|
||
var users = schema.define('users', { | ||
username: { type: String, length: 25, index: true }, | ||
email: { type: String, length: 50, index: true }, | ||
password: { type: String, length: 50 } | ||
}); | ||
|
||
users.validatesPresenceOf("username", { message: "username is required" }); | ||
users.validatesPresenceOf("password", { message: "password is required" }); | ||
users.validatesPresenceOf("email", { message: "email is required" }); | ||
users.validatesUniquenessOf("username", { message: "username is not unique" }); | ||
users.validatesUniquenessOf("email", { message: "email is not unique" }); | ||
|
||
users.signin = function(username, password, cb) { | ||
async.waterfall([ | ||
function(cb) { | ||
users.all({ 'where': { 'username': username } }, cb); | ||
}, | ||
|
||
function(results, cb) { | ||
if (!results || results.length == 0) { | ||
throw new Error("No users found matching the username " + username); | ||
} | ||
else if (results.length > 1) { | ||
throw new Error("Found more than one user matching the username " + username); | ||
} | ||
else { | ||
cb(results[0]); | ||
} | ||
}, | ||
|
||
function(user, cb) { | ||
bcrypt.compare(password, user.password, function(err,result) { cb(err,user,result); }); | ||
}, | ||
|
||
function(user, result, cb) { | ||
if (result) | ||
cb(user); | ||
else | ||
throw new Error("Password does not match"); | ||
} | ||
], | ||
|
||
cb); | ||
}; | ||
|
||
users.signup = function(data, cb) { | ||
async.waterfall([ | ||
function(cb) { | ||
bcrypt.genSalt(10, cb); | ||
}, | ||
|
||
function(salt, cb) { | ||
bcrypt.hash(data.password, salt, cb); | ||
}, | ||
|
||
function(hash, cb) { | ||
data.password = hash; | ||
|
||
users.create(data,cb); | ||
} | ||
], | ||
|
||
cb); | ||
}; | ||
|
||
module.exports = users; | ||
|
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
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
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
Oops, something went wrong.