Skip to content

Commit

Permalink
fix: error with redirect where root path is /
Browse files Browse the repository at this point in the history
fixes #24
  • Loading branch information
wojtek-krysiak committed Sep 28, 2020
1 parent 90c441f commit c3567c1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ const buildAuthenticatedRouter = (

const { rootPath } = admin.options
let { loginPath, logoutPath } = admin.options
// since we are inside already namespaced router we have to replace login and logout routes that
// they don't have rootUrl inside. So changing /admin/login to just /login.
// but there is a case where user gives / as a root url and /login becomes `login`. We have to
// fix it by adding / in front of the route
loginPath = loginPath.replace(rootPath, '')
if (!loginPath.startsWith('/')) { loginPath = `/${loginPath}` }

logoutPath = logoutPath.replace(rootPath, '')
if (!logoutPath.startsWith('/')) { logoutPath = `/${logoutPath}` }

router.get(loginPath, async (req, res) => {
const login = await admin.renderLogin({
Expand Down Expand Up @@ -221,7 +228,11 @@ const buildAuthenticatedRouter = (
router.use((req, res, next) => {
if (AdminBro.Router.assets.find(asset => req.originalUrl.match(asset.path))) {
next()
} else if (req.session.adminUser) {
} else if (req.session.adminUser
// these routes doesn't need authentication
|| req.originalUrl.startsWith(admin.options.loginPath)
|| req.originalUrl.startsWith(admin.options.logoutPath)
) {
next()
} else {
// If the redirection is caused by API call to some action just redirect to resource
Expand Down

0 comments on commit c3567c1

Please sign in to comment.