简体   繁体   English

服务器端文件更改后,未定义Passport.js req.user对象(节点,快速)

[英]Passport.js req.user object is undefined after server side file changes (node, express)

I'm using passport.js to authenticate the user to my app, but I have noticed that after any changes on server files, req.user is undefined. 我正在使用passport.js对用户进行身份验证,但我注意到在服务器文件上进行任何更改后,req.user是未定义的。 Nodemon is correctly reloading all the files but after I hit refresh - an app is broken and req.user is undefined. Nodemon正确地重新加载了所有文件,但是在单击刷新后-一个应用程序损坏了,并且req.user未定义。 What is important: it only happens when server-side files are changed. 重要的是:仅在更改服务器端文件时才会发生。 When there are no changes I can refresh many times and everything works well. 如果没有更改,我可以刷新很多次,并且一切正常。

This is my config code: 这是我的配置代码:

app.use(
  session({
    secret: process.env.EXPRESS_SESSION_KEY,
    unset: 'destroy',
    saveUninitialized: false,
    resave: false
  })
);
app.use(bodyParser.urlencoded({ extended: false, credentials: true }));
app.use(bodyParser.json());
app.use(
  cors({
    origin: FRONTEND_URL,
    credentials: true
  })
);
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static('../../dist'));

I can't find any solution. 我找不到任何解决方案。 It's pretty annoying to rebuild the whole app after any file change on the server side. 在服务器端进行任何文件更改后,重新构建整个应用程序是很烦人的。

I can also notice that req.session is empty after file changes. 我还可以注意到文件更改后req.session为空。 Why is that? 这是为什么?

You haven't specified the store property , so Express Session will default to using a MemoryStore . 您尚未指定store属性 ,因此Express Session将默认使用MemoryStore This keeps session data in memory, so when you change the server side code and trigger a reload of the server, the session store is wiped and all session data is lost. 这会将会话数据保留在内存中,因此,当您更改服务器端代码并​​触发服务器重新加载时,将擦除会话存储,并且所有会话数据都会丢失。

Use a different session store (such as session file store ) which stores the data somewhere persistent. 使用不同的会话存储 (例如会话文件存储 )将数据存储在持久的位置。

Use below link to and see see there is some thing called Store . 使用下面的链接到,看看有什么东西叫做Store Use that which actually storing using passportjs sessions 使用实际使用护照护照会话存储的内容

https://blog.risingstack.com/node-hero-node-js-authentication-passport-js/ https://blog.risingstack.com/node-hero-node-js-authentication-passport-js/

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

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