错误:未使用passport.initialize()中间件

时间:2019-08-21 03:35:13

标签: node.js express passport.js

我正在使用passport.js创建登录名。但是得到一个错误'passport.initialize()中间件未使用。尝试了所有现有的堆栈溢出类似问题,但我仍然收到错误消息, 谁能建议修复程序?

错误:IncomingMessage.req.login.req.logIn(/srv/node_modules/passport/lib/http/request.js:46:34)处未使用passport.initialize()中间件(/srv/node_modules/passport/lib/middleware/authenticate.js:248:13)已通过验证(/srv/node_modules/passport-oauth2/lib/strategy.js:186:20) datastore.runQuery.then.results(/srv/config/passport-setup.js:48:16)

passport-setup.js

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const keys = require('./keys');
const gstore = require('gstore-node')();
const Datastore = require('@google-cloud/datastore');
const projectId = 'stately';
const datastore = new Datastore({
    projectId: projectId,
});
gstore.connect(datastore);



passport.serializeUser((user, done)=> {
    done(null, user);
  })

  passport.deserializeUser(function(user, done) {
    done(null, user);
  });  

passport.use(
    new GoogleStrategy({
        clientID: keys.google.clientID,
        clientSecret: keys.google.clientSecret,
        callbackURL: '/auth/google/callback',
        userProfileURL:keys.google.userProfileURL
   
},(accessToken, refreshToken, profile, done) => {
console.log(profile);
var email =profile.emails[0].value;
var user =profile.id;
const newUser = {
username :profile.displayName,
googleId : profile.id,
email:email
}
const query = datastore.createQuery('Profile').filter('email', '=', email);
datastore.runQuery(query).then(results => {
    const tasks = results[0];
    const newData = JSON.stringify(tasks);
    const parsed = JSON.parse(newData);
    const data = parsed;
    const gUser = data[0].email;
    if (gUser == email) {
        //gUser = profile.id;
        console.log("My Email",user,profile.id)
        return done (null, user);
        }
        else {
        return done (null, false);
        }
/*
    if (gUser == email) {
        return done (null, user);
     } else {
    datastore.save({
    key: datastore.key(['Profile', username]),
    data: newUser,
    }).then(() => {
    //  res.send("Iteam Saved to Database");
    return done (null, false);
    res.redirect('/login');
*/
    }).catch((err) => {
   console.log(err)
    })
    })
    )

pp.js

var express = require('express');
var server = express();
const https = require('https');
const ejs = require('ejs');
const gstore = require('gstore-node')();
const Datastore = require('@google-cloud/datastore');
const mimeTypes = require('mimetypes');
const fs = require('fs');
const projectId = 'statelyXXXXX';
var path = require("path");
const bodyParser = require("body-parser");
const passport = require('passport');
const cookieSession = require('cookie-session');
server.set('views', path.join(__dirname, 'views'));
server.set('view engine', 'ejs');
server.engine('html', require('ejs').renderFile);
//Set path for static assets
server.use(express.static(path.join(__dirname, 'public')));

server.use(bodyParser.json({
    limit: '50mb'
}));
server.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true
}));
const SendOtp = require('sendotp');
const sendOtp = new SendOtp('233', 'Your verification code is {{otp}}');
var moment = require('moment');
const swal = require('sweetalert2');
var nodemailer = require('nodemailer');
var SMTP = require('nodemailer-smtp-transport');
const vision = require('@google-cloud/vision');

//Database
const datastore = new Datastore({
    projectId: projectId,
});
gstore.connect(datastore);
//View engine setup

const keys = require('./config/keys');
const passportSetup = require('./config/passport-setup');
const authRoutes = require('./routes/auth-routes');
var templates = require('./routes/templates');
//set Routes
server.use('/', templates);
server.use('/auth', authRoutes);



server.use(cookieSession({
maxAge:24*60*60*1000,
keys:[keys.session.cookieKey]
}))

//initialize passport 

server.use(passport.initialize());
server.use(passport.session());

1 个答案:

答案 0 :(得分:0)

node.js从上到下依次加载index.js。说到阅读

const passportSetup = require('./config/passport-setup');

Node.js将解析此模块,并会看到装载了password.js以及正在使用的一些password.js函数,但未激活它们。因此它将向您发送由护照造成的错误。 js

首先,您必须使用cookie会话。因为cookie会话将解析cookie,并检索user.id所需的数据并将其传递给passport.js

server.use(cookieSession({
maxAge:24*60*60*1000,
keys:[keys.session.cookieKey]
}))

然后我们需要使用

app.use(passport.initialize())

中间件函数可初始化password.js,因此我们可以在路由处理程序中使用Passport。中间件是一些小功能,可用于在将我们的应用程序发送到路由处理程序之前修改对我们应用程序的传入请求。

然后我们使用

app.use(passport.session())

中间件,用于将password.js连接到Cookie会话。