Add initial User model

This commit is contained in:
Ritchie Martori 2013-07-01 16:50:03 -07:00
parent 2a30efe23f
commit 643877b677
4 changed files with 58 additions and 62 deletions

View File

@ -814,21 +814,7 @@ Register and authenticate users of your app locally or against 3rd party service
Extend a vanilla Asteroid model using the built in User model.
// define a User model
var User = asteroid.User.extend(
'user',
{
email: {
type: 'EmailAddress',
username: true
},
password: {
hideRemotely: true, // default for Password
type: 'Password',
min: 4,
max: 26
}
}
);
var User = asteroid.User.extend('user');
// attach to the memory connector
User.attachTo(memory);
@ -862,7 +848,7 @@ Setup an authentication strategy.
// create a custom strategy
var LocalStrategy = require('passport-local').Strategy;
User.use(new LocalStrategy(function(username, password, done) {
passport.use(new LocalStrategy(function(username, password, done) {
User.findOne({ username: username }, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
@ -943,7 +929,8 @@ To require email verification before a user is allowed to login, supply a verifi
// that contains the email
// to verify
email: 'email',
template: 'email.ejs'
template: 'email.ejs',
redirect: '/'
}
}
},

View File

@ -260,3 +260,9 @@ asteroid.remoteMethod = function (fn, options) {
fn.http = fn.http || {verb: 'get'};
}
/*
* Built in models
*/
asteroid.Model = asteroid.createModel('model');
asteroid.User = require('./models/user');

View File

@ -1,11 +1,13 @@
// User model
var UserSchema = {
id: {type: String, required: true},
username: {type: String, required: true},
password: String,
authData: [],
email: String,
emailVerified: Boolean,
created: Date,
lastUpdated: Date
}
/**
* Module Dependencies.
*/
var Model = require('../asteroid').Model;
/**
* Extends from the built in `asteroid.Model` type.
*/
var User = module.exports = Model.extend('user');

View File

@ -464,39 +464,40 @@ describe('Model', function() {
});
});
// describe('Model.extend()', function(){
// it('Create a new model by extending an existing model.', function(done) {
// var User = asteroid.Model.extend('user', {
// email: String
// });
//
// User.foo = function () {
// return 'bar';
// }
//
// User.prototype.bar = function () {
// return 'foo';
// }
//
// var MyUser = User.extend('my-user', {
// foo: String,
// bar: String
// });
//
// assert(MyUser.prototype.bar === User.prototype.bar);
// assert(MyUser.foo === User.foo);
//
// var user = new MyUser({
// email: 'foo@bar.com',
// foo: 'foo',
// bar: 'bar'
// });
//
// assert.equal(user.email, 'foo@bar.com');
// assert.equal(user.foo, 'foo');
// assert.equal(user.bar, 'bar');
// });
// });
describe('Model.extend()', function(){
it('Create a new model by extending an existing model.', function() {
var User = asteroid.Model.extend('user', {
email: String
});
User.foo = function () {
return 'bar';
}
User.prototype.bar = function () {
return 'foo';
}
var MyUser = User.extend('my-user', {
a: String,
b: String
});
assert.equal(MyUser.prototype.bar, User.prototype.bar);
assert.equal(MyUser.foo, User.foo);
debugger;
var user = new MyUser({
email: 'foo@bar.com',
a: 'foo',
b: 'bar'
});
assert.equal(user.email, 'foo@bar.com');
assert.equal(user.a, 'foo');
assert.equal(user.b, 'bar');
});
});
// describe('Model.hasAndBelongsToMany()', function() {
// it("TODO: implement / document", function(done) {