简体   繁体   English

隐藏了用于 React 的 AWS Amplify 身份验证自定义 UI

[英]AWS Amplify Authentication Custom UI for React is hidden

I'm trying to customize AWS Amplify Authentication UI.我正在尝试自定义 AWS Amplify 身份验证 UI。 (just for sign up page) (仅用于注册页面)

I created React App using create-react-app to test Amplify, and I want to change sign-up form.我使用 create-react-app 创建了 React App 来测试 Amplify,我想更改注册表。 I want sign-up form without phone number required, so I created 'MySignUp.jsx' which extends SignUp Component.我想要不需要电话号码的注册表单,所以我创建了扩展 SignUp 组件的“MySignUp.jsx”。 However, when I clicked the sign up button, my sign up form was not displayed.但是,当我单击注册按钮时,并没有显示我的注册表单。 I'm wondering why this happened and how to solve this.我想知道为什么会发生这种情况以及如何解决这个问题。

Here is my App.js这是我的 App.js

import * as React from "react"
import logo from './logo.svg';
import './App.css';
import awsconfig from './aws-exports';

import Amplify from 'aws-amplify';

import {
  ConfirmSignIn,
  ConfirmSignUp,
  ForgotPassword,
  SignIn,
  VerifyContact,
  withAuthenticator,
} from "aws-amplify-react";
import MySignUp from './MySignUp';

Amplify.configure(awsconfig);

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default withAuthenticator(App,false, [
  <SignIn />,
  <ConfirmSignIn />,
  <VerifyContact />,
  <MySignUp override={'SignUp'} />,
  <ConfirmSignUp />,
  <ForgotPassword />,
])

Here is my MySignUp.jsx这是我的 MySignUp.jsx

import React from 'react';

import { Auth } from 'aws-amplify';
import {
    SignUp,
    FormSection,
    SectionHeader,
    SectionBody,
    SectionFooter,
    InputRow,
    ButtonRow,
    Link,
} from 'aws-amplify-react';

export default class MySignUp extends SignUp {

    signUp() {
        const { username, password, email } = this.inputs;
        const param = {
            username: username,
            password: password,
            attributes:{
                email: email,
            }
        }
        Auth.signUp(param)
            .then(() => this.changeState('confirmSignUp', username))
            .catch(err => this.error(err));
    }

    showComponent(theme) {
        const { hide } = this.props;
        if (hide && hide.includes(SignUp)) { return null; }

        return (
            <FormSection theme={theme}>
                <SectionHeader theme={theme}>{'Sign Up Account'}</SectionHeader>
                <SectionBody theme={theme}>
                <InputRow
                        autoFocus
                        placeholder={'Username'}
                        theme={theme}
                        key="username"
                        name="username"
                        onChange={this.handleInputChange}
                    />
                    <InputRow
                        placeholder={'Password'}
                        theme={theme}
                        type="password"
                        key="password"
                        name="password"
                        onChange={this.handleInputChange}
                    />
                    <InputRow
                        placeholder={'Email'}
                        theme={theme}
                        key="email"
                        name="email"
                        onChange={this.handleInputChange}
                    />
                    <ButtonRow onClick={this.signUp} theme={theme}>
                        {'Sign Up'}
                    </ButtonRow>
                </SectionBody>
                <SectionFooter theme={theme}>
                    <div style={theme.col6}>
                        <Link theme={theme} onClick={() => this.changeState('confirmSignUp')}>
                            {'Confirm a Code'}
                        </Link>
                    </div>
                    <div style={Object.assign({textAlign: 'right'}, theme.col6)}>
                        <Link theme={theme} onClick={() => this.changeState('signIn')}>
                            {'Sign In'}
                        </Link>
                    </div>
                </SectionFooter>
            </FormSection>
        )
    }
}

I have faced into the same problem.我遇到了同样的问题。 So what I did to get it works is removing this:所以我为让它工作所做的就是删除这个:

const { hide } = this.props;
        if (hide && hide.includes(SignUp)) { return null; }

Let me know if it works让我知道它是否有效

The main reason is why you are inheriting from the SignUp class, and when you specify in the next block of code that your MySignUp component overwrites the SignUp component, you are inherently hiding the SignUp component.主要原因是您从 SignUp 类继承的原因,并且当您在下一个代码块中指定 MySignUp 组件覆盖 SignUp 组件时,您本质上隐藏了 SignUp 组件。

export default withAuthenticator(App,false, [
  <SignIn />,
  <ConfirmSignIn />,
  <VerifyContact />,
  <MySignUp override={'SignUp'} />,
  <ConfirmSignUp />,
  <ForgotPassword />,
])

What the Authenticator component does is include it in the array of components to be hidden: https://github.com/aws-amplify/amplify-js/blob/master/packages/aws-amplify-react/src/Auth/Authenticator.tsx#L259 Authenticator 组件的作用是将其包含在要隐藏的组件数组中: https : //github.com/aws-amplify/amplify-js/blob/master/packages/aws-amplify-react/src/Auth/Authenticator .tsx#L259

hide = hide.filter(
            component => !props_children.find(child => child.type === component)
        );

There are two options:有两种选择:

  1. The first is to eliminate those lines of code, as @brian-nguyen has mentioned第一个是消除那些代码行,正如@brian-nguyen 提到的
    const { hide } = this.props;
        if (hide && hide.includes(SignUp)) { return null; }
  1. The second is to modify them to include your own component as follows:第二个是修改它们以包含您自己的组件,如下所示:
    const { hide } = this.props;
        if (hide && hide.includes(MySignUp)) { return null; }

I hope this is helpful!!!我希望这是有帮助的!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM