feat: new shorthand route /api/v3/users/bySlug/:userslug

closes #8844
v1.18.x
Julian Lam 4 years ago
parent 7e273e7710
commit 60e1e99b4f

@ -144,7 +144,7 @@ helpers.notAllowed = async function (req, res, error) {
}; };
helpers.redirect = function (res, url, permanent) { helpers.redirect = function (res, url, permanent) {
if (res.locals.isAPI) { if (res.locals.isAPI && !url.startsWith('/api/v3/')) {
res.set('X-Redirect', encodeURI(url)).status(200).json(url); res.set('X-Redirect', encodeURI(url)).status(200).json(url);
} else { } else {
res.redirect(permanent ? 308 : 307, relative_path + encodeURI(url)); res.redirect(permanent ? 308 : 307, relative_path + encodeURI(url));

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const api = require('../../api'); const api = require('../../api');
const user = require('../../user');
const meta = require('../../meta'); const meta = require('../../meta');
const privileges = require('../../privileges'); const privileges = require('../../privileges');
const utils = require('../../utils'); const utils = require('../../utils');
@ -16,6 +17,17 @@ const hasAdminPrivilege = async (uid, privilege) => {
} }
}; };
Users.redirectBySlug = async (req, res) => {
const uid = await user.getUidByUserslug(req.params.userslug);
if (uid) {
const path = req.path.split('/').slice(3).join('/');
helpers.redirect(res, `/api/v3/users/${uid}/${path}`, false);
} else {
helpers.formatApiResponse(404, res);
}
};
Users.create = async (req, res) => { Users.create = async (req, res) => {
await hasAdminPrivilege(req.uid, 'users'); await hasAdminPrivilege(req.uid, 'users');
const userObj = await api.users.create(req, req.body); const userObj = await api.users.create(req, req.body);

@ -33,6 +33,9 @@ function authenticatedRoutes() {
setupApiRoute(router, 'post', '/:uid/tokens', [...middlewares, middleware.assert.user], controllers.write.users.generateToken); setupApiRoute(router, 'post', '/:uid/tokens', [...middlewares, middleware.assert.user], controllers.write.users.generateToken);
setupApiRoute(router, 'delete', '/:uid/tokens/:token', [...middlewares, middleware.assert.user], controllers.write.users.deleteToken); setupApiRoute(router, 'delete', '/:uid/tokens/:token', [...middlewares, middleware.assert.user], controllers.write.users.deleteToken);
// Shorthand route to access user routes by userslug
router.all('/+bySlug/:userslug*?', [...middlewares], controllers.write.users.redirectBySlug);
} }
module.exports = function () { module.exports = function () {

Loading…
Cancel
Save