简体   繁体   English

需要帮助在Node.js中调试我的Web应用程序

[英]Need help debugging my webapp in nodejs

I am using google outh2 in nodejs with help of passport.js by developing google strategy . 我正在通过开发Google策略在通行证.js的帮助下在nodejs中使用Google outh2。 But issues comes here when i use console.log any where in the code in any file whatever file is it in web app directory its working. 但是问题出在这里,当我使用console.log在任何文件中的代码中的任何位置时,无论它在Web应用程序目录中的工作状态如何。 But when i use my console.log in this function nothing is being executed neither i am able to fetch user data nor able to execute console.log to just display a message in console, when executed inside this code 但是当我在此函数中使用console.log时,在此代码中执行时,既无法获取用户数据,也无法执行console.log以仅在控制台中显示一条消息

(accessToken, refreshToken, profile, done) => {
console.log('data');
})

Full code is here: File : passport-setup.js 完整的代码在这里: File:passport-setup.js

var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.use(new GoogleStrategy({
    clientID: "",
    clientSecret: "",
    callbackURL: "http://127.0.0.1:3000/auth/google/redirect"
  },
  function(accessToken, refreshToken, profile, done) {

  console.log('logineed');
  }
));

Route: index.js 路线: index.js

var express = require('express');
var passport = require('passport');
var router = express.Router();

router.get('/login', function(req, res, next) {
  res.render('index', { title: 'Login' });
});
router.get('/google',passport.authenticate('google', { scope: ['profile'] }));

router.get('/google/redirect',  function(req,res){
    res.redirect('/app');
});
router.get('/app',function(req,res){
  res.send('Your are in');
});
module.exports = router;

Entry point: app.js 入口点: app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var passportConfig = require('./config/passport-setup');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/auth', indexRouter);
app.use('/app', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

It seems you are not initializing passport as middleware for Express. 看来您没有将护照初始化为Express的中间件。

Try adding this to app.js 尝试将其添加到app.js

app.use(passport.initialize());

The problem was with code 问题出在代码上

router.get('/google/redirect',  function(req,res){
    res.redirect('/app');
});

in router.js file 在router.js文件中

It should we written as 我们应该写成

router.get('/google/redirect', passport.authenticate('google'),  function(req,res){
    res.redirect('/app');
});

Because what happens is at time of login , before redirection of link happens the google data of that person is accessible. 因为发生的是在登录时,在链接重定向发生之前,该人的google数据是可访问的。 But after the redirection all the data access to google account data is lost. 但是重定向后,对Google帐户数据的所有数据访问都会丢失。 To access it again we will call authenticate function of passport with google strategy which will load the google user data. 要再次访问它,我们将使用Google策略调用护照的身份验证功能,该功能会加载Google用户数据。 As given in code above 如上面的代码所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM