import User from '../models/users'
import config from '../../config'
import { getToken } from '../utils/auth'
import { verify } from 'jsonwebtoken'

export async function ensureUser (ctx, next) {
  const token = getToken(ctx)

  if (!token) {
    ctx.throw(401)
  }

  let decoded = null
  try {
    decoded = verify(token, config.token)
  } catch (err) {
    ctx.throw(401)
  }

  const user = await User.findById(decoded.id, '-password')
  if (!user) {
    ctx.throw(401)
  }

  return next()
}

@@ -376,21 +376,25 @@
   %7D%0A%0A  c
-onst 
+tx.state.
 user = a
@@ -444,16 +444,26 @@
 %0A  if (!
+ctx.state.
 user) %7B%0A

Attach current user to context.user