Express.js redirect user after sign up

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tdrsam
    New Member
    • May 2015
    • 97

    Express.js redirect user after sign up

    I can't find an answer to this: What do I do after a new user signs up to my express.js app?

    I have a view, and this in the routes/index file that correspnds:
    Code:
    router.get('/users/firstSignIn', function(req, res, next) {
      res.render('uesrs/firstSignIn', { title: 'title' });
    });

    And this signup function in my users controller:
    Code:
    exports.create = function(req, res, password) {
    
      if(req.method.toLowerCase() != "post") {
        res.render("signup.jade", {layout: false});
      }
      else {
         new user(req.body).save();
    	 res.redirect("/users/firstSignIn");
    	 //res.send("ok");  // USE THIS TO REDIRECT AFTER SIGNUP
      }
    }

    This is in my app.js:
    Code:
    app.get('/users/create', users.create);
    app.post('/users/create', users.create);

    When I sign up a new user, I can't figure out how to redirect them. Am I going about this all wrong, or I just need to add to my controller?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you use two different functions for the get and post route, not one function that does it all.

    if you want to use export, then you need to export two functions, not one:

    Code:
    exports.create = {
        get: function(req, res, next) {},
        post: function(req, res, next) {}
    };
    Code:
    app.get('/users/create', users.create.get);
    app.post('/users/create', users.create.post);

    Comment

    • tdrsam
      New Member
      • May 2015
      • 97

      #3
      I'm not sure if it's just my system (my work computer is pretty ordinary), but I changed it to this:

      Code:
      exports.create = {
      
      	post: function(req, res, next) { // password
      
      		if(req.method.toLowerCase() != "post") {
      		res.render("signup.jade", {layout: false});
      		}
      	
      		else {
      			new user(req.body).save();
      		}
      	},
      	
      	get: function(req, res, next){
      		res.redirect("/users/firstSignIn");
      	}
      	
      }
      And it's doing some strange things. It's posting the new user to the Db, but it's adding the user twice. It's also not loading the new page (/users/firstSignIn). The browser crashes with an error message saying: "the localhost page isn't working. localhost didn't send any data"

      But when I check the db the data is there, as I said twice.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        first, if(req.method.t oLowerCase() != "post") is pointless as the request method of a POST route is always POST. second, you never send a response after inserting data into the db.

        Comment

        • tdrsam
          New Member
          • May 2015
          • 97

          #5
          That was correct. The function now looks like this:

          Code:
          exports.create = {
            post: function(req, res, next) {
              new user(req.body).save();
              res.redirect("/users/firstSignIn");
            },
            get: function(req, res, next){
              res.redirect("/users/firstSignIn");
            }
          }
          Now I just need to customise it.

          Comment

          Working...