Add API endpoint to fetch team

This commit is contained in:
2020-11-04 21:18:47 -06:00
parent d1d12663a7
commit 3befe69f30
7 changed files with 184 additions and 36 deletions

View File

@@ -1,32 +0,0 @@
const { Middleware } = require('libflitter')
/*
* HomeLogger Middleware
* -------------------------------------------------------------
* This is a sample middleware. It simply prints a console message when
* the route that it is tied to is accessed. By default, it is called if
* the '/' route is accessed. It can be injected in routes globally using
* the middlewares service.
*/
class HomeLogger extends Middleware {
static get services() {
return [...super.services, 'output']
}
/*
* Run the middleware test.
* This method is required by all Flitter middleware.
* It should either call the next function in the stack,
* or it should handle the response accordingly.
*/
test(req, res, next, args) {
this.output.debug('Home was accessed!')
/*
* Call the next function in the stack.
*/
next()
}
}
module.exports = HomeLogger

View File

@@ -0,0 +1,27 @@
const { Middleware } = require('libflitter')
/*
* InjectUserTeam Middleware
* -------------------------------------------------------------
* For the authenticated user, looks up the associated Team instance
* and injects it as request.team.
*/
class InjectUserTeam extends Middleware {
static get services() {
return [...super.services, 'models']
}
/*
* Run the middleware test.
*/
async test(req, res, next, args = {}){
if ( !req.user ) return res.redirect('/auth/login')
const Team = this.models.get('Team')
req.user_team = await Team.getForUser(req.user)
return next()
}
}
module.exports = InjectUserTeam

View File

@@ -0,0 +1,68 @@
/*
* API Routes
* -------------------------------------------------------------
* These routes are related to the AJAX API used by the front-end.
*/
const index = {
/*
* Define the prefix applied to each of these routes.
* For example, if prefix is '/auth':
* '/' becomes '/auth'
* '/login' becomes '/auth/login'
*/
prefix: '/api/v1',
/*
* Define middleware that should be applied to all
* routes defined in this file. Middleware should be
* included using its non-prefixed canonical name.
*
* You can pass arguments along to a middleware by
* specifying it as an array where the first element
* is the canonical name of the middleware and the
* second element is the argument passed to the
* handler's test() method.
*/
middleware: [
// Require an authenticated user
'auth:UserOnly',
// Inject the user's team
'InjectUserTeam',
],
/*
* Define GET routes.
* These routes are registered as GET methods.
* Handlers for these routes should be specified as
* an array of canonical references to controller methods
* or middleware that are applied in order.
*/
get: {
// handlers should be a list of either controller:: or middleware:: references
// e.g. middleware::HomeLogger
// e.g. controller::Home.welcome
'/': [
'controller::Home.welcome'
],
'/my-team': ['controller::Teams.get_my_team'],
},
/*
* Define POST routes.
* These routes are registered as POST methods.
* Handlers for these routes should be specified as
* an array of canonical references to controller methods
* or middleware that are applied in order.
*/
post: {
},
// You can include other HTTP verbs here.
// Supported ones are: get, post, put, delete, copy, patch
}
module.exports = exports = index

View File

@@ -28,9 +28,6 @@ const index = {
middleware: [
// Sets the locale scope
['i18n:Scope', {scope: 'common'}],
['HomeLogger', {note: 'arguments can be specified as the second element in this array'}],
// 'MiddlewareName', // Or without arguments
],
/*