首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用React和and / UmiJS实现AWS放大器认证

用React和and / UmiJS实现AWS放大器认证
EN

Stack Overflow用户
提问于 2020-06-23 22:57:38
回答 1查看 891关注 0票数 1

在Ant / UmiJS中实现AWS放大器认证

在UmiJS config.ts文件中,我有以下配置:

代码语言:javascript
复制
routes: [
(...){
  path: '/',
  component: '../layouts/AwsSecurityLayout',
  routes: [
    {
      path: '/',
      component: '../layouts/BasicLayout',
      authority: ['admin', 'user'],
      routes: [
        {
          path: '/links',
          name: 'fb.links',
          icon: 'BarsOutlined',
          component: './Links',

        },(...)

基本组件AwsSecurityLayout封装将守卫的路由。看起来是这样:

代码语言:javascript
复制
import React from 'react';
import { withAuthenticator } from '@aws-amplify/ui-react';
import { PageLoading } from '@ant-design/pro-layout';

class AwsSecurityLayout extends React.Component {

  render() {
    const { children } = this.props;

    if (!children) {
      return <PageLoading />;
    }

    return children;
  }
}

export default withAuthenticator(AwsSecurityLayout);

我使用了withAuthenticator函数--来自UmiJ的道具没有传递给组件,所以道具和props.child都是无敌的。

的原始文件:

代码语言:javascript
复制
import React from 'react';
import { PageLoading } from '@ant-design/pro-layout';
import { Redirect, connect, ConnectProps } from 'umi';
import { stringify } from 'querystring';
import { ConnectState } from '@/models/connect';
import { CurrentUser } from '@/models/user';

interface SecurityLayoutProps extends ConnectProps {
  loading?: boolean;
  currentUser?: CurrentUser;
}

interface SecurityLayoutState {
  isReady: boolean;
}

class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayoutState> {
  state: SecurityLayoutState = {
    isReady: false,
  };

  componentDidMount() {
    this.setState({
      isReady: true,
    });
    const { dispatch } = this.props;
    if (dispatch) {
      dispatch({
        type: 'user/fetchCurrent',
      });
    }
  }

  render() {
    const { isReady } = this.state;
    const { children, loading, currentUser } = this.props;
    // 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 (!isLogin && window.location.pathname !== '/user/login') {
      return <Redirect to={`/user/login?${queryString}`} />;
    }
    return children;
  }
}

export default connect(({ user, loading }: ConnectState) => ({
  currentUser: user.currentUser,
  loading: loading.models.user,
}))(SecurityLayout);

我只是简单地使用withAuthenticator包装组件

EN

回答 1

Stack Overflow用户

发布于 2020-06-24 17:07:41

我使用条件呈现doc:https://docs.amplify.aws/ui/auth/authenticator/q/framework/react#manage-auth-state-and-conditional-app-rendering进行了求解。

代码:

代码语言:javascript
复制
import React from 'react';
import { AmplifyAuthenticator } from '@aws-amplify/ui-react';
import { AuthState, onAuthUIStateChange } from '@aws-amplify/ui-components';

const AwsSecurityLayout: React.FunctionComponent = (props: any | undefined) => {
  const [authState, setAuthState] = React.useState<AuthState>();
  const [user, setUser] = React.useState<any | undefined>();

  React.useEffect(() => {
    return onAuthUIStateChange((nextAuthState, authData) => {
      setAuthState(nextAuthState);
      setUser(authData);
    });
  }, []);

  return authState === AuthState.SignedIn && user ? (
    props.children
  ) : (
      // TODO: change style / implement https://github.com/mzohaibqc/antd-amplify-react
      <AmplifyAuthenticator />
    );
}

export default AwsSecurityLayout;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62545028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档