diff options
author | Dennis Eriksen <dennis.se@gmail.com> | 2013-04-29 09:16:00 +0200 |
---|---|---|
committer | Dennis Eriksen <dennis.se@gmail.com> | 2013-04-29 09:16:00 +0200 |
commit | dd061b09ae345a272810984335cc84281b0c87ce (patch) | |
tree | b7c6d40673d3452a4a1456ba9370eca1cec986f1 | |
parent | Wrong location for img (diff) | |
parent | cleaning (diff) | |
download | Divid-dd061b09ae345a272810984335cc84281b0c87ce.tar.gz |
Merge branch 'dev'
Conflicts:
app.js
router.js
-rw-r--r-- | README.md | 23 | ||||
-rw-r--r-- | app.js | 62 | ||||
-rw-r--r-- | config/config.js | 25 | ||||
-rw-r--r-- | config/email-settings.js | 13 | ||||
-rw-r--r-- | config/express.js | 60 | ||||
-rw-r--r-- | config/middlewares/authorization.js | 13 | ||||
-rw-r--r-- | config/passport.js | 109 | ||||
-rw-r--r-- | controllers/system.js | 62 | ||||
-rw-r--r-- | controllers/users.js | 67 | ||||
-rw-r--r-- | models/user.js | 128 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | public/css/login.css | 138 | ||||
-rw-r--r-- | router.js | 296 | ||||
-rw-r--r-- | views/navbar.ejs | 4 | ||||
-rw-r--r-- | views/signup.ejs | 251 |
15 files changed, 538 insertions, 716 deletions
@@ -2,9 +2,26 @@ DERS ==== Bachelorprosjekt -hei + +Requirements: +- Node 0.10.x +- MongoDB + + +To run, first clone the project. + ```git clone git@github.com:dennisse/Divid.git``` + +Then install module dependencies by running ```npm install``` from inside the repository. + +Start the application with ```npm start``` + -Credit where credit is due: -https://github.com/braitsch/node-login + + + + + +Credit where credit is due: +https://github.com/madhums/nodejs-express-mongoose-demo @@ -2,58 +2,54 @@ /** * Module dependencies. */ - var express = require('express') - , path = require('path') - , bcrypt = require('bcrypt') + , fs = require('fs') , passport = require('passport'); -var app = express(); // initiates express - /** * App configuration */ - -app.configure(function(){ - // this controls the port the application will be running on. - // by adding 'process.enc.PORT' we enable the app to run on automated systems like heroku - app.set('port', process.env.PORT || 8001); - - app.set('views', __dirname + '/views'); // sets views to the right directory - app.set('view engine', 'ejs'); // initiates viewengine. We use EJS, or embedded js - http://embeddedjs.com/ - app.use(express.favicon(__dirname + '/public/faviconb.ico')); // sets favicon - app.use(express.logger('dev')); - app.use(express.bodyParser()); - app.use(express.cookieParser()); - app.use(express.session({ secret: 'lsdrghoi4hgqio42nqf2uqi32f3bilu23fl23b' })); - app.use(express.methodOverride()); - app.use(require('less-middleware')({ src: __dirname + '/public' })); - app.use(express.static(path.join(__dirname, 'public'))); - app.use(passport.initialize()); - app.use(passport.session()); +var port = process.env.PORT || 8000 + , env = process.env.NODE_ENV || 'development' + , config = require('./config/config')[env] + , auth = require('./config/middlewares/authorization') + , mongoose = require('mongoose'); + +// Bootstrap db connection +mongoose.connect(config.db); +var db = mongoose.connection; +db.on('error', console.error.bind(console, 'connection error:')); +db.once('open', function callback(){ + console.log('Connected to MongoDB'); }); -app.configure('development', function(){ - app.use(express.errorHandler()); +// Bootstrap models +var models_path = __dirname + '/models'; +fs.readdirSync(models_path).forEach( function(file) { + require(models_path + '/' + file); }); - +// Bootstrap passport config +require('./config/passport')(passport, config); /** - * Routes + * Express */ +var app = express(); +// express settings +require('./config/express')(app, config, passport); -require('./router')(app); +/** + * Routes + */ +require('./router')(app, passport, auth); /** * Server initiation */ - -app.listen(app.get('port'), function() { - console.log("Express server listening on port " + app.get('port')); +app.listen(port, function() { + console.log("Express server listening on port " + port); }); - - diff --git a/config/config.js b/config/config.js new file mode 100644 index 0000000..50dea23 --- /dev/null +++ b/config/config.js @@ -0,0 +1,25 @@ +var path = require('path') + , rootPath = path.normalize(__dirname + '/..'); + + +module.exports = { + development: { + db: 'mongodb://localhost/test', + root: rootPath, + app: { + name: 'Divid' + }, + facebook: { + clientID: "504825706245603", + clientSecret: "e5ea0faed85d8749cafd38732530ef35", + callbackURL: "https://divid.no/auth/facebook/callback" + }, + twitter: { + clientID: "tpCfKBUyAfogTpFxnb9w", + clientSecret: "abzInK4Nu0IFUhyXl73O2XjlFLFlzmBtLmbXk6v8", + callbackURL: "https://divid.no/auth/twitter/callback" + } + } +} + + diff --git a/config/email-settings.js b/config/email-settings.js deleted file mode 100644 index 8bc9983..0000000 --- a/config/email-settings.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Email settings - */ - -module.exports = { - - host: 'localhost', - user: 'divid', - password: '', - sender: 'Divid <divid@divid.no>' - -} - diff --git a/config/express.js b/config/express.js new file mode 100644 index 0000000..eb10b20 --- /dev/null +++ b/config/express.js @@ -0,0 +1,60 @@ + +/** + * Module dependencies + */ + +var express = require('express'); + + +/** + * Module + */ + +module.exports = function (app, config, passport) { + + //sets view engine and path + app.set('views', config.root + '/views'); + app.set('view engine', 'ejs'); + + app.use(express.static(config.root + '/public')); + + // don't use logger for test enc + if (process.env.NODE_ENV !== 'test') app.use(express.logger('dev')); + + + app.configure(function () { + + app.use(express.cookieParser()); //must be above sessions + + app.use(express.bodyParser()); //must be above methodOverride + + app.use(express.methodOverride()); + + app.use(express.session({ secret: 'lsdrghoi4hgqio42nqf2uqi32f3bilu23fl23b' })); + + // use passport session + app.use(passport.initialize()); + app.use(passport.session()); + + app.use(express.favicon(__dirname + '/public/faviconb.ico')); + + // use LESS for CSS + app.use(require('less-middleware')({ src: config.root + '/public' })); + + app.use(app.router); + + app.use(function(err, req, res, next) { + if (~err.message.indexOf('not fount')) return next(); // treat like 404 + + console.error(err.stack); + + res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); // render page + }); + + app.use(function(req, res, next) { + res.status(404).render('error', { title: '404', text: 'Fant ikke siden du så etter' }); // render page + }); + }); +} + + diff --git a/config/middlewares/authorization.js b/config/middlewares/authorization.js new file mode 100644 index 0000000..440f085 --- /dev/null +++ b/config/middlewares/authorization.js @@ -0,0 +1,13 @@ + + +/* + * Generic require login routing + */ + +exports.requiresLogin = function(req, res, next) { + if (!req.isAuthenticated()) return res.redirect('/login'); + next(); +} + + + diff --git a/config/passport.js b/config/passport.js new file mode 100644 index 0000000..9518409 --- /dev/null +++ b/config/passport.js @@ -0,0 +1,109 @@ +// dependencies +var mongoose = require('mongoose') + , LocalStrategy = require('passport-local').Strategy + , FacebookStrategy = require('passport-facebook').Strategy + , TwitterStrategy = require('passport-twitter').Strategy + , User = mongoose.model('User'); + +/** + * This is where the magic happends + */ + +module.exports = function (passport, config) { + + // serialize sessions + passport.serializeUser( function(user, done) { + done(null, user.id); + }); + passport.deserializeUser( function(id, done) { + User.findOne({ _id: id }, function(err, user) { + done(err, user); + }); + }); + + + /** + * Local strategy + */ + passport.use(new LocalStrategy({ + usernameField: 'email', + passwordField: 'password' + }, function(email, password, done) { + + // looks up the user in the database, and check if password matches + User.findOne({ email: email }, function(err, user) { + if (err) return done(err); + if (!user) return done(null, false, { message: 'Unknown user' }); + if (!user.authenticate(password)) return done(null, false, { message: 'Invalid password' }); + return done(null, user); + }); + })); + + + /** + * Facebook strategy + */ + passport.use(new FacebookStrategy({ + clientID: config.facebook.clientID + , clientSecret: config.facebook.clientSecret + , callbackURL: config.facebook.callbackURL + }, function(accessToken, refreshToken, profile, done) { + + // looks up the user in the database. Will create the user if it does not exist + User.findOne({ 'facebook.id': profile.id }, function(err, user) { + if(err) return done(err); + if (!user) { + user = new User({ + name: profile.displayName + , email: profile.emails[0].value + , username: profile.username + , provider: 'facebook' + , facebook: profile._json + }); + user.save(function(err) { + if (err) console.log(err); + return done(err, user); + }); + } else { + return done(err, user); + } + }); + })); + + + /** + * Twitter strategy + */ + passport.use(new TwitterStrategy({ + consumerKey: config.twitter.clientID + , consumerSecret: config.twitter.clientSecret + , callbackURL: config.twitter.callbackURL + }, function(token, tokenSecret, profile, done) { + + // looks up the user in the database. Will create the user if it does not exist + User.findOne({ 'twitter.id': profile.id }, function(err, user) { + if (err) return done(err); + if (!user) { + user = new User({ + name: profile.displayName + , username: profile.username + , provider: 'twitter' + , twitter: profile._json + }); + user.save(function(err) { + if (err) console.log(err); + return done(err, user); + }); + } else { + return done(err, user); + } + }); + })); + + + + + + +} + diff --git a/controllers/system.js b/controllers/system.js new file mode 100644 index 0000000..e54fc1c --- /dev/null +++ b/controllers/system.js @@ -0,0 +1,62 @@ + +/** + * Module dependencies + */ + + + + + +/** + * Before the user log in + * =============================================================== +*/ + +exports.index = function(req, res) { + res.render('index', { title: 'DERS' }); + }; + + + +exports.test = function(req, res) { + res.render('test', { + title: 'test', + loggedin: false + }); + }; + + +exports.home = function(req, res) { + res.render('home', { + title: 'home', + loggedin: false + }); + }; + + + + + + + +/** + * After the user has logged in + * =============================================================== +*/ + + + +exports.dashboard = function(req, res) { + console.log('/dashboard - ' + req.user); + res.render('dashboard', { + title: 'kanin', + loggedin: true + }); + }; + + + +exports.project = function(req, res) { + res.render('project', { title: 'Harepus', loggedin: true }); +} + diff --git a/controllers/users.js b/controllers/users.js new file mode 100644 index 0000000..d465468 --- /dev/null +++ b/controllers/users.js @@ -0,0 +1,67 @@ + +/** + * Module dependencies + */ + +var mongoose = require('mongoose') + , User = mongoose.model('User'); + +/** + * Login + */ +exports.login = function(req, res) { + res.render('login', { + title: 'Login' + }); + +} + + +/** + * Logout + */ +exports.logout = function(req, res) { + req.logout(); + res.resirect('/test'); +} + + +/** + * Signin + * This is triggered when the user post to /login + */ +exports.signin = function(req, res) {} + + +/** + * Signup + */ +exports.signup = function(req, res) { + res.render('signup', { title: 'Registrer deg' }); +} + +/** + * Create users + */ +exports.create = function(req, res) { + var user = new User(req.body); + user.provider = 'local'; + user.save(function(err) { + if (err) return res.render('/signup', { errors: err.errors, user: user }); + req.logIn(user, function(err) { + if (err) return next(err); + return res.redirect('/dashboard'); + }); + }); +} + + +/** + * AuthCallback + * This is what happends when a user has signed in using facebook/twitter + */ + +exports.authCallback = function(req, res, next) { + res.redirect('/dashboard'); +} + diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..b61f7c9 --- /dev/null +++ b/models/user.js @@ -0,0 +1,128 @@ + +/** + * Module dependencies + */ + +var mongoose = require('mongoose') + , Schema = mongoose.Schema + , crypto =require('crypto') + , authTypes = ['facebook', 'twitter']; + + +/** + * User schema + */ + +var UserSchema = new Schema({ + name: String, + email: String, + username: String, + provider: String, + hashed_password: String, + salt: String, + facebook: {}, + twitter: {} +}); + +/** + * Virtuals + */ + +UserSchema + .virtual('password') + .set(function(password) { + this._password = password + this.salt = this.makeSalt() + this.hashed_password = this.encryptPassword(password) + }).get(function() { return this._password }); + +/** + * Validations + */ +var validatePrecenceOf = function (value) { + return value && value.length; +} + +// the four validations below only apply if you are signing up traditionally + +UserSchema.path('name').validate(function(name) { + // if you're authenticated by any of the oauth strategies (facebook, twitter), don't validate + if(authTypes.indexOf(this.provider) !== -1) return true; + return name.length; +}, 'Name cannot be blank'); + +UserSchema.path('email').validate(function(email) { + if(authTypes.indexOf(this.provider) !== -1) return true; + return email.length; +}, 'Email cannot be blank'); + +UserSchema.path('username').validate(function(username) { + if(authTypes.indexOf(this.provider) !== -1) return true; + return username.length; +}, 'Username cannot be blank'); + +UserSchema.path('hashed_password').validate(function(hashed_password) { + if(authTypes.indexOf(this.provider) !== -1) return true; + return hashed_password.length; +}, 'Password cannot be blank'); + +/** + * Pre-save hook + */ + +UserSchema.pre('save', function(next) { + if (!this.isNew) return next(); + + if(!validatePrecenceOf(this.password) + && authTypes.indexOf(this.provider) === -1) + next(new Error('Invalid password')); + else next(); +}); + + +/** + * Methods + */ + +UserSchema.methods = { + + /** + * Authenticate - check if passwords are the same + * + * @param {String} plainText + * @return {Bolean} + * @api public + */ + + authenticate: function(plainText) { + return this.encryptPassword(plainText) === this.hashed_password; + }, + + /** + * Make salt + * + * @return {String} + * @api public + */ + + makeSalt: function() { + return Math.round((new Date().valueOf() * Math.random())) + ''; + }, + + /** + * Encrypt password + * + * @param {String} password + * @return {String} + * @api public + */ + + encryptPassword: function(password) { + if (!password) return ''; + return crypto.createHmac('sha1', this.salt).update(password).digest('hex'); + } +} + +mongoose.model('User', UserSchema); + + diff --git a/package.json b/package.json index 48ae466..c2ca75b 100644 --- a/package.json +++ b/package.json @@ -22,10 +22,9 @@ , "passport-local": "latest" , "passport-facebook": "latest" , "passport-twitter": "latest" - , "bcrypt": "latest" , "nodemon": "latest" } - , "decDependencies": { + , "devDependencies": { "mocha": "latest" } } diff --git a/public/css/login.css b/public/css/login.css deleted file mode 100644 index 5709783..0000000 --- a/public/css/login.css +++ /dev/null @@ -1,138 +0,0 @@ -body { - margin: 0; - padding: 0; - height: 100%; - background: #f2f9fe; - background: -moz-radial-gradient(center, ellipse cover, #f2f9fe 0%, #d6f0fd 100%); -/* FF3.6+ */ - background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%, #f2f9fe), color-stop(100%, #d6f0fd)); -/* Chrome,Safari4+ */ - background: -webkit-radial-gradient(center, ellipse cover, #f2f9fe 0%, #d6f0fd 100%); -/* Chrome10+,Safari5.1+ */ - background: -o-radial-gradient(center, ellipse cover, #f2f9fe 0%, #d6f0fd 100%); -/* Opera 12+ */ - background: -ms-radial-gradient(center, ellipse cover, #f2f9fe 0%, #d6f0fd 100%); -/* IE10+ */ - background: radial-gradient(center, ellipse cover, #f2f9fe 0%, #d6f0fd 100%); -/* W3C */ -} -#login-container { - width: 340px; - margin: 160px auto; -} -#login-container label { - margin: 15px 0 5px 5px; -} -#login-container button { - width: 150px; - padding: 6px 40px 6px 40px; - float: left; - margin-top: 15px; -} -#login-container .checkbox { - margin: 23px 0 0 180px; -} -#login-container .btm-links { - text-align: center; - margin-top: 10px; -} -#login-container #create-account { - float: right; - margin-right: 13px; -} -#login-container #forgot-password { - float: left; - margin-left: 10px; -} -#account-form-container { - width: 460px; - margin: 100px auto; -} -#country-list { - width: 280px; -} -.modal-form-errors li { - color: #808080; - text-shadow: -1px 1px 0 #fff; - -moz-text-shadow: -1px 1px 0 #fff; - -webkit-text-shadow: -1px 1px 0 #fff; - color: #d8000c; -} -.modal-single-input { - width: 452px; - margin: -200px 0 0 -232px; -} -.modal-single-input form { - color: #808080; - text-shadow: -1px 1px 0 #fff; - -moz-text-shadow: -1px 1px 0 #fff; - -webkit-text-shadow: -1px 1px 0 #fff; - margin-bottom: 0; -} -.modal-single-input form button { - width: 80px; - top: 213px; - position: absolute; -} -.modal-single-input form #cancel { - right: 110px; -} -.modal-single-input form #submit { - right: 18px; -} -.modal-single-input .modal-body { - height: 140px; - overflow: hidden; - padding-bottom: 0; -} -.modal-single-input .modal-footer { - height: 20px; -} -.modal-alert { - width: 310px; - margin: -200px 0 0 -150px; -} -#btn-logout { - width: 100px; - top: 1px; - right: 8px; - position: absolute; -} -#print { - padding: 20px; -} -#four04 { - padding: 20px; -} -.clear-fix { - clear: both; -} -.well { - margin-left: 0; - border: 1px solid #ccc; -} -.form-horizontal .control-label { - width: 80px; -} -.form-horizontal .controls { - margin-left: 120px; -} -.form-horizontal .form-actions { - padding: 17px 0 0 270px; -} -label { - color: #808080; - text-shadow: -1px 1px 0 #fff; - -moz-text-shadow: -1px 1px 0 #fff; - -webkit-text-shadow: -1px 1px 0 #fff; -} -.subheading { - color: #808080; - text-shadow: -1px 1px 0 #fff; - -moz-text-shadow: -1px 1px 0 #fff; - -webkit-text-shadow: -1px 1px 0 #fff; - margin-top: 3px; -} -button i { - padding-left: 8px; -} @@ -1,162 +1,11 @@ -var passport = require('passport') - , LocalStrategy = require('passport-local').Strategy - , FacebookStrategy = require('passport-facebook').Strategy - , mongodb = require('mongodb') - , mongoose = require('mongoose') - , bcrypt = require('bcrypt') - , SALT_WORK_FACTOR = 15; - -var FACEBOOK_APP_ID = "504825706245603"; -var FACEBOOK_APP_SECRET = "e5ea0faed85d8749cafd38732530ef35"; - -// connects to mongodb -mongoose.connect('localhost', 'test'); -var db = mongoose.connection; -db.on('error', console.error.bind(console, 'connection error:')); -db.once('open', function callback(){ - console.log('Connected to MongoDB'); -}); - -// user scheme -var userSchema = mongoose.Schema({ - username: { type: String, required: true, unique: true }, - email: { type: String, required: true, unique: true }, - password: { type: String, required: true }, //passwords doesn't need to be unique - accessToken:{ type: String } // used for Remember Me -}); - -// bcrypt middleware -userSchema.pre('save', function(next) { - var user = this; - - if (!user.isModified('password')) return next(); - - bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { - if(err) return next(err); - - bcrypt.hash(user.password, salt, function(err, hash) { - user.password = hash; - next(); - }); - }); -}); - -// password verification -userSchema.methods.comparePassword = function(candidatePassword, cb) { - bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { - if (err) return cb(err); - cb(null, isMatch); - }); -}; -// remember me implementation -userSchema.methods.generateRandomToken = function () { - var user = this, - chars = "_!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", - token = new Date().getTime() + '_'; - for (var x = 0; x < SALT_WORK_FACTOR; x++) { - var i = Math.floor(Math.random() * 94); - token += chars.charAt(i); - } - return token; -}; +/** + * Dependencies + */ -// seed a test user -var User = mongoose.model('User', userSchema); -/* -var usr = new User({ username: 'bob', email: 'bob@example.com', password: 'secret' }); -usr.save(function(err) { - if (err) { - console.log(err); - } else { - console.log('user: ' + usr.username + + 'saved.'); - } -})*/ - -// Passport session setup. -// To support persistent login sessions, Passport needs to be able to -// serialize users into and deserialize users out of the session. Typically, -// this will be as simple as storing the user ID when serializing, and finding -// the user by ID when deserializing. -// -// Both serializer and deserializer edited for Remember Me functionality -passport.serializeUser( function(user, done) { - var createAccessToken = function() { - var token = user.generateRandomToken(); - User.findOne( { accessToken: token }, function (err, existingUser) { - if (err) return done(err); - if (existingUser) { - createAccessToken(); //run it again. has to be unique - } else { - user.set('accessToken', token); - user.save( function(err) { - if (err) return done(err); - return done(null, user.get('accessToken')); - }); - } - }); - } - console.log('serializing user'); - if (user._id) { createAccessToken(); } - else { done(null, user); } -}); - -passport.deserializeUser( function(token, done) { - console.log('deserializing ' + token.provider); - if (token.provider === undefined) { - User.findOne( { accessToken: token }, function(err, user) { - done(err, user); - }); - } else { done(null, token); } -}); - -// Use the LocalStrategy within Passport. -// Strategies in passport require a `verify` function, which accept -// credentials (in this case, a username and password), and invoke a callback -// with a user object. In the real world, this would query a database; -// however, in this example we are using a baked-in set of users. -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 }); } - user.comparePassword(password, function(err, isMatch) { - if (err) return done(err); - if (isMatch) { - return done(null, user); - } else { - return done(null, false, { message: 'Invalid password' }); - } - }); - }); -})); - -// Use the FacebookStrategy within Passport. -// Strategies in Passport require a `verify` function, which accept -// credentials (in this case, an accessToken, refreshToken, and Facebook -// profile), and invoke a callback with a user object. -passport.use(new FacebookStrategy({ - clientID: FACEBOOK_APP_ID, - clientSecret: FACEBOOK_APP_SECRET, - callbackURL: "https://divid.no/auth/facebook/callback" -}, function(accessToken, refreshToken, profile, done) { - // asynchronous verification, for effect... - process.nextTick(function() { - - // To keep the example simple, the user's Facebook profile is returned to - // represent the logged-in user. In a typical application, you would want - // to associate the Facebook account with a user record in your database, - // and return that user instead. - return done(null, profile); - }); - } -)); +var users = require('./controllers/users') + , system = require('./controllers/system'); -// to ensure that users are logged in -function ensureAuthenticated(req, res, next) { - console.log('checking to see if authenticated'); - if (req.isAuthenticated()) return next(); - res.redirect('/login'); -} /* * ============================================================ @@ -164,89 +13,24 @@ function ensureAuthenticated(req, res, next) { * */ +module.exports = function(app, passport, auth) { + app.get('/', system.index); + app.get('/test', system.test); -module.exports = function(app) { - /* - * GET home page. - * - * '/' - */ - - app.get('/', function(req, res){ - res.render('index', { title: 'DERS' }); - }); - - /* - * GET TEST PAGE - * - * '/test' - */ - - app.get('/test', function(req, res) { - res.render('test', { - title: 'test', - loggedin: false - }); - }); - - app.get('/home', function(req, res) { - res.render('home', { - title: 'home', - loggedin: false - }); - }); - - app.get('/faq', function(req, res) { - res.render('faq', { - title: 'faq', - loggedin: false - }); - }); - - /* - * GET dashboard - * - * '/dashboard' - */ + app.get('/home', system.home); - app.get('/dashboard', function(req, res) { - console.log('/dashboard - ' + req.user); - res.render('dashboard', { - title: 'kanin', - loggedin: true - }); - }); + app.get('/faq', system.faq); + app.get('/dashboard', system.dashboard); - /* - * GET login page - * - * '/login' - */ - app.get('/login', function(req, res) { - res.render('login', { title: 'Logg inn' }); - }); + app.get('/login', users.login); - /* POST */ + app.post('/login', users.signin); - app.post('/login', function(req, res, next) { - passport.authenticate('local', function(err, user, info) { - if (err) return next(err); - if (!user) { - console.log(info.message); - req.session.messages = [info.message]; - return res.redirect('/login'); - } - req.logIn(user, function(err) { - if (err) return next(err); - return res.redirect('/dashboard'); - }) - })(req, res, next); - }); // GET /auth/facebook // Use passport.authenticate() as route middleware to authenticate the @@ -267,19 +51,15 @@ module.exports = function(app) { console.log('/auth/facebook/callback --- ' + req.user.username); res.redirect('/dashboard'); }); - - - + app.get('/auth/twitter', passport.authenticate('twitter', { failureRedirect: '/login' }), users.signin); + app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/login' }), users.authCallback); /* * GET logout * * '/logout' */ - app.get('/logout', function(req, res) { - req.logout(); - res.redirect('/test'); - }); + app.get('/logout', users.logout); @@ -290,11 +70,7 @@ module.exports = function(app) { * '/project' */ - app.get('/project', function(req, res) { - res.render('project', { title: 'Harepus', loggedin: true }); - }) - - + app.get('/project', system.project); @@ -304,46 +80,12 @@ module.exports = function(app) { * '/signup' */ - app.get('/signup', function(req, res) { - res.render('signup', { title: 'Registrer deg' }); - }); + app.get('/signup', users.signup); /* POST */ - app.post('/signup', function(req, res) { - AM.addNewAccount({ - name : req.param('name'), - email : req.param('email'), - user : req.param('user'), - pass : req.param('pass'), - country : req.param('country') - }, function(e) { - if (e) { - res.send(e, 400); - } else { - res.send('ok', 200); - } - }); - }); - - - - - - /* - * ERRORS - */ - - /* 404 */ - app.get('*', function(req, res) { - res.render('error', { title: '404', text: 'Fant ikke siden' }); - }); - - /* 403 on POST */ - app.post('*', function(req, res) { - res.render('error', { title: '403', text: 'Du har ikke tilgang til denne siden' }); - }); + app.post('/signup', users.create); }; diff --git a/views/navbar.ejs b/views/navbar.ejs index aac3c2e..79cb55a 100644 --- a/views/navbar.ejs +++ b/views/navbar.ejs @@ -30,7 +30,9 @@ </label> </div> <div class="span5"> - <button type="submit" class="btn btn-inverse btn-small">Logg inn</button><a href="/auth/facebook">f</a> + <button type="submit" class="btn btn-inverse btn-small">Logg inn</button> + <a href="/auth/facebook">f</a> + <a href="/auth/twitter">t</a> </div> </div> </form> diff --git a/views/signup.ejs b/views/signup.ejs index b64845b..01a5831 100644 --- a/views/signup.ejs +++ b/views/signup.ejs @@ -22,265 +22,18 @@ <input id="email-tf" type="text" name="email" class="input-xlarge"> </div> </div> - <div id="clist-cg" class="control-group"> - <label for="country-list" class="control-label">Location</label> - <div class="controls"> - <select id="country-list" name="country"> - <option>Please select a country</option> - <option>Afghanistan</option> - <option>Albania</option> - <option>Algeria</option> - <option>American Samoa</option> - <option>Andorra</option> - <option>Angola</option> - <option>Anguilla</option> - <option>Antarctica</option> - <option>Antigua and Barbuda</option> - <option>Argentina</option> - <option>Armenia</option> - <option>Aruba</option> - <option>Australia</option> - <option>Austria</option> - <option>Azerbaijan</option> - <option>Bahamas</option> - <option>Bahrain</option> - <option>Bangladesh</option> - <option>Barbados</option> - <option>Belarus</option> - <option>Belgium</option> - <option>Belize</option> - <option>Benin</option> - <option>Bermuda</option> - <option>Bhutan</option> - <option>Bolivia</option> - <option>Bosnia and Herzegowina</option> - <option>Botswana</option> - <option>Bouvet Island</option> - <option>Brazil</option> - <option>British Indian Ocean Territory</option> - <option>Brunei Darussalam</option> - <option>Bulgaria</option> - <option>Burkina Faso</option> - <option>Burundi</option> - <option>Cambodia</option> - <option>Cameroon</option> - <option>Canada</option> - <option>Cape Verde</option> - <option>Cayman Islands</option> - <option>Central African Republic</option> - <option>Chad</option> - <option>Chile</option> - <option>China</option> - <option>Christmas Island</option> - <option>Cocos (Keeling) Islands</option> - <option>Colombia</option> - <option>Comoros</option> - <option>Congo</option> - <option>Congo, the Democratic Republic of the</option> - <option>Cook Islands</option> - <option>Costa Rica</option> - <option>Cote d'Ivoire</option> - <option>Croatia (Hrvatska)</option> - <option>Cuba</option> - <option>Cyprus</option> - <option>Czech Republic</option> - <option>Denmark</option> - <option>Djibouti</option> - <option>Dominica</option> - <option>Dominican Republic</option> - <option>East Timor</option> - <option>Ecuador</option> - <option>Egypt</option> - <option>El Salvador</option> - <option>Equatorial Guinea</option> - <option>Eritrea</option> - <option>Estonia</option> - <option>Ethiopia</option> - <option>Falkland Islands (Malvinas)</option> - <option>Faroe Islands</option> - <option>Fiji</option> - <option>Finland</option> - <option>France</option> - <option>France, Metropolitan</option> - <option>French Guiana</option> - <option>French Polynesia</option> - <option>French Southern Territories</option> - <option>Gabon</option> - <option>Gambia</option> - <option>Georgia</option> - <option>Germany</option> - <option>Ghana</option> - <option>Gibraltar</option> - <option>Greece</option> - <option>Greenland</option> - <option>Grenada</option> - <option>Guadeloupe</option> - <option>Guam</option> - <option>Guatemala</option> - <option>Guinea</option> - <option>Guinea-Bissau</option> - <option>Guyana</option> - <option>Haiti</option> - <option>Heard and Mc Donald Islands</option> - <option>Holy See (Vatican City State)</option> - <option>Honduras</option> - <option>Hong Kong</option> - <option>Hungary</option> - <option>Iceland</option> - <option>India</option> - <option>Indonesia</option> - <option>Iran (Islamic Republic of)</option> - <option>Iraq</option> - <option>Ireland</option> - <option>Israel</option> - <option>Italy</option> - <option>Jamaica</option> - <option>Japan</option> - <option>Jordan</option> - <option>Kazakhstan</option> - <option>Kenya</option> - <option>Kiribati</option> - <option>Korea, Democratic People's Republic of</option> - <option>Korea, Republic of</option> - <option>Kuwait</option> - <option>Kyrgyzstan</option> - <option>Lao People's Democratic Republic</option> - <option>Latvia</option> - <option>Lebanon</option> - <option>Lesotho</option> - <option>Liberia</option> - <option>Libyan Arab Jamahiriya</option> - <option>Liechtenstein</option> - <option>Lithuania</option> - <option>Luxembourg</option> - <option>Macau</option> - <option>Macedonia, The Former Yugoslav Republic of</option> - <option>Madagascar</option> - <option>Malawi</option> - <option>Malaysia</option> - <option>Maldives</option> - <option>Mali</option> - <option>Malta</option> - <option>Marshall Islands</option> - <option>Martinique</option> - <option>Mauritania</option> - <option>Mauritius</option> - <option>Mayotte</option> - <option>Mexico</option> - <option>Micronesia, Federated States of</option> - <option>Moldova, Republic of</option> - <option>Monaco</option> - <option>Mongolia</option> - <option>Montserrat</option> - <option>Morocco</option> - <option>Mozambique</option> - <option>Myanmar</option> - <option>Namibia</option> - <option>Nauru</option> - <option>Nepal</option> - <option>Netherlands</option> - <option>Netherlands Antilles</option> - <option>New Caledonia</option> - <option>New Zealand</option> - <option>Nicaragua</option> - <option>Niger</option> - <option>Nigeria</option> - <option>Niue</option> - <option>Norfolk Island</option> - <option>Northern Mariana Islands</option> - <option>Norway</option> - <option>Oman</option> - <option>Pakistan</option> - <option>Palau</option> - <option>Panama</option> - <option>Papua New Guinea</option> - <option>Paraguay</option> - <option>Peru</option> - <option>Philippines</option> - <option>Pitcairn</option> - <option>Poland</option> - <option>Portugal</option> - <option>Puerto Rico</option> - <option>Qatar</option> - <option>Reunion</option> - <option>Romania</option> - <option>Russian Federation</option> - <option>Rwanda</option> - <option>Saint Kitts and Nevis</option> - <option>Saint LUCIA</option> - <option>Saint Vincent and the Grenadines</option> - <option>Samoa</option> - <option>San Marino</option> - <option>Sao Tome and Principe</option> - <option>Saudi Arabia</option> - <option>Senegal</option> - <option>Seychelles</option> - <option>Sierra Leone</option> - <option>Singapore</option> - <option>Slovakia (Slovak Republic)</option> - <option>Slovenia</option> - <option>Solomon Islands</option> - <option>Somalia</option> - <option>South Africa</option> - <option>South Georgia and the South Sandwich Islands</option> - <option>Spain</option> - <option>Sri Lanka</option> - <option>St. Helena</option> - <option>St. Pierre and Miquelon</option> - <option>Sudan</option> - <option>Suriname</option> - <option>Svalbard and Jan Mayen Islands</option> - <option>Swaziland</option> - <option>Sweden</option> - <option>Switzerland</option> - <option>Syrian Arab Republic</option> - <option>Taiwan, Province of China</option> - <option>Tajikistan</option> - <option>Tanzania, United Republic of</option> - <option>Thailand</option> - <option>Togo</option> - <option>Tokelau</option> - <option>Tonga</option> - <option>Trinidad and Tobago</option> - <option>Tunisia</option> - <option>Turkey</option> - <option>Turkmenistan</option> - <option>Turks and Caicos Islands</option> - <option>Tuvalu</option> - <option>Uganda</option> - <option>Ukraine</option> - <option>United Arab Emirates</option> - <option>United Kingdom</option> - <option>United States</option> - <option>United States Minor Outlying Islands</option> - <option>Uruguay</option> - <option>Uzbekistan</option> - <option>Vanuatu</option> - <option>Venezuela</option> - <option>Viet Nam</option> - <option>Virgin Islands (British)</option> - <option>Virgin Islands (U.S.)</option> - <option>Wallis and Futuna Islands</option> - <option>Western Sahara</option> - <option>Yemen</option> - <option>Yugoslavia</option> - <option>Zambia</option> - <option>Zimbabwe</option> - </select> - </div> - </div> <hr> <p id="sub2" class="subheading"></p> <div id="user-cg" class="control-group"> <label for="user-tf" class="control-label">Username</label> <div class="controls"> - <input id="user-tf" type="text" name="user" class="input-xlarge disabled"> + <input id="user-tf" type="text" name="username" class="input-xlarge disabled"> </div> </div> <div id="pass-cg" class="control-group"> <label for="pass-tf" class="control-label">Password</label> <div class="controls"> - <input id="pass-tf" type="password" name="pass" value="" class="input-xlarge"> + <input id="pass-tf" type="password" name="password" value="" class="input-xlarge"> </div> </div> <div class="form-actions"> |