diff options
-rw-r--r-- | app.js | 6 | ||||
-rw-r--r-- | controllers/system.js | 62 | ||||
-rw-r--r-- | controllers/users.js | 15 | ||||
-rw-r--r-- | router.js | 218 |
4 files changed, 88 insertions, 213 deletions
@@ -21,7 +21,11 @@ var port = process.env.PORT || 8000 // 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'); +}); // Bootstrap models var models_path = __dirname + '/models'; 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 index af454b5..d465468 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -9,7 +9,6 @@ var mongoose = require('mongoose') /** * Login */ - exports.login = function(req, res) { res.render('login', { title: 'Login' @@ -21,8 +20,7 @@ exports.login = function(req, res) { /** * Logout */ - -exports.logoug = function(req, res) { +exports.logout = function(req, res) { req.logout(); res.resirect('/test'); } @@ -30,14 +28,21 @@ exports.logoug = function(req, res) { /** * Signin + * This is triggered when the user post to /login */ - exports.signin = function(req, res) {} + /** - * Create users + * 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'; @@ -1,118 +1,6 @@ -var mongodb = require('mongodb') - , mongoose = require('mongoose'); - -var users = require('./controllers/users'); - -// 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; -}; +var users = require('./controllers/users') + , system = require('./controllers/system'); -// 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); } -}); - - -// 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'); -} -/* /* * ============================================================ * Routes @@ -122,82 +10,24 @@ function ensureAuthenticated(req, res, next) { module.exports = function(app, passport, auth) { - /* - * GET home page. - * - * '/' - */ + app.get('/', system.index); - app.get('/', function(req, res){ - res.render('index', { title: 'DERS' }); - }); + app.get('/test', system.test); - /* - * GET TEST PAGE - * - * '/test' - */ + app.get('/home', system.home); - 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 - }); - }); - /* - * GET dashboard - * - * '/dashboard' - */ + app.get('/dashboard', system.dashboard); - app.get('/dashboard', function(req, res) { - console.log('/dashboard - ' + req.user); - res.render('dashboard', { - title: 'kanin', - loggedin: true - }); - }); + app.get('/login', users.login); - /* - * GET login page - * - * '/login' - */ - - app.get('/login', function(req, res) { - res.render('login', { title: 'Logg inn' }); - }); + app.post('/login', users.signin); - /* POST */ - - app.post('/login', users.signin);/* function(req, res, next) { - passport.authenticate('local', function(err, user, info) { - if (err) return next(err); - if (!user) { - console.log('post/login'); - 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 // request. The first step in Facebook authentication will involve @@ -225,10 +55,7 @@ module.exports = function(app, passport, auth) { * * '/logout' */ - app.get('/logout', function(req, res) { - req.logout(); - res.redirect('/test'); - }); + app.get('/logout', users.logout); @@ -239,11 +66,7 @@ module.exports = function(app, passport, auth) { * '/project' */ - app.get('/project', function(req, res) { - res.render('project', { title: 'Harepus', loggedin: true }); - }) - - + app.get('/project', system.project); @@ -253,9 +76,7 @@ module.exports = function(app, passport, auth) { * '/signup' */ - app.get('/signup', function(req, res) { - res.render('signup', { title: 'Registrer deg' }); - }); + app.get('/signup', users.signup); /* POST */ @@ -263,21 +84,4 @@ module.exports = function(app, passport, auth) { app.post('/signup', users.create); - - - /* - * 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' }); - }); - - }; |