Do I have to store tokens in cookies or localstorage or session?

后端 未结 4 722
醉酒成梦
醉酒成梦 2020-11-27 02:41

I am using React SPA, Express, Express-session, Passport, and JWT. I\'m confused about some of the different client-side storage options to store tokens: Cookies, Session, a

4条回答
  •  日久生厌
    2020-11-27 02:59

    HTTP is a stateless protocol. Read that answer for more detail, but essentially that means that HTTP servers, such as your web server, do not store any information about clients beyond the lifetime of one request. This is a problem for web apps because it means you can't remember which user is logged in.

    Cookies were invented as the solution to this. Cookies are textual data that the client and server send back and forth on every request. They allow you to effectively maintain application state data, by having the client and server agree on what they remember each time they communicate.

    This means, fundamentally, you cannot have a session without a cookie. There must be a cookie that stores at least the session ID, so that you can find out which user is currently logged into your app by looking up the session. This is what express-session does: the documentation for the main session method explicitly notes that the session ID is stored in a cookie.

    so my question is do I need to store cookies?because I can access it via req.sessionID to get the data needed.

    You don't need to store cookies. express-session will do this for you. Your application as a whole does need to store a cookie; without it, you wouldn't have a req.sessionID to look up.

提交回复
热议问题