mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-22 17:12:03 -07:00
26 lines
455 B
JavaScript
26 lines
455 B
JavaScript
import User from '../models/users'
|
|
import config from '../../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.token)
|
|
} catch (err) {
|
|
ctx.throw(401)
|
|
}
|
|
|
|
const user = await User.findById(decoded.id, '-password')
|
|
if (!user) {
|
|
ctx.throw(401)
|
|
}
|
|
|
|
return next()
|
|
}
|