From 4dd7550b87386114ba4e86be17afbaa4d3fa11d6 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Thu, 25 Apr 2013 02:12:34 +0200 Subject: addedpassport config file and did some other stuff --- router.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'router.js') diff --git a/router.js b/router.js index cf3bd51..0750bdf 100644 --- a/router.js +++ b/router.js @@ -6,8 +6,6 @@ var passport = require('passport') , bcrypt = require('bcrypt') , SALT_WORK_FACTOR = 15; -var FACEBOOK_APP_ID = "504825706245603"; -var FACEBOOK_APP_SECRET = "e5ea0faed85d8749cafd38732530ef35"; // connects to mongodb mongoose.connect('localhost', 'test'); @@ -135,9 +133,9 @@ passport.use(new LocalStrategy(function(username, password, done) { // 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" + clientID: config.facebook.clientID, + clientSecret: config.facebook.clientSecret, + callbackURL: config.facebook.callbackURL }, function(accessToken, refreshToken, profile, done) { // asynchronous verification, for effect... process.nextTick(function() { -- cgit v1.2.3 From a0dbc1b3b71acc340d1099b8711d411128ee17f7 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Thu, 25 Apr 2013 17:00:26 +0200 Subject: removed some passport classes that's going in config/passport.js --- router.js | 42 ------------------------------------------ 1 file changed, 42 deletions(-) (limited to 'router.js') diff --git a/router.js b/router.js index 0750bdf..9b6744f 100644 --- a/router.js +++ b/router.js @@ -1,6 +1,4 @@ var passport = require('passport') - , LocalStrategy = require('passport-local').Strategy - , FacebookStrategy = require('passport-facebook').Strategy , mongodb = require('mongodb') , mongoose = require('mongoose') , bcrypt = require('bcrypt') @@ -108,46 +106,6 @@ passport.deserializeUser( function(token, done) { } 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: config.facebook.clientID, - clientSecret: config.facebook.clientSecret, - callbackURL: config.facebook.callbackURL -}, 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); - }); - } -)); // to ensure that users are logged in function ensureAuthenticated(req, res, next) { -- cgit v1.2.3 From fc50b5b7f18d02ffa8056dc1edbd3bd43e973493 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Mon, 29 Apr 2013 03:39:15 +0200 Subject: been cleaning a bit, moving stuff to where it belongs. signup and signin works. --- router.js | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-) (limited to 'router.js') diff --git a/router.js b/router.js index 9b6744f..afb64e5 100644 --- a/router.js +++ b/router.js @@ -1,18 +1,16 @@ -var passport = require('passport') - , mongodb = require('mongodb') - , mongoose = require('mongoose') - , bcrypt = require('bcrypt') - , SALT_WORK_FACTOR = 15; +var mongodb = require('mongodb') + , mongoose = require('mongoose'); +var users = require('./controllers/users'); // connects to mongodb -mongoose.connect('localhost', 'test'); +//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 }, @@ -76,6 +74,7 @@ usr.save(function(err) { // 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(); @@ -113,7 +112,7 @@ function ensureAuthenticated(req, res, next) { if (req.isAuthenticated()) return next(); res.redirect('/login'); } - +/* /* * ============================================================ * Routes @@ -122,7 +121,7 @@ function ensureAuthenticated(req, res, next) { -module.exports = function(app) { +module.exports = function(app, passport, auth) { /* * GET home page. * @@ -183,10 +182,11 @@ module.exports = function(app) { /* POST */ - app.post('/login', function(req, res, next) { + 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'); @@ -196,7 +196,7 @@ module.exports = function(app) { return res.redirect('/dashboard'); }) })(req, res, next); - }); + });*/ // GET /auth/facebook // Use passport.authenticate() as route middleware to authenticate the @@ -217,9 +217,8 @@ 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 @@ -261,22 +260,7 @@ module.exports = function(app) { /* 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); - } - }); - }); - + app.post('/signup', users.create); -- cgit v1.2.3 From c1371f712789e97fac6351daee93a048b12db69b Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Mon, 29 Apr 2013 04:13:18 +0200 Subject: cleaned up router.js --- router.js | 218 ++++---------------------------------------------------------- 1 file changed, 11 insertions(+), 207 deletions(-) (limited to 'router.js') diff --git a/router.js b/router.js index afb64e5..72acd85 100644 --- a/router.js +++ b/router.js @@ -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' }); - }); - - }; -- cgit v1.2.3 From 53c89fd5bb00ed6c8100facb2a0ddcc54b8b9bb7 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Mon, 29 Apr 2013 04:27:33 +0200 Subject: cleaning --- router.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'router.js') diff --git a/router.js b/router.js index 72acd85..a9d9c90 100644 --- a/router.js +++ b/router.js @@ -1,14 +1,18 @@ + +/** + * Dependencies + */ + var users = require('./controllers/users') , system = require('./controllers/system'); + /* * ============================================================ * Routes * */ - - module.exports = function(app, passport, auth) { app.get('/', system.index); -- cgit v1.2.3