在passport的帮助下,我正在nodejs中使用googleouth2。通过开发谷歌战略。但当我使用控制台时,问题就出现了。在任何文件的代码中的任何位置记录它在web应用程序目录中的任何文件。但当我使用控制台时。登录此函数未执行任何操作。我无法获取用户数据,也无法执行控制台。当在此代码内执行时,登录以仅在控制台中显示消息
(accessToken, refreshToken, profile, done) => {
console.log('data');
})
完整代码如下:
文件:passport设置。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');
}
));
路线:
指数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;
入口点:
应用程序。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;