From 643877b677394c8a771e6a5fa275e676f3d736f4 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Mon, 1 Jul 2013 16:50:03 -0700 Subject: [PATCH] Add initial User model --- README.md | 21 +++----------- lib/asteroid.js | 6 ++++ lib/models/user.js | 24 ++++++++-------- test/model.test.js | 69 +++++++++++++++++++++++----------------------- 4 files changed, 58 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 0d68a77e..2d772ee4 100644 --- a/README.md +++ b/README.md @@ -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: '/' } } }, diff --git a/lib/asteroid.js b/lib/asteroid.js index 4cb569bf..7c2c46d4 100644 --- a/lib/asteroid.js +++ b/lib/asteroid.js @@ -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'); diff --git a/lib/models/user.js b/lib/models/user.js index 38d20452..531606d4 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -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 -} \ No newline at end of file +/** + * Module Dependencies. + */ + +var Model = require('../asteroid').Model; + + +/** + * Extends from the built in `asteroid.Model` type. + */ + +var User = module.exports = Model.extend('user'); + diff --git a/test/model.test.js b/test/model.test.js index bf2a5228..4ecf4d28 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -463,40 +463,41 @@ 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) {