From 0eaad1db4b76c46073b3e8acc9ab51b7d9ea28b4 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Tue, 26 Jun 2018 11:53:40 +0800 Subject: [PATCH] Redirect to expected page after logging in --- src/layouts/UserLayout.js | 12 +++++++++++- src/models/login.js | 13 ++++++++++++- src/router.js | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/layouts/UserLayout.js b/src/layouts/UserLayout.js index 447f44fb..188ba102 100644 --- a/src/layouts/UserLayout.js +++ b/src/layouts/UserLayout.js @@ -31,6 +31,16 @@ const copyright = ( ); +function getLoginPathWithRedirectPath() { + const routePath = '/user/login'; + const urlParams = new URL(window.location.href); + const redirect = urlParams.searchParams.get('redirect'); + if (redirect) { + return `${routePath}?redirect=${encodeURIComponent(redirect)}`; + } + return routePath; +} + class UserLayout extends React.PureComponent { getPageTitle() { const { routerData, location } = this.props; @@ -66,7 +76,7 @@ class UserLayout extends React.PureComponent { exact={item.exact} /> ))} - + diff --git a/src/models/login.js b/src/models/login.js index ea9fd30a..274621cb 100644 --- a/src/models/login.js +++ b/src/models/login.js @@ -20,7 +20,18 @@ export default { // Login successfully if (response.status === 'ok') { reloadAuthorized(); - yield put(routerRedux.push('/')); + const urlParams = new URL(window.location.href); + let redirect = urlParams.searchParams.get('redirect'); + if (redirect) { + const redirectUrlParams = new URL(redirect); + if (redirectUrlParams.origin === urlParams.origin) { + redirect = redirect.substr(urlParams.origin.length); + } else { + window.location.href = redirect; + return; + } + } + yield put(routerRedux.push(redirect || '/')); } }, *logout(_, { put, select }) { diff --git a/src/router.js b/src/router.js index 3c3a9786..144c8267 100644 --- a/src/router.js +++ b/src/router.js @@ -26,7 +26,7 @@ function RouterConfig({ history, app }) { path="/" render={props => } authority={['admin', 'user']} - redirectPath="/user/login" + redirectPath={`/user/login?redirect=${encodeURIComponent(window.location.href)}`} /> -- GitLab