Created
January 27, 2012 15:33
-
-
Save mikevalstar/1689312 to your computer and use it in GitHub Desktop.
Files for Node.js Tutorial Part 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /********************** | |
| A simple script to add a new user | |
| **********************/ | |
| // Includes | |
| var crypto = require('crypto'); | |
| function hashString(value) { | |
| hash = crypto.createHash('sha1'); | |
| hash.update(value); | |
| return hash.digest('hex'); | |
| } | |
| // Database | |
| var Database = require('./lib/Database'); | |
| var db = new Database(); | |
| db.connect('mongodb://localhost/mv'); | |
| if(process.argv.length == 4){ | |
| // 0 will be node, 1 will be the script | |
| var au = db.model('adminUser'); | |
| var usr = new au({login: process.argv[2], password: hashString(process.argv[3]) }); | |
| usr.save(function(err){ | |
| console.log("User added to database"); | |
| process.exit(0); // Success | |
| }); | |
| }else{ | |
| console.error("Script requires exactly 2 arguments"); | |
| console.error("Usage: node addUser.js <email> <password>"); | |
| process.exit(1); // Failure | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| h2 Admin | |
| h3 You are logged in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Includes | |
| var crypto = require('crypto'); | |
| function hashString(value) { | |
| hash = crypto.createHash('sha1'); | |
| hash.update(value); | |
| return hash.digest('hex'); | |
| } | |
| var AdminPages = module.exports = function AdminPages(){}; | |
| AdminPages.prototype = { | |
| db: null | |
| , initPages: function(app, db){ | |
| this.db = db; | |
| var self = this; | |
| app.get ('/Admin/Login', function(req, res) { self.pageLogin(req, res); } ); | |
| app.post('/Admin/Login', function(req, res) { self.pageLoginPost(req, res); } ); | |
| app.get ('/Admin/Logout', function(req, res) { self.pageLogout(req, res); } ); | |
| app.get ('/Admin', function(req, res) { self.pageIndex(req, res); } ); | |
| } | |
| , _checkLogin: function(req, res){ | |
| if(req.session && req.session.loggedIn === true) | |
| return true; | |
| res.redirect('/Admin/Login'); | |
| return false; | |
| } | |
| , pageLogin: function(req, res){ | |
| res.render('admin/login', { | |
| title: 'Login', | |
| showFullNav: false | |
| }); | |
| } | |
| , pageLoginPost: function(req, res){ | |
| if(req.body && req.body.password && req.body.email){ | |
| var adminuser = this.db.model('adminUser'); | |
| adminuser.findOne( | |
| { login: req.body.email | |
| , password: hashString(req.body.password) } | |
| , function(err, row){ | |
| if(err){ | |
| res.render('admin/login', { | |
| title: 'Login', | |
| showFullNav: false, | |
| error_text: err | |
| }); | |
| }else{ | |
| if(row){ | |
| req.session.loggedIn = true; // register user is logged in | |
| res.redirect('/Admin') | |
| }else{ | |
| res.render('admin/login', { | |
| title: 'Login', | |
| showFullNav: false, | |
| error_text: 'User not found, Please try again', | |
| email: req.body.email | |
| }); | |
| } | |
| } | |
| }); | |
| }else{ | |
| res.render('admin/login', { | |
| title: 'Login', | |
| showFullNav: false, | |
| error_text: 'Error processing login.' | |
| }); | |
| } | |
| } | |
| , pageIndex: function(req, res){ | |
| if( !this._checkLogin(req, res) ) return; | |
| res.render('admin/index', { | |
| title: 'Admin Index', | |
| showFullNav: false | |
| }); | |
| } | |
| , pageLogout: function(req, res){ | |
| delete req.session.loggedIn; | |
| res.redirect('/Admin/Login'); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //... | |
| // Database | |
| var Database = require('./lib/Database'); | |
| var db = new Database(); | |
| db.connect('mongodb://localhost/mv'); | |
| //... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Module dependencies. | |
| */ | |
| var express = require('express'); | |
| var app = module.exports = express.createServer(express.cookieParser() | |
| ,express.session({ secret: "mv secret" })); | |
| // ... | |
| var admin_pages = require('./lib/AdminPages'); | |
| var ap = new admin_pages(); | |
| ap.initPages(app, db); | |
| // ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Content: Login Block */ | |
| .loginform{ margin: auto; width: 340px; border: 1px solid #BBB; background: #DDD; border-radius: 4px; padding: 0.1em 1em; } | |
| .loginform h3{ padding: 0; margin: 0; } | |
| .loginform input[type="text"], .loginform input[type="password"]{ width: 240px; font-size: 1.05em; } | |
| .loginform button, .loginform input[type="button"], .loginform input[type="submit"]{ cursor: pointer; background: #777; border: none; border-radius: 20px; color: #ffffff; display: inline-block; font-size: 1em; font-weight: bold; letter-spacing: 1px; margin: 0.5em 0; padding: 7px 25px; text-transform: uppercase; } | |
| .loginform > form > span{ display: inline-block; width: 90px; } | |
| /* Content: Error Block */ | |
| div.error{ border: 1px solid #99432e; background: #99432e; padding: 0.45em 1em; margin: 0.5em 0; border-radius: 4px; color: white; } | |
| div.error span{ padding: 0.3em 1em; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mongoose = require('mongoose'); | |
| var Schema = mongoose.Schema | |
| , ObjectId = Schema.ObjectId; | |
| var Database = module.exports = function Database(){}; | |
| Database.prototype = { | |
| _collections: { | |
| adminUser: { | |
| login : String | |
| , password : String | |
| } | |
| } | |
| , _db: null | |
| , _schema: {} | |
| , _model: {} | |
| , connect: function(url){ | |
| mongoose.connect(url); | |
| this._schema.adminUser = new Schema(this._collections.adminUser); | |
| this._model.adminUser = mongoose.model('adminUser', this._schema.adminUser); | |
| } | |
| , model: function(mod){ | |
| switch (mod){ | |
| case 'adminUser': | |
| return this._model.adminUser; | |
| } | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| h2 Admin | |
| h3 You are logged in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| h2 Admin - Login | |
| div.loginform | |
| form(method="post") | |
| h3 Login | |
| - if(typeof(error_text) != 'undefined') | |
| div.error | |
| span=error_text | |
| span Login | |
| - if(typeof(email) != 'undefined') | |
| input(type="text", name="email", value=email) | |
| - else | |
| input(type="text", name="email") | |
| br | |
| span Password | |
| input(type="password", name="password") | |
| br | |
| span | |
| input(type="submit", value="Login") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "mikevalstar.com website" | |
| , "version": "0.0.3" | |
| , "private": true | |
| , "dependencies": { | |
| "express": ">= 2.5.0" | |
| , "jade": ">= 0.0.1" | |
| , "markdown-js": ">= 0.0.1" | |
| , "mongoose": ">= 2.4.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment