Created
June 3, 2011 14:44
-
-
Save Migrate/1006435 to your computer and use it in GitHub Desktop.
Problem with mongoose-auth and connect-form
This file contains 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 express = require('express'), | |
extras = require('express-extras'), | |
Form = require('connect-form'), | |
Step = require('step'), | |
path = require('path'), | |
mongoose = require('mongoose'), | |
mongooseTypes = require("mongoose-types"), | |
everyauth = require('everyauth'), | |
Promise = everyauth.Promise, | |
mongooseAuth = require('mongoose-auth'); | |
var app = express.createServer(); | |
// Configuration | |
app.configure(function(){ | |
app.use(express.methodOverride()); | |
app.use(express.bodyParser()); | |
// Need to put connect-form *after* bodyParser. | |
//http://groups.google.com/group/express-js/browse_thread/thread/a552d29f7d5fdc81 | |
//https://gist.github.com/906731 | |
app.use(Form()); | |
app.use(express.cookieParser()); | |
app.use(express.session({ secret: '....', store: new MongoStore({db: "....." })})); | |
app.use(mongooseAuth.middleware()); | |
app.use(app.router); | |
app.set('view engine', 'jade'); | |
}); | |
function requiresLogin(req, res, next) { | |
if(!req.loggedIn) { | |
res.redirect('/login?redir=' + req.url); | |
return; | |
} else { | |
next(); | |
} | |
} | |
function change_image(req, res, next) { | |
if (!req.form) { | |
next(); | |
} | |
else { | |
// connect-form adds the req.form object | |
// we can (optionally) define onComplete, passing | |
// the exception (if any) fields parsed, and files parsed | |
req.form.complete(function(err, fields, files){ | |
console.log("req.form.complete ***"); | |
if (err) { | |
next(err); | |
} else { | |
console.log('\nuploaded %s to %s' | |
, files.image.filename | |
, files.image.path); | |
var source_file = files.image.path; | |
} | |
// We can add listeners for several form | |
// events such as "progress" | |
req.form.on('progress', function(bytesReceived, bytesExpected){ | |
var percent = (bytesReceived / bytesExpected * 100) | 0; | |
process.stdout.write('Uploading: %' + percent + '\r'); | |
}); | |
}); | |
} | |
} | |
app.post('/account/image/:id', requiresLogin, change_image); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment