So, I created a custom plugin just to add a new URL "page" and its API endpoint like this.
Plugin.load = function (params, callback) {
var router = params.router;
var middleware = params.middleware;
// Define the function that renders the custom route.
async function render(req, res, next) {
if ( ( req.user !== undefined && req.user !== null ) ) {
// Get whatever data you want to send to the template here.
var data = {
query: req.query,
};
// This is the path to your template without the .tpl, relative to the templates directory in plugin.json
var template = 'comments'
// Send the page to the user.
res.render(template, data);
} else {
next();
}
}
// This actually creates the routes, you need two routes for every page.
// The first parameter is the actual path to your page.
router.get('/comments', middleware.buildHeader, render);
router.get('/api/comments', render);
callback();
};
When I do
if ( ( req.user !== undefined && req.user !== null ) ) {
I ensure that the user has to be logged in in order to see the page or API.
But, how can I check if a user is querying this through API and with a valid bearer token?