feat: add api support and auth
This commit is contained in:
parent
31e4b854db
commit
a244d98806
21 changed files with 585 additions and 63 deletions
17
src/middlewares/AdminAuthMiddleware.ts
Normal file
17
src/middlewares/AdminAuthMiddleware.ts
Normal 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()
|
||||
}
|
||||
}
|
||||
23
src/middlewares/DelegateAuthMiddleware.ts
Normal file
23
src/middlewares/DelegateAuthMiddleware.ts
Normal 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' }
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue