Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running middleware before uploading #21

Open
AyKarsi opened this issue May 16, 2013 · 1 comment
Open

Running middleware before uploading #21

AyKarsi opened this issue May 16, 2013 · 1 comment

Comments

@AyKarsi
Copy link

AyKarsi commented May 16, 2013

Before I let a user upload files, I want to perform an authentication check.

The uploading works fine if I just use the middleware like this:

app.use('/upload/blogs', function (req, res, next) {
    upload.fileHandler({
        uploadDir: function () {
            return __dirname + '\\public\\uploads\\blogs\\';
        },
        uploadUrl: function () {
            return '/uploads/blogs';
        }
    })(req, res, next);
});

If I add any sort of async middlerware prior to above code, the upload does nothing. e.g. adding the following use before the above code:

app.use(function (req, res, next) {
    var token = req.headers["x-authenticationtoken"];
    if (token == null)
        return next();
    tokenRepository.getUserByToken(token, function (result) {
            if (result.error != null) {
                req.currentUser = null;
                console.log("Not logged in");
            }
            else {
                req.currentUser = result.data;
                console.log("user authenitcated");
            }
            next();
        }
    );
});

If I comment out the async part like this, it will work:

app.use(function (req, res, next) {
//    var token = req.headers["x-authenticationtoken"];
//    if (token == null)
//        return next();
//    tokenRepository.getUserByToken(token, function (result) {
//            if (result.error != null) {
//                req.currentUser = null;
//                console.log("Not logged in");
//            }
//            else {
//                req.currentUser = result.data;
//                console.log("user authenitcated");
//            }
//            next();
//        }
//    );
    next();
});

Any ideas on what I'm doing wrong?

@mogadget
Copy link

there's no route specified on your code
app.use(function (req, res, next) {

instead, try this approach

app.use('/upload/blogs', function (req, res, next) { var token = req.headers["x-authenticationtoken"]; if (token == null) return next(); tokenRepository.getUserByToken(token, function (result) { upload.fileHandler({ uploadDir: function () { return __dirname + '\\public\\uploads\\blogs\\'; }, uploadUrl: function () { return '/uploads/blogs'; } })(req, res, next); } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants