Moved middleware to middleware directory

This commit is contained in:
Adrian Obelmejias
2016-02-15 16:36:18 -05:00
parent f3172adf77
commit 409520d9f1
2 changed files with 36 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
export function errorMiddleware() {
return async (ctx, next) => {
try {
await next()
} catch (err) {
ctx.status = err.status || 500
ctx.body = err.message
ctx.app.emit('error', err, ctx)
}
}
}
+25
View File
@@ -0,0 +1,25 @@
import User from '../models/users'
import config from '../../config/config'
import { verify } from 'jsonwebtoken'
export async function ensureUser(ctx, next) {
const { token } = ctx.query
if (!token) {
ctx.throw(401)
}
let decoded = null
try {
decoded = verify(token, config.tokenSecret)
} catch (err) {
ctx.throw(401)
}
const user = await User.findById(decoded.id, '-password -salt')
if (!user) {
ctx.throw(401)
}
return next()
}