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. Extend a vanilla Asteroid model using the built in User model.
// define a User model // define a User model
var User = asteroid.User.extend( var User = asteroid.User.extend('user');
'user',
{
email: {
type: 'EmailAddress',
username: true
},
password: {
hideRemotely: true, // default for Password
type: 'Password',
min: 4,
max: 26
}
}
);
// attach to the memory connector // attach to the memory connector
User.attachTo(memory); User.attachTo(memory);
@ -862,7 +848,7 @@ Setup an authentication strategy.
// create a custom strategy // create a custom strategy
var LocalStrategy = require('passport-local').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) { User.findOne({ username: username }, function(err, user) {
if (err) { return done(err); } if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); } 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 // that contains the email
// to verify // to verify
email: 'email', 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'}; 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 = { * Module Dependencies.
id: {type: String, required: true}, */
username: {type: String, required: true},
password: String, var Model = require('../asteroid').Model;
authData: [],
email: String,
emailVerified: Boolean, /**
created: Date, * Extends from the built in `asteroid.Model` type.
lastUpdated: Date */
}
var User = module.exports = Model.extend('user');

View File

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