how to set and get session with koajs and koa-session?

140 Views Asked by At

im already read from the documentation and also find some tutorial like tutorialspoint. But, so far i dont know how to create and use session with koa. Here's my code:

router.post('/login', async ctx => {
  const user = ctx.request.body
  const name = user.name
  const email = user.email
  const password = user.password
  const users = require('./client').db('mydatabase').collection('users')
  const userExist = await users.findOne({"name": name})
  const emailExist = await users.findOne({"email": email})

  if(userExist || emailExist) {
    this.session.email = email
    ctx.body = this.session.email
  }
})

above code give error below:

TypeError: Cannot set properties of undefined (setting 'email')

Previously i've learn a lot from laravel, Here's how we create and use the session with laravel:

//creating session
...
session([
'email' => $request->input('email')
])
...
//use session
$email = session('email')
...

can you tell me? or do you have better example? Thanks

0

There are 0 best solutions below