2

UPDATE: Using a different path for my successRedirect I have managed to get a 200 response to my GET request (see code below). This is ONLY true the FIRST TIME I hit the route, though. Even so, the browser does not change the page rendered. i.e. after I successfully register a dummy user, there is no actual redirect.

After some more research about code 304 and some playing it seems like a caching issue, but that's as far as I've gotten thus far.

GET /test

app.get('/test', function(req, res) {
        res.render('video-view.html');
    });

    // process the signup form
    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect : '/test', // redirect to the secure profile section
        failureRedirect : '/sign-up', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

I'm having a lot of trouble getting my router to properly redirect after a successful local signup. My /config/passport.js file properly adds a row to the database and appears to serialize the session. It returns the user to my router, which attempts to redirect the browser. Looking at my console requests, I see this unsuccessful request: GET /home 304.

I've been working at this for quite a while now and haven't been able to find a solution or fix the problem. Part of my issue is that I'm not using a templating engine - just serving html scripted with angular.js. Maybe I haven't configured this properly, but all other routing and functionality is working well. Any thoughts?

Here is my router code:

var express = require('express');

module.exports = function(app, passport) {

    app.get('/signup', function(req, res) {
        // render the page and pass in any flash data if it exists
        res.render('sign-up.html');
    });

    app.get('/home', function(req, res) {
        res.render('video-view.html');
    });

    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect : '/home', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

};

My server is set up like this:

var express  = require('express');
var app      = express();
var port     = process.env.PORT || 8080;
var passport = require('passport');
var flash    = require('connect-flash');

require('./config/passport')(passport); // pass passport for configuration

app.configure(function() {
    app.use(express.logger('dev')); // log every request to the console
    app.use(express.cookieParser()); // read cookies (needed for auth)
    app.use(express.bodyParser()); // get information from html forms
    app.engine('html', require('ejs').renderFile);
    app.use('/', express.static(__dirname + '/views'));

    app.use(express.session({ secret: 'vidyapathaisalwaysrunning' } )); // session secret
    app.use(passport.initialize());
    app.use(passport.session()); // persistent login sessions
    app.use(flash()); // use connect-flash for flash messages stored in session
});

require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

app.listen(port);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

3

Resolved: Issue was caused by code in the view, surprisingly.

Here is my technique before and after:

Before using Angular.js below.
sign-up.html

<div class="col-xs-6" id="register">
    <p> Enter Username </p>
    <input type="text" ng-model="username"/>
    <p> Enter Password </p>
    <input type="text" ng-model="password"/>
    <input type="button" ng-click="register()">
</div>

controller.js

$scope.register = function(){
    var send = {'username': $scope.username,
                'password': $scope.password};
    $http.post('/signup', send);
};

I believe the reason this didn't work is that the browser did not know to wait for a response from node. Not positive but my best guess is that this problem could be solved with promises in Angular.

I solved the redirect issue by leaving angular out of the equation and handling the POST with a form in html. Here is the working code with no changes to server-side:

sign-up.html

 <form action="/signup" method="post"> 
     <input type="text" name="username" />
     <input type="text" name="password" />
     <button type="submit" id="enter" value="ENTER" />
 </form>
  • Your problem is that you expect browser to redirect when you login with AJAX. That does not happen. The AJAX call will follow the redirect and receives the rendered HTML page, and most likely there's an error but you have no error handling in `$http.post()` so it won't show. Check this for more information http://stackoverflow.com/questions/26019995/how-to-use-jquery-ajax-data-for-passportjs/26032067#26032067 – vesse Oct 17 '14 at 04:39
0

I don't think get home 304 is necessarily an error. It's telling you that it's getting the page but it hasn't changed I believe. The issue I think is what you're sending back. I'm not familiar with how ejs works, but are you just trying to render an html page? Then you should be using sendFile I believe, not render.

15 Oct 01:25:24 - [nodemon] starting `node ./bin/www`
GET / 200 826.199 ms - 11667
GET /js/lib/bootstrap.min.js 304 3.557 ms - -
GET /js/lib/metisMenu.min.js 304 2.810 ms - -
GET /js/lib/morris.min.js 304 0.607 ms - -
GET /js/lib/raphael.min.js 304 1.195 ms - -
GET /js/lib/sb-admin-2.js 304 0.522 ms - -
GET /js/lib/angular/angular-bootstrap.min.js 304 1.334 ms - -
GET /js/app/app.js 304 2.389 ms - -
GET / 200 366.541 ms - 11667
GET /css/bootstrap/bootstrap.min.css 304 1.078 ms - -
GET /css/mm/metisMenu.min.css 304 1.264 ms - -
GET /css/sb-admin-2.css 304 0.955 ms - -
GET /css/morris.css 304 0.739 ms - -
GET /css/font-awesome.min.css 304 1.277 ms - -
....

This is showing that it's getting the route okay. /. it's all good. / route now says get all these js files while it renders the html (which just resides at /). Those are all 304 because they never change. Chances are you have an error in the way you're sending the file.

dkran
  • 284
  • 2
  • 4
  • 16