Cookie-Session我如何知道会话是否已设置并检查超时

时间:2019-05-01 06:07:58

标签: node.js express cookies session-cookies

我需要添加一个带有会话时间的cookie,以便快速 会话超时结束后,请使用Web应用程序发送以下标头:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic Realm="MyApp"

这将提示浏览器再次要求提供凭据。

为了实现这一点,我正在使用

import cookieSession from 'cookie-session';
.
.
.
 app.use(
      cookieSession({
        name: 'Zipsession',
        keys: ['key1'],
        maxAge: 3600,
        httpOnly: false,
        secure: false,
        secureProxy: false,
      })
    );

当我在浏览器中查看cookie时,没有任何设置 另外,当我console.log(req)

cookies: {},
   signedCookies: {},
   sessionCookies: 
    Cookies {
      secure: undefined,
      request: [Circular],
      response: 
       ServerResponse {
         domain: null,
         _events: [Object],
         _eventsCount: 1,
         _maxListeners: undefined,
         output: [],
         outputEncodings: [],
         outputCallbacks: [],
         outputSize: 0,
         writable: true,
         _last: false,
         upgrading: false,
         chunkedEncoding: false,
         shouldKeepAlive: true,
         useChunkedEncodingByDefault: true,
         sendDate: true,
         _removedConnection: false,
         _removedContLen: false,
         _removedTE: false,
         _contentLength: null,
         _hasBody: true,
         _trailer: '',
         finished: false,
         _headerSent: false,
         socket: [Object],
         connection: [Object],
         _header: null,
         _onPendingData: [Function: bound updateOutgoingData],
         _sent100: false,
         _expect_continue: false,
         req: [Circular],
         locals: {},
         writeHead: [Function: writeHead],
         [Symbol(outHeadersKey)]: [Object] },
      keys: Keygrip { sign: [Function], verify: [Function], index: [Function] } },
   sessionOptions: {},
   sessionKey: 'Zipsession',
   session: [Getter/Setter],

显示cookie为空。

我该如何 A)设置会话 B)当会话过期时触发401响应

0 个答案:

没有答案