Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Initial commit of Web application file structure, and some in-progres…
Browse files Browse the repository at this point in the history
…s API support for user handling.
  • Loading branch information
allanmc committed Jun 13, 2013
1 parent 5b02688 commit 195505c
Show file tree
Hide file tree
Showing 19 changed files with 630 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
config
.idea
41 changes: 39 additions & 2 deletions README.md
@@ -1,4 +1,41 @@
scripler Scripler
======== ========
The Scripler web application source code repository.


The Scripler web application source code repository Folder Structure
----------------

* documentation
* Early documentation drafts of the API
* public
* Static files to be deliveredserved through the Web server
* views
* Dynamic templates that needs rendering before being served from the Web server
* config
* Configuration files for the Node.js server
* models
* The MongoDB schemas for the collections
* routes
* Application logic for the different logical domains

Requirements
------------
* [MongoDB][1]
* [Node.js][2] 10.0+

Installation
------------
npm install

How To Run
----------
node app.js


Bcrypt Installation Problems
----------------------------
Ask for a pre-build module, or follow the guide:
https://github.com/ncb000gt/node.bcrypt.js/#dependencies

[1]: http://www.mongodb.org/
[2]: http://nodejs.org/
54 changes: 54 additions & 0 deletions app.js
@@ -0,0 +1,54 @@
var express = require('express')
, index = require('./routes/index')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, mongoose = require('mongoose')
, conf = require('config')
, passport = require('passport')
, scriplerPassport = require('./routes/passport');

var app = express();

// db connect
mongoose.connect(conf.db.uri);

// all environments
app.set('port', conf.app.port);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('ejs', require('ejs-locals'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(conf.app.cookie_secret));
app.use(express.session({ secret: conf.app.session_secret }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

/*Dummy GUI*/
//app.get('/', index.index);
//app.get('/account', index.account);
//app.get('/login', index.login);
//app.post('/login', user.login);
//app.get('/new-user', index.newUser);
//app.post('/new-user', index.newUserPost);

/*API*/
app.get('/users', user.list);
app.post('/user/login', user.login);
app.post('/user/logout', user.logout);
app.post('/user/register', user.register);

scriplerPassport.initPaths(app);

http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port') + ('development' == app.get('env') ? ' - in development mode!' : ''));
});
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions models/project.js
@@ -0,0 +1,15 @@
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, bcrypt = require('bcrypt')
, SALT_WORK_FACTOR = 10;

/**
* User DB
*/
var ProjectSchema = new Schema({
name: { type: String, required: true },
order: { type: Number},
modified: { type: Date, default: Date.now }
});

exports.Project = mongoose.model('Project', ProjectSchema);
48 changes: 48 additions & 0 deletions models/user.js
@@ -0,0 +1,48 @@
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, bcrypt = require('bcrypt')
, SALT_WORK_FACTOR = 10;

/**
* User DB
*/
var UserSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
providers: [ {} ],
modified: { type: Date, default: Date.now }
});

/** Handle bcrypt password-hashing.
* Source: http://devsmash.com/blog/password-authentication-with-mongoose-and-bcrypt
*/
UserSchema.pre('save', function (next) {
var user = this;

// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) return next();

// generate a salt
bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
if (err) return next(err);

// hash the password along with our new salt
bcrypt.hash(user.password, salt, function (err, hash) {
if (err) return next(err);

// override the cleartext password with the hashed one
user.password = hash;
next();
});
});
});

UserSchema.methods.comparePassword = function (candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};

exports.User = mongoose.model('User', UserSchema);
30 changes: 30 additions & 0 deletions package.json
@@ -0,0 +1,30 @@
{
"name": "scripler-server",
"description": "Scripler Server",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.2.4",
"config": "~0.4.25",
"mongoose": "~3.6.11",
"ejs": "~0.8.4",
"ejs-locals": "~1.0.2",
"bcrypt": "~0.7.6",
"passport": "~0.1.17",
"passport-twitter": "~0.1.5",
"passport-facebook": "~0.1.5",
"passport-linkedin": "~0.1.3",
"passport-google": "~0.3.0",
"passport-local": "~0.1.6",
"xtend": "~2.0.5"
},
"readmeFilename": "README.md",
"main": "app.js",
"devDependencies": {},
"repository": "",
"author": "",
"license": "BSD"
}
Binary file added public/favicon.ico
Binary file not shown.
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
25 changes: 25 additions & 0 deletions routes/index.js
@@ -0,0 +1,25 @@
var user = require('./user');

exports.index = function (req, res) {
res.render('index', {user: req.user});
};
exports.account = function (req, res) {
res.render('account', { user: req.user });
};
exports.login = function (req, res) {
res.render('login', { user: req.user, message: req.session.messages });
};

exports.newUser = function (req, res) {
res.render('new-user', { user: req.user, message: req.session.messages });
};

exports.newUserPost = function (req, res, next) {
//res.send(util.inspect(req.body, false, null));
var email = req.body.email
if (!email) {
req.session.messages = ["You need to enter an email address!"];
return res.redirect('/new-user');
}
user.register(req, res);
}

0 comments on commit 195505c

Please sign in to comment.