简体   繁体   English

护照js会议与科尔斯没有合作

[英]passport js sessions does not work with Cors

I search a lot on internet but I can not find the answer. 我在互联网上搜索了很多,但我找不到答案。

I am using my frontend on one port and my backend in another. 我在一个端口使用我的前端,在另一个端口使用我的后端。 I use cors to enable the requests and this works. 我使用cors来启用请求,这是有效的。 But when I implement passport js and sessions with mongodb whit does not work, it means when I try to get the user with req.user this is undefined. 但是当我实现护照js并且使用mongodb whit的会话不起作用时,这意味着当我尝试使用req.user获取用户时,这是未定义的。 Here is my index.js code 这是我的index.js代码

app.use(cors());
app.use(session({
resave: false,
saveUninitialized: false,
secret: 's3cr3et',
store: new MongoStore({
    url: process.env.MONGO_URI
})
}));
app.use(passport.initialize());
app.use(passport.session());

my passport js is 我的护照js是

Here i define my auth, serialize and deserialize user. 在这里,我定义我的auth,序列化和反序列化用户。 My Auth and serialize is working well but deserialize not 我的Auth和序列化工作正常, 但不反序列化

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

passport.deserializeUser(function (id, done)  {
  User.findById(id, (err, user) => {
    done(null, user);
  });
});

passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
  User.findOne({ email: email.toLowerCase() }, (err, user) => {
    if (err) { return done(err); }
    if (!user) { return done(null, false, { error: 'Invalid credentials.' }); }
    user.comparePassword(password, (err, isMatch) => {
      if (err) { return done(err); }
      if (isMatch) {
        return done(null, user);
      }
      return done(null, false, { error: 'Invalid credentials.' });
    });
  });
}));

I tried also cookie session instead mongo db but I guess is not the solution. 我也试过cookie会话而不是mongo db,但我想这不是解决方案。 The problem is the cors, for some reason passport dont read the session. 问题是因为某些原因护照不读会议。

Also I tried to enable in coors credentials and specify the origin but deserialize still not set the user 此外,我尝试启用coors凭据并指定原点但反序列化仍然没有设置用户

app.use(cors({
    credentials: true,
    origin: ['http://localhost:8080']
}));

I hope you can help me because I can not get it work Thanks !! 我希望你能帮助我,因为我无法让它工作谢谢!!

I had the same issue. 我遇到过同样的问题。 In my case, I was having an SPA on the frontend with a RESFful (well, not really as we are maintaining state here) backend on Node.js. 在我的情况下,我在前端有一个SPA,在Node.js上有一个RESFful(好吧,不是我们在这里维护状态)。 Later I found out that I was not sending the browser cookies along with the data when I used to make requests to my RESTful API. 后来我发现当我以前向RESTful API发出请求时,我没有发送浏览器cookie和数据。 This was exactly where the issue was that what was not letting users get Deserialized because we need cookies data on the backend to find out which user has sent a particular request. 这正是问题所在,即不让用户反序列化的问题,因为我们需要后端的cookie数据来找出哪个用户发送了特定请求。 Try doing the same, I hope that would solve your problem as it did mine. 尝试做同样的事情,我希望能像我一样解决你的问题。 :) :)

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

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