Commit 0eaad1db authored by yoyo837's avatar yoyo837 Committed by ι™ˆεΈ…

Redirect to expected page after logging in

parent c5df0734
...@@ -31,6 +31,16 @@ const copyright = ( ...@@ -31,6 +31,16 @@ const copyright = (
</Fragment> </Fragment>
); );
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 { class UserLayout extends React.PureComponent {
getPageTitle() { getPageTitle() {
const { routerData, location } = this.props; const { routerData, location } = this.props;
...@@ -66,7 +76,7 @@ class UserLayout extends React.PureComponent { ...@@ -66,7 +76,7 @@ class UserLayout extends React.PureComponent {
exact={item.exact} exact={item.exact}
/> />
))} ))}
<Redirect exact from="/user" to="/user/login" /> <Redirect from="/user" to={getLoginPathWithRedirectPath()} />
</Switch> </Switch>
</div> </div>
<GlobalFooter links={links} copyright={copyright} /> <GlobalFooter links={links} copyright={copyright} />
......
...@@ -20,7 +20,18 @@ export default { ...@@ -20,7 +20,18 @@ export default {
// Login successfully // Login successfully
if (response.status === 'ok') { if (response.status === 'ok') {
reloadAuthorized(); 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 }) { *logout(_, { put, select }) {
......
...@@ -26,7 +26,7 @@ function RouterConfig({ history, app }) { ...@@ -26,7 +26,7 @@ function RouterConfig({ history, app }) {
path="/" path="/"
render={props => <BasicLayout {...props} />} render={props => <BasicLayout {...props} />}
authority={['admin', 'user']} authority={['admin', 'user']}
redirectPath="/user/login" redirectPath={`/user/login?redirect=${encodeURIComponent(window.location.href)}`}
/> />
</Switch> </Switch>
</ConnectedRouter> </ConnectedRouter>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment