feat: add api support and auth

This commit is contained in:
Matthieu Bessat 2020-07-10 22:44:01 +02:00
parent 31e4b854db
commit a244d98806
21 changed files with 585 additions and 63 deletions

View file

@ -0,0 +1,17 @@
import express from 'express'
export default class AdminAuthMiddleware {
static handle(req: express.Request, res: express.Response, next: any) {
let auth: string | undefined = req.get('Authorization')
if (auth === undefined || auth.replace('Bearer ', '') !== process.env.ADMIN_TOKEN) {
res
.status(400)
.json({
success: false,
errors: { code: 'invalid-auth', message: 'Invalid admin Authorization header' }
})
return
}
next()
}
}

View file

@ -0,0 +1,23 @@
import express from 'express'
import Organization from '../models/Organization'
export default class DelegateAuthMiddleware {
static async handle(req: express.Request, res: express.Response, next: express.NextFunction) {
let token: string | undefined = req.get('Authorization')
// fetch the token
if (token !== undefined) {
let data = await Organization.findOne({ token: token.replace('Bearer ', '') })
if (data !== null) {
res.locals.organization = data
next()
return
}
}
res
.status(400)
.json({
success: false,
errors: { code: 'invalid-auth', message: 'Invalid admin Authorization header' }
})
}
}