req.session.user – Property 'user' does not exist on type 'Session' error when trying to pass session data using express-session

huangapple go评论79阅读模式
英文:

req.session.user - Property 'user' does not exist on type 'Session' error when trying to pass session data using express-session

问题

I am trying to make a simple login form for users to log into a website, where I then save their session data into a session cookie. I am using express-session and in their documentation it gives this as an example of setting it up:

app.post('/login', express.urlencoded({ extended: false }), function (req, res) {
  // login logic to validate req.body.user and req.body.pass
  // would be implemented here. for this example any combo works

  // store user information in session, typically a user id
  req.session.user = req.body.user

  // save the session before redirection to ensure page
  // load does not happen before session is saved
  req.session.save(function (err) {
    if (err) return next(err)
    res.redirect('/')
  })
})

but in my code I keep getting an error at the 'req.session.user' part which says: "Property 'user' does not exist on type 'Session & Partial' even if I am using the exact same code from the express-session documentation.

req.session.user – Property 'user' does not exist on type 'Session' error when trying to pass session data using express-session

I follow all the instructions in the documentation and have this at the top of the program as well:

import session from 'express-session';

app.set('trust proxy', 1);

app.use(session({  
  name: `First_test`,
  secret: 'secret_text', 
  saveUninitialized: true, 
  resave: true,
  cookie: { 
    secure: false, 
    maxAge: 960000 
  } 
}));

When debugging I can see that inside the req.session there is indeed no 'user' property.

I have searched google and stackoverflow but haven't been able to find a solution. Most likely I have missed some small step and I am hoping someone here can help educate me.

Thanks for the help in advance.

英文:

I am trying to make a simple login form for users to log into a website, where I then save their session data into a session cookie. I am using express-session and in their documentation it gives this as an example of setting it up:

app.post('/login', express.urlencoded({ extended: false }), function (req, res) {
  // login logic to validate req.body.user and req.body.pass
  // would be implemented here. for this example any combo works

    // store user information in session, typically a user id
    req.session.user = req.body.user

    // save the session before redirection to ensure page
    // load does not happen before session is saved
    req.session.save(function (err) {
      if (err) return next(err)
      res.redirect('/')
    })
  })
})

but in my code I keep getting an error at the 'req.session.user' part which says: "Property 'user' does not exist on type 'Session & Partial<SessionData>'" even if I am using the exact same code from the express-session documentation.

req.session.user – Property 'user' does not exist on type 'Session' error when trying to pass session data using express-session

I follow all the instructions in the documentation and have this at the top of the program as well:

import session from &#39;express-session&#39;;

app.set(&#39;trust proxy&#39;, 1);

app.use(session({  
  name: `First_test`,
  secret: &#39;secret_text&#39;, 
  saveUninitialized: true, 
  resave: true,
  cookie: { 
    secure: false, 
    maxAge: 960000 
  } 
}));

When debugging I can see that inside the req.session there is indeed no 'user' property.

I have searched google and stackoverflow but haven't been able to find a solution. Most likely I have missed some small step and I am hoping someone here can help educate me.

Thanks for the help in advance.

答案1

得分: 0

From the @types/express-session package, we can see:

/**
 * This interface allows you to declare additional properties on your session object using declaration merging.
 *
 * @example
 * declare module 'express-session' {
 *     interface SessionData {
 *         views: number;
 *     }
 * }
 *
 */
interface SessionData {
    cookie: Cookie;
}

So you should declare the user properties on your session object like this:

declare module 'express-session' {
  interface SessionData {
    user: string;
  }
}

app.post('/login', express.urlencoded({ extended: false }), function (req, res) {
  req.session.user = req.body.user
})

Package version: "express-session": "^1.17.1"

英文:

From the @types/express-session package, we can see

    /**
     * This interface allows you to declare additional properties on your session object using [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html).
     *
     * @example
     * declare module &#39;express-session&#39; {
     *     interface SessionData {
     *         views: number;
     *     }
     * }
     *
     */
    interface SessionData {
        cookie: Cookie;
    }

So you should declare the user properties on your session object like this:

declare module &#39;express-session&#39; {
  interface SessionData {
    user: string;
  }
}

app.post(&#39;/login&#39;, express.urlencoded({ extended: false }), function (req, res) {
  req.session.user = req.body.user
})

req.session.user – Property 'user' does not exist on type 'Session' error when trying to pass session data using express-session

package version: &quot;express-session&quot;: &quot;^1.17.1&quot;

huangapple
  • 本文由 发表于 2023年4月19日 17:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定