diff --git a/Gruntfile.js b/Gruntfile.js index 5f08384..a93ae3f 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -32,7 +32,7 @@ var proxyConfig = { }; -//var lrSnippet = require('connect-livereload')({port: LIVERELOAD_PORT}); +var lrSnippet = require('connect-livereload')({port: LIVERELOAD_PORT}); var mountFolder = function(connect, dir) { return connect.static(require('path').resolve(dir)); }; @@ -101,7 +101,7 @@ module.exports = function(grunt) { open: true, base: ['.tmp', '<%= yeoman.app %>'], middleware: function(connect, options) { - startNodeServer(options, connect); + //startNodeServer(options, connect); return [ require('json-proxy').initialize(proxyConfig), mountFolder(connect, '.grunt'), diff --git a/app/index.html b/app/index.html index a463c2e..0835bfb 100755 --- a/app/index.html +++ b/app/index.html @@ -35,9 +35,9 @@ - - - + + + @@ -53,7 +53,7 @@ - + - @@ -102,7 +102,7 @@ - + @@ -115,7 +115,7 @@ - + diff --git a/config/config.json b/config/config.json index ebe49ca..98cd9eb 100644 --- a/config/config.json +++ b/config/config.json @@ -36,7 +36,7 @@ "port": 5001 }, "staticDir": "./app", - "publicDir": "./www", + "publicDir": "./app", "uploadsTmpDir": "./.tmp", "uploadsDestDir": "./www/cms-content/uploads", "uploadsUrl": "/cms-content/", diff --git a/package.json b/package.json index 8f8d985..8486501 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,25 @@ { "name": "angular-cms", + "productName": "angular-cms-app", "version": "0.0.2", "description": "This is a lightweight CMS built with Angular.js, Twitter Bootstrap and Node.js.", "author": "Jonnie Spratley", "license": "MIT", "readmeFilename": "README.md", + "main": "index.js", "homepage": "https://github.com/jonniespratley/angular-cms", - "main": "Gruntfile.js", "bugs": { "url": "https://github.com/jonniespratley/angular-cms/issues" }, + "engines": { + "node": "0.10.x", + "npm": "1.3.x" + }, + "scripts": { + "test": "grunt test", + "start": "electron .", + "build": "electron-packager . $npm_package_productName --prune --asar --all --version=0.29.1" + }, "directories": { "doc": "docs", "test": "test" @@ -64,11 +74,12 @@ "request": "~2.34.0", "serve-static": "^1.8.0", "socket.io": "~0.9.16", - "websocket": "^1.0.14" + "websocket": "^1.0.14", + "electron-debug": "^0.1.1" }, "devDependencies": { "chai": "^1.10.0", - "connect-proxy": "~1.0.2", + "del": "^1.1.0", "grunt": "~0.4.4", "grunt-angular-templates": "~0.5.1", @@ -127,18 +138,12 @@ "mocha": "^2.0.1", "protractor": "^1.5.0", "supertest": "^0.15.0", - "time-grunt": "~0.2.1" + "time-grunt": "~0.2.1", + "electron-packager": "^5.0.0", + "electron-prebuilt": "^0.29.1" }, "peerDependencies": { "grunt": "0.4.x", "karma": "~0.12.0" - }, - "engines": { - "node": "0.10.x", - "npm": "1.3.x" - }, - "scripts": { - "start": "node server.js", - "test": "grunt test" } } diff --git a/routes/cms-auth.js b/routes/cms-auth.js index 03e4da8..4d5bedd 100644 --- a/routes/cms-auth.js +++ b/routes/cms-auth.js @@ -1,7 +1,6 @@ var bodyParser = require('body-parser'), - mongoose = require('mongoose'), + util = require('util'), - User = require('./models/user'), session = require('express-session'), crypto = require('crypto'), bcrypt = require('bcrypt-nodejs'); @@ -15,6 +14,8 @@ var cmsAuth = function(config, app) { return bcrypt.hashSync(pass); }; + var UserModel = require('./models/user'); +var User = new UserModel(config, app); var cmsAuth = { /** * //### login @@ -33,11 +34,25 @@ var cmsAuth = function(config, app) { } query.password = hashPassword(req.body.password, query.username); console.warn('trying to login', query); + + query._id = 'user-'+ query.username; + + app.locals.db.get(query._id).then(function(resp){ + console.log('found user', resp); + res.status(200).json(resp); + }).catch(function(err){ + console.log('No user so creating'); + res.status(404).json(err); + }); + + + /* + User.findOne({ username: query.username }, function(err, data) { if (err) { - return res.jsonp(400, err); + return res.json(400, err); } try { if (data && bcrypt.compareSync(req.body.password, data.password)) { @@ -54,6 +69,7 @@ var cmsAuth = function(config, app) { }); } }); + */ }, /** * Handle registering a new user @@ -71,7 +87,7 @@ var cmsAuth = function(config, app) { if (req.body.email) { data.email = req.body.email; } - + data._id = 'user-'+ data.username; data.password = hashPassword(req.body.password, data.username); data.created_at = new Date(); data.updated_at = new Date(); @@ -80,6 +96,22 @@ var cmsAuth = function(config, app) { console.warn('trying to register', data); + app.locals.db.get(data._id).then(function(resp){ + console.log('found user', resp); + res.status(404).json({ + message: 'User already exists' + }); + }).catch(function(err){ + console.log('No user so creating'); + app.locals.db.put(data).then(function(resp){ + res.status(201).json(resp); + }); + }) + + + + +/* //Try and find user User.find({ username: data.username @@ -89,7 +121,7 @@ var cmsAuth = function(config, app) { })); var user = new User(data); if (err) { - res.jsonp(400, { + res.json(400, { message: 'Problem registering!' }); } @@ -101,7 +133,7 @@ var cmsAuth = function(config, app) { } else { user.save(function(er, ok) { if (er) { - return res.jsonp(400, { + return res.json(400, { message: 'Problem registering!' }); } else { @@ -111,7 +143,11 @@ var cmsAuth = function(config, app) { } }); + */ }, + + + session: function(req, res, next) { var user = req.session; if (req.session && req.session.user) { diff --git a/routes/cms-db.js b/routes/cms-db.js index 1792b69..9403325 100644 --- a/routes/cms-db.js +++ b/routes/cms-db.js @@ -1,4 +1,3 @@ -var mongoose = require('mongoose'); module.exports = function(config) { console.warn('cms-db initialized'); diff --git a/routes/cms-rest.js b/routes/cms-rest.js index f3aa068..8df1078 100644 --- a/routes/cms-rest.js +++ b/routes/cms-rest.js @@ -8,7 +8,7 @@ module.exports = function(config, app) { var router = express.Router(); router.use(function(req, res, next) { - console.log('cms-rest Time:', Date.now()); + console.log('cms-rest Time:', Date.now(), req.params, req.method, req.body); next(); }); diff --git a/routes/cms-router.js b/routes/cms-router.js index 7ddee1c..bc5e7f0 100644 --- a/routes/cms-router.js +++ b/routes/cms-router.js @@ -17,13 +17,52 @@ module.exports = function(config, app) { // TODO: Using pouchdb var PouchDB = require('pouchdb'); PouchDB.debug('*'); - var db = new PouchDB(config.db.local); + var db2 = new PouchDB(config.db.remote); + PouchDB.sync(db, db2); app.locals.db = db; console.log('Connected to', config.db.local); +// TODO: + +/* +router.route('/users/:user_id') +.all(function(req, res, next) { + // runs for all HTTP verbs first + // think of it as route specific middleware! + next(); +}) +.get(function(req, res, next) { + res.json(req.user); +}) +.put(function(req, res, next) { + // just an example of maybe updating the user + req.user.name = req.params.name; + // save user ... etc + res.json(req.user); +}) +.post(function(req, res, next) { + next(new Error('not implemented')); +}) +.delete(function(req, res, next) { + next(new Error('not implemented')); +}) + + +var replication = PouchDB.replicate('mydb', 'http://localhost:5984/mydb', {live: true}) + .on('change', function (info) { + // handle change + }).on('complete', function (info) { + // handle complete + }).on('uptodate', function (info) { + // handle up-to-date + }).on('error', function (err) { + // handle error + }); +replication.cancel(); // whenever you want to cancel +*/ var serverPort = process.env.PORT || config.port; var serverHost = process.env.IP || config.host; @@ -32,7 +71,7 @@ module.exports = function(config, app) { require('./cms-db')(config); require('./cms-auth')(config, app); // require('./cms-passport')(config, app); - require('./cms-upload')(config, app); + //require('./cms-upload')(config, app); require('./cms-server')(config, app); require('./cms-rest')(config, app); //require('./cms-proxy')(config, app); diff --git a/routes/models/upload.js b/routes/models/upload.js index 54cfb27..71436fb 100644 --- a/routes/models/upload.js +++ b/routes/models/upload.js @@ -1,14 +1,2 @@ -var mongoose = require( 'mongoose' ); - -module.exports = mongoose.model( 'Upload', mongoose.Schema( { - name: String, - filename: String, - size: Number, - url: String, - type: String, - path: String, - meta: Object, - created: Date, - updated: Date -} ) ); +module.exports = function(){}; diff --git a/routes/models/user.js b/routes/models/user.js index 1f7e6cb..fa32069 100644 --- a/routes/models/user.js +++ b/routes/models/user.js @@ -1,88 +1,100 @@ // app/models/user.js // load the things we need -var mongoose = require( 'mongoose' ); +//var mongoose = require( 'mongoose' ); var bcrypt = require( 'bcrypt-nodejs' ); var hash = bcrypt.hashSync("bacon"); -// define the schema for our user model -var UserSchema = mongoose.Schema( { - id: String, - provider: String, - displayName: String, - name: Object, - emails: Array, - photos: Array, - username: String, - email: String, - password: String, - active: Boolean, - meta: Object, - token: String, - created_at: Date, - updated_at: Date, - local: { - email: String, - password: String, - }, - facebook: { - id: String, - token: String, - email: String, - name: String - }, - twitter: { + + + +// create the model for users and expose it to our app +module.exports = function(config, app){ + // define the schema for our user model + this.Schema = { id: String, - token: String, + provider: String, displayName: String, - username: String - }, - google: { - id: String, - token: String, + name: Object, + emails: Array, + photos: Array, + username: String, email: String, - name: String - } + password: String, + active: Boolean, + meta: Object, + token: String, + created_at: Date, + updated_at: Date, + local: { + email: String, + password: String, + }, + facebook: { + id: String, + token: String, + email: String, + name: String + }, + twitter: { + id: String, + token: String, + displayName: String, + username: String + }, + google: { + id: String, + token: String, + email: String, + name: String + } -} ); + } ; + // methods ====================== + this.method = function(name, cb){ + this[name] = cb; + console.log('adding method', name); + } + // generating a hash + this.method( 'generateHash', function (password) { + return bcrypt.hashSync( password, bcrypt.genSaltSync( 8 ), null ); + }); -// methods ====================== -// generating a hash -UserSchema.method( 'generateHash', function (password) { - return bcrypt.hashSync( password, bcrypt.genSaltSync( 8 ), null ); -}); + // checking if password is valid + this.method( 'validPassword', function (password) { + return bcrypt.compareSync( password, this.password ); + }); -// checking if password is valid -UserSchema.method( 'validPassword', function (password) { - return bcrypt.compareSync( password, this.password ); -}); + this.method( 'findOrCreate', function (profile, fn) { + console.warn( 'findOrCreate', profile ); + this.findByEmail( profile.emails[0], function (err, user) { + fn( user ); + } ); + }); -UserSchema.method( 'findOrCreate', function (profile, fn) { - console.warn( 'findOrCreate', profile ); - UserSchema.findByEmail( profile.emails[0], function (err, user) { - fn( user ); + this.method( 'findByUsername', function (username, fn) { + console.warn( 'findByUsername', username ); + this.find( {username: username}, function (err, data) { + if (err) { + fn( false, err ); + } + fn( data ); + } ); } ); -}); -UserSchema.method( 'findByUsername', function (username, fn) { - console.warn( 'findByUsername', username ); - UserSchema.find( {username: username}, function (err, data) { - if (err) { - fn( false, err ); + this.method( 'findByEmail', function (email, fn) { + console.warn( 'findByEmail', email ); + for (var i = 0, len = users.length; i < len; i++) { + var user = users[i]; + if (user.email === email) { + return fn( null, user ); + } } - fn( data ); + return fn( null, null ); } ); -} ); -UserSchema.method( 'findByEmail', function (email, fn) { - console.warn( 'findByEmail', email ); - for (var i = 0, len = users.length; i < len; i++) { - var user = users[i]; - if (user.email === email) { - return fn( null, user ); - } - } - return fn( null, null ); -} ); + this.method('find', function(user){ + console.log('find this uer', user); + }) -// create the model for users and expose it to our app -module.exports = mongoose.model( 'User', UserSchema ); + +}; diff --git a/server.js b/server.js index 0209c1e..39a76ae 100755 --- a/server.js +++ b/server.js @@ -46,7 +46,14 @@ var options = { var staticDir = config.publicDir; console.log('staticDir', staticDir); -app.use(express.static(staticDir, options)); + + +//app.use(express.static(config.publicDir)); +//app.use(express.static(config.staticDir)); +//app.use(express.static('www')); +app.use(express.static('app')); + +app.use('/www', express.static('./www')); app.use(express.static(path.resolve(__dirname, config.staticDir), options)); app.all('/', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); @@ -57,8 +64,9 @@ app.all('/', function(req, res, next) { app.get('/', function(res, req, next) { var indexFile = config.publicDir + path.sep + 'index.html'; - console.warn('Loading index', indexFile); - req.send(indexFile); + console.log('Loading index', indexFile); + req.sendFile(indexFile); + next(); }); app.listen(port, function() {