Sunday, 4 June 2017

Cannot POST signup form, 404 error using Passport and Sequelize (Node)

I'm trying to get authentication to work with passport. Every time that I post a filled in sign-up form I get a 404 error back on the succesRedirect.

Also, when I try to go to a page that requires an active session after sign-up the page stays on loading forever. Here's my code:

./routes/auth.js. Note that the failureRedirect works (checked by trying to create duplicate user):

  app.post(
    '/signup',
    passport.authenticate('local-signup', {
      succesRedirect: '/userprofile',

      failureRedirect: '/signup',
    })
  );

My passport strategy for signing up in ./config/passport.js

  passport.use(
    'local-signup',
    new LocalStrategy(
      {
        usernameField: 'email',
        passwordField: 'password',
        passReqToCallback: true
      },
      (req, email, password, done) => {
        const generateHash = password => bCrypt.hashSync(password, bCrypt.genSaltSync(8), null);
        // create hashed password

        User.findOne({
          where: { email }
        }).then(user => {
          if (user) {
            return done(null, false, {
              message: 'That email is already taken'
            });
          }

          const userPassword = generateHash(password); // store hash
          const data = {
            // data for sequelize model
            email,
            password: userPassword
          };

          // creates user, callback(user) if user exists or false if something went wrong
          User.create(data).then(newUser => (!newUser ? done(null, false) : done(null, newUser)));
        });
      }
    )
  );

And finally, the errors in my console (note that user creation works just fine):

Executing (default): SELECT "id", "username", "email", "password", "createdAt", "updatedAt" FROM "users" AS "user" WHERE "user"."email" = 'testuser@gmail.com' LIMIT 1;
Executing (default): INSERT INTO "users" ("id","email","password","createdAt","updatedAt") VALUES (DEFAULT,'testuser@gmail.com','$2a$08$Ob0Vu6f4cqa7C3cyGSPFcevb2o1Kwg1EANTlJRy3MU.qW456aEVfW','2017-06-04 20:33:23.578 +00:00','2017-06-04 20:33:23.578 +00:00') RETURNING *;
POST /signup 404 256.770 ms - 146

I'm at a loss at how I should debug this (using failureFlash doesn't give me any hints beside a 404 either). I have a feeling there's something going wrong with the authentication part/sessions, but I'm unsure. Hope someone can help me out!



via Rishi Speets

No comments:

Post a Comment