瀏覽代碼

optimize SecurityLayout (#5252)

kwzm 6 年之前
父節點
當前提交
893594dbd0
共有 1 個文件被更改,包括 11 次插入3 次删除
  1. 11 3
      src/layouts/SecurityLayout.tsx

+ 11 - 3
src/layouts/SecurityLayout.tsx

@@ -1,6 +1,7 @@
 import React from 'react';
 import { connect } from 'dva';
 import { Redirect } from 'umi';
+import { stringify } from 'querystring';
 import { ConnectState, ConnectProps } from '@/models/connect';
 import { CurrentUser } from '@/models/user';
 import PageLoading from '@/components/PageLoading';
@@ -34,11 +35,18 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout
   render() {
     const { isReady } = this.state;
     const { children, loading, currentUser } = this.props;
-    if ((!currentUser.userid && loading) || !isReady) {
+    // You can replace it to your authentication rule (such as check token exists)
+    // 你可以把它替换成你自己的登录认证规则(比如判断 token 是否存在)
+    const isLogin = currentUser && currentUser.userid;
+    const queryString = stringify({
+      redirect: window.location.href,
+    });
+
+    if ((!isLogin && loading) || !isReady) {
       return <PageLoading />;
     }
-    if (!currentUser.userid) {
-      return <Redirect to="/user/login"></Redirect>;
+    if (!isLogin) {
+      return <Redirect to={`/user/login?${queryString}`}></Redirect>;
     }
     return children;
   }