From 8345ec073304ad1d1f73b97fd26acd4da0a845b7 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Sat, 16 Nov 2019 19:25:44 -0500 Subject: [PATCH] add Account route --- src/api/routes/Account.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/api/routes/Account.ts diff --git a/src/api/routes/Account.ts b/src/api/routes/Account.ts new file mode 100644 index 0000000..7b5aa4c --- /dev/null +++ b/src/api/routes/Account.ts @@ -0,0 +1,21 @@ +/* eslint-disable consistent-return */ +import { Server } from '..'; +import { Route } from '../../class'; + +export default class Account extends Route { + constructor(server: Server) { + super(server, { path: '/account', deprecated: false }); + } + + public async bind() { + this.router.use(async (req, res, next) => { + const url = new URL(req.url); + const account = await this.server.client.db.Account.findOne({ username: url.username }); + if (!account) return res.status(401).json({ code: this.constants.codes.ACCOUNT_NOT_FOUND, message: 'UNAUTHORIZED' }); + // eslint-disable-next-line no-underscore-dangle + const authResult = await this.server.security.checkBearer(account._id, this.server.security.extractBearer(req)); + if (!authResult) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: 'UNAUTHORIZED' }); + next(); + }); + } +}