W
W
Wasya UK2018-06-17 22:28:03
Node.js
Wasya UK, 2018-06-17 22:28:03

Why is there no redirect during authorization?

The code seems to be working, but there is no redirect to the main one, although it is clear that serializeUser works:
PASSPORT.JS

passport.serializeUser((user, done) => {
  console.log("USER:", user);
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  console.log(id);
  User.findOne({
    where: {id: id}
  })
  .then(({dataValues: user}) => {
    done(null, user);
  })
  .catch(err => {
    done(err, null);
  });
});

passport.use(new LocalStrategy((username, password, done) => {
  console.log(username)
  User.findOne({
    where: {username: username}
  })
  .then(({dataValues: user}) => {
    if (!user) {
      return done(null, false, { message: 'Incorrect username.' });
    }

    User.validPassword(password, user.password)
      .then(isMatch => {
        if (isMatch) {
          return done(null, user);
        } else {
          return done(null, false, { message: 'Incorrect password.' });
        }
      })
  })
  .catch(done);
}));

module.exports = passport;

ROUTING
app.get('/login', (req, res) => {
  res.set('Content-Type', 'text/html');
  res.render('create_user.pug');
});

app.post('/login', (req, res, next) => {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }

    if (!user) {
      console.log("REDIRECT", 'login')
      return res.redirect('/login');
    }

    req.logIn(user, function(err) {
      if (err) { return next(err); }

      console.log("REDIRECT", user)
      return res.redirect('/');
    });
  })(req, res, next);
});

// all
app.get('/*', (req, res, next) => {
  if (!req.isAuthenticated()) {
    res.redirect('/login');
  } else {
    next();
  }
});

require('./routes/index')(app);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jumandjilos, 2018-06-17
@Jumandjilos

Come on, first explain how you understand what the res.redirect method does?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question