mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
Add user validation middleware
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
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)
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await User.findById(decoded.id)
|
||||
if(!user) {
|
||||
ctx.throw(401)
|
||||
}
|
||||
} catch (err) {
|
||||
ctx.throw(500)
|
||||
}
|
||||
|
||||
return next()
|
||||
}
|
||||
Reference in New Issue
Block a user