koajs / session

Simple session middleware for koa
MIT License
902 stars 113 forks source link

Remember user login state #91

Closed cpprookie closed 7 years ago

cpprookie commented 7 years ago

I want to take koa-session to remember user login state. And after reading docs and code ,I got blocked by the concept store session in cookie.

I take this code to show my understanding route --> /signin

router.post('signin', async ctx => { 
   /*
    *  parse  http  request  body    param-->(userName, password)
    */
    ctx.session.userList = ctx.session.userList || []    // userList to register user who have logged 
    if (ctx.session.userList.indexOf(userName) === -1) {
      ctx.throw (400, 'user  is online')
    }  else {
     //  check userName and  password  in db
     ctx.session.userList.push(userName)
     ctx.status=200  // response to browser and  create a cookie  (key as config.key, value as encode(session))
   }
})

In this way, anyOne who visits route signin will recieve a cookie. And as more users log in, userList in session and cookie(key.value) both will grow.

Is this a correct understaning?I used to store session id in db to implement this situation so i'm really not certain about this. Please show me something if it's wrong, thanks a lot.

cpprookie commented 7 years ago

Looks like it's not a good question. For anyone who arrives here, i take cookie as store config at last. Thanks!