diff --git a/lib/routes/admin-api/user.js b/lib/routes/admin-api/user.js index e4b5fe036d..d66c3018bc 100644 --- a/lib/routes/admin-api/user.js +++ b/lib/routes/admin-api/user.js @@ -26,6 +26,7 @@ class UserController extends Controller { } } + // Depcreated, use "/logout" instead. Will be removed in later release. logout(req, res) { if (req.session) { req.session = null; @@ -33,7 +34,7 @@ class UserController extends Controller { if (req.logout) { req.logout(); } - res.redirect('/'); + res.redirect(`${this.config.baseUriPath}/`); } } diff --git a/lib/routes/admin-api/user.test.js b/lib/routes/admin-api/user.test.js index c16e3c47c5..116759428f 100644 --- a/lib/routes/admin-api/user.test.js +++ b/lib/routes/admin-api/user.test.js @@ -55,5 +55,5 @@ test('should logout and redirect', t => { return request .get(`${base}/api/admin/user/logout`) .expect(302) - .expect('Location', '/'); + .expect('Location', `${base}/`); }); diff --git a/lib/routes/index.js b/lib/routes/index.js index 91600e4db2..322e81ed56 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -7,6 +7,7 @@ const FeatureController = require('./client-api/feature.js'); const Controller = require('./controller'); const HealthCheckController = require('./health-check'); const BackstageCTR = require('./backstage.js'); +const LogoutController = require('./logout'); const api = require('./api-def'); class IndexRouter extends Controller { @@ -14,6 +15,7 @@ class IndexRouter extends Controller { super(); this.use('/health', new HealthCheckController(config).router); this.use('/internal-backstage', new BackstageCTR(config).router); + this.use('/logout', new LogoutController(config).router); this.get(api.uri, this.index); this.use(api.links.admin.uri, new AdminApi(config).router); this.use(api.links.client.uri, new ClientApi(config).router); diff --git a/lib/routes/logout.js b/lib/routes/logout.js new file mode 100644 index 0000000000..c741c8313d --- /dev/null +++ b/lib/routes/logout.js @@ -0,0 +1,22 @@ +'use strict'; + +const Controller = require('./controller'); + +class HealthCheckController extends Controller { + constructor(config) { + super(config); + this.get('/', this.logout); + } + + logout(req, res) { + if (req.session) { + req.session = null; + } + if (req.logout) { + req.logout(); + } + res.redirect(`${this.config.baseUriPath}/`); + } +} + +module.exports = HealthCheckController; diff --git a/lib/routes/logout.test.js b/lib/routes/logout.test.js new file mode 100644 index 0000000000..4cc6da822c --- /dev/null +++ b/lib/routes/logout.test.js @@ -0,0 +1,46 @@ +'use strict'; + +const test = require('ava'); +const supertest = require('supertest'); +const store = require('./../../test/fixtures/store'); +const getLogger = require('../../test/fixtures/no-logger'); +const getApp = require('../app'); +const User = require('../user'); + +const { EventEmitter } = require('events'); +const eventBus = new EventEmitter(); + +const currentUser = new User({ email: 'test@mail.com' }); + +function getSetup() { + const base = `/random${Math.round(Math.random() * 1000)}`; + const stores = store.createStores(); + const app = getApp({ + baseUriPath: base, + stores, + eventBus, + getLogger, + preHook: a => { + a.use((req, res, next) => { + req.user = currentUser; + next(); + }); + }, + }); + + return { + base, + strategyStore: stores.strategyStore, + request: supertest(app), + }; +} + +test('should logout and redirect', t => { + t.plan(0); + const { request, base } = getSetup(); + + return request + .get(`${base}/logout`) + .expect(302) + .expect('Location', `${base}/`); +});