Add `/me*` route which redirects to `/user/[userslug]*` (#6063)

* Add `/me*` route which redirects to the current user's information

- `/me` -> `/user/[usertslug]`
- `/me/bookmarks` -> `/user/[userslug]/bookmarks`
- `/me/settings` -> `/user/[userslug]/settings`

etc

* Add tests for `/me/*`
v1.18.x
Peter Jaszkowiak 7 years ago committed by Barış Soner Uşaklı
parent 643008041c
commit f5385e38bf

@ -140,6 +140,22 @@ module.exports = function (middleware) {
], next);
};
middleware.redirectMeToUserslug = function (req, res, next) {
var uid = req.uid;
async.waterfall([
function (next) {
user.getUserField(uid, 'userslug', next);
},
function (userslug) {
if (!userslug) {
return res.status(401).send('not-authorized');
}
var path = req.path.replace(/^(\/api)?\/me/, '/user/' + userslug);
controllers.helpers.redirect(res, path);
},
], next);
};
middleware.isAdmin = function (req, res, next) {
async.waterfall([
function (next) {

@ -7,7 +7,8 @@ module.exports = function (app, middleware, controllers) {
var middlewares = [middleware.checkGlobalPrivacySettings];
var accountMiddlewares = [middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions];
setupPageRoute(app, '/uid/:uid/:section1?/:section2?', middleware, [], middleware.redirectUidToUserslug);
setupPageRoute(app, '/me/*', middleware, [], middleware.redirectMeToUserslug);
setupPageRoute(app, '/uid/:uid*', middleware, [], middleware.redirectUidToUserslug);
setupPageRoute(app, '/user/:userslug', middleware, middlewares, controllers.accounts.profile.get);
setupPageRoute(app, '/user/:userslug/following', middleware, middlewares, controllers.accounts.follow.getFollowing);

@ -186,8 +186,9 @@ describe('authentication', function () {
url: nconf.get('url') + '/api/me',
json: true,
jar: jar,
}, function (err, response, body) {
}, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 401);
assert.equal(body, 'not-authorized');
done();
});

@ -937,6 +937,35 @@ describe('Controllers', function () {
});
});
describe('/me/*', function () {
it('api should redirect to /user/[userslug]/bookmarks', function (done) {
request(nconf.get('url') + '/api/me/bookmarks', { jar: jar, json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.equal(res.headers['x-redirect'], '/user/foo/bookmarks');
assert.equal(body, '/user/foo/bookmarks');
done();
});
});
it('api should redirect to /user/[userslug]/edit/username', function (done) {
request(nconf.get('url') + '/api/me/edit/username', { jar: jar, json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.equal(res.headers['x-redirect'], '/user/foo/edit/username');
assert.equal(body, '/user/foo/edit/username');
done();
});
});
it('should 401 if user is not logged in', function (done) {
request(nconf.get('url') + '/me/bookmarks', { json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 401);
assert.equal(body, 'not-authorized');
done();
});
});
});
it('should 401 if user is not logged in', function (done) {
request(nconf.get('url') + '/api/admin', { json: true }, function (err, res) {
assert.ifError(err);

Loading…
Cancel
Save