Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS passport dosen';本地身份验证后无法重定向_Node.js_Passport.js - Fatal编程技术网

Node.js NodeJS passport dosen';本地身份验证后无法重定向

Node.js NodeJS passport dosen';本地身份验证后无法重定向,node.js,passport.js,Node.js,Passport.js,我的NodeJS应用程序有问题。我使用Passport进行身份验证,它似乎可以工作,因为如果没有经过身份验证的会话,就无法访问受保护的路由。唯一的问题是/login路由在sussessfull登录后没有重定向。我必须手工做。 我读了一些关于它的帖子,人们认为这可能是由于这一行代码中没有出现,但我已经知道了: })(req, res, next); 完整的代码如下所示: exports.postLogin = (req, res, next) => { req.assert('emai

我的NodeJS应用程序有问题。我使用Passport进行身份验证,它似乎可以工作,因为如果没有经过身份验证的会话,就无法访问受保护的路由。唯一的问题是/login路由在sussessfull登录后没有重定向。我必须手工做。 我读了一些关于它的帖子,人们认为这可能是由于这一行代码中没有出现,但我已经知道了:

})(req, res, next);
完整的代码如下所示:

exports.postLogin = (req, res, next) => {   req.assert('email', 'Email is not valid').isEmail();   req.assert('password', 'Password cannot be blank').notEmpty();   req.sanitize('email').normalizeEmail({ gmail_remove_dots: false });

  const errors = req.validationErrors();

  if (errors) {
    req.flash('errors', errors);
    return res.redirect('/login');   }

  passport.authenticate('local', (err, user, info) => {
    if (err) { 
      console.log("Error");
      return next(err); 
    }
    if (!user) {
      console.log("Error again");
      req.flash('errors', info);
      return res.redirect('/login');
    }
    req.logIn(user, (err) => {
      console.log("logged in");
      if (err) { 
        console.log("Woops not: "+err);
        return next(err); 
      }
      req.flash('success', { msg: 'Success! You are logged in.' });
      res.redirect(req.session.returnTo || '/');
    });   })(req, res, next); };
和我的护照配置:

passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
  console.log("Email fra Passport: "+email);
  console.log("Password fra Passport: "+password);
  let pwdhash = bcrypt.hashSync(password, 10);

    const client = pool.connect();

pool.connect((err, client, release) => {
    if (err) {
      return console.error('Error acquiring client', err.stack)
    }
    client.query('SELECT * FROM "user" WHERE "email"=$1',[email], (err, result) => {
      release()
      if (err) {
        return console.error('Error executing query', err.stack)
      }
      if(result.rows[0] == null){
        console.log("Oops. Incorrect login details");
        //req.flash('danger', "Oops. Incorrect login details.");
        return done(null, false);
      }
      else{
        bcrypt.compare(password, result.rows[0].password, function(err, check) {
        console.log(pwdhash);
        console.log(result.rows[0].password);
        if (err){
          console.log('Error while checking password: '+err);
          return done(null, false, { msg: 'Password did not match.' });
        }
        else if (check){
          console.log("All good");
          return done(null, [{email: result.rows[0].email, firstName: result.rows[0].firstName}]);
        }
        else{
          console.log("Invalid email or password: "+check);
          return done(null, false, { msg: 'Invalid email or password.' });
        }
        });
      }
      console.log(result)
    })
  })
}));
为什么我的代码不会重定向到:

res.redirect(req.session.returnTo || '/');
更新:

更新了从app.js到静态内容的路由和用户路由:

app.use('/', express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));
app.use('/public', express.static(path.join(__dirname, 'public/'), { maxAge: 31557600000 }));
app.use('/views', express.static(path.join(__dirname, 'views/'), { maxAge: 31557600000 }));
app.use('/modules', express.static(path.join(__dirname, 'node_modules/'), { maxAge: 31557600000 }));
//Handling specific pathts due to the changes from local modules to node_modules, we need this:
app.use('/webfonts', express.static(path.join(__dirname, 'node_modules/@fortawesome/fontawesome-free/webfonts'), { maxAge: 31557600000 }));
app.use('/public/css/fonts/@fortawesome', express.static(path.join(__dirname, 'node_modules/@fortawesome/fontawesome-free/webfonts'), { maxAge: 31557600000 }));
app.use('/public/css/fonts/flaticon', express.static(path.join(__dirname, 'public/css/flaticon/font'), { maxAge: 31557600000 }));
app.use('/public/flaticon2', express.static(path.join(__dirname, 'public/css/flaticon2'), { maxAge: 31557600000 }));
app.use('/public/css/fonts/flaticon2', express.static(path.join(__dirname, 'public/css/flaticon2/font'), { maxAge: 31557600000 }));
app.use('/public/css/fonts/line-awesome', express.static(path.join(__dirname, 'public/css/line-awesome/fonts'), { maxAge: 31557600000 }));

/**
 * Primary app routes.
 */
app.get('/', homeController.index);
app.get('/dashboard', dashboardController.index);
app.get('/login', userController.getLogin);
app.post('/login', userController.postLogin);
app.get('/logout', userController.logout);
app.get('/forgot', userController.getForgot);
app.post('/forgot', userController.postForgot);
app.get('/reset/:token', userController.getReset);
app.post('/reset/:token', userController.postReset);
app.get('/signup', userController.getSignup);
app.post('/signup', userController.postSignup);
app.get('/contact', contactController.getContact);
app.post('/contact', contactController.postContact);
app.get('/account', passportConfig.isAuthenticated, userController.getAccount);
app.post('/account/profile', passportConfig.isAuthenticated, userController.postUpdateProfile);
app.post('/account/password', passportConfig.isAuthenticated, userController.postUpdatePassword);
app.post('/account/delete', passportConfig.isAuthenticated, userController.postDeleteAccount);
app.get('/account/unlink/:provider', passportConfig.isAuthenticated, userController.getOauthUnlink);

在stackoverflow上四处搜索,发现这是对一篇ajax文章的攻击。因此,后端的成功没有处理重定向(因为它不应该)。我已经更新了success:改为重定向

在stackoverflow上四处搜索,发现这是对一篇ajax文章的攻击。因此,后端的成功没有处理重定向(因为它不应该)。我已经更新了success:改为重定向

我还不懂你的代码。但我知道,在passport中,通过authenicate passport中间件登录后支持重定向。您可以尝试:

app.post('/login', passport.authenticate('local', { successRedirect: '/',
                                                    failureRedirect: '/login' }));

如果您想使用flash发送消息。您可以在
failureRedirect
failureFlash:true

之后添加阿曲布他丁。我还不懂您的代码。但我知道,在passport中,通过authenicate passport中间件登录后支持重定向。您可以尝试:

app.post('/login', passport.authenticate('local', { successRedirect: '/',
                                                    failureRedirect: '/login' }));

如果您想使用flash发送消息。你可以在
failureRedirect
failureFlash:true

之后添加atribute,这取决于你的app.js/server.js是如何为静态资产或api调用服务的。刚刚用app.js中的一些代码更新了我的问题,这就是你想问的吗?这取决于你的app.js/server.js是如何编写的,以服务于静态资产还是api调用。刚刚用app.js中的一些代码更新了我的问题,这是你想问的吗?