Customize AWS Amplify Authentication UI for React

Viewed 3200

I'm trying to create a customized UI for all the pages related to the authentication on a React app. I need a big form with various fields to be inserted when the user is signing up.

I was following this tutorial (https://github.com/richardzcode/Journal-AWS-Amplify-Tutorial) until a realize the versions for aws-amplify and aws-amplify-react were different.

I tried to read the documentation about it but is too superficial, I can't get it. https://aws-amplify.github.io/amplify-js/media/authentication_guide#create-your-own-ui

I tried to look up directly in the code and reproduce the same pattern that I found there, no success.

Here my Auth component:

  import { Authenticator } from 'aws-amplify-react'

  import {
    LoginForm,
    RegisterForm,
    ConfirmRegisterForm,
    VerifyContactForm,
    ForgotPasswordForm
  } from '../components'

  export default class Login extends Component {
    render () {
      return (
        <Authenticator hideDefault>
          <LoginForm />
          <RegisterForm />
          <ConfirmRegisterForm />
          <VerifyContactForm />
          <ForgotPasswordForm />
        </Authenticator>
      )
    }
  }

And here my LoginForm:

    import React from 'react'
    import { Button, Form, Grid, Header, Image, Message, Segment } from 'semantic-ui-react'
    import Auth from '@aws-amplify/auth'
    import { ConsoleLogger as Logger } from '@aws-amplify/core'
    import { AuthPiece } from 'aws-amplify-react'

    const logger = new Logger('LoginForm')

    class LoginForm extends AuthPiece {
      constructor (props) {
        super(props)
        this.signIn = this.signIn.bind(this)
        this._validAuthStates = ['signIn', 'signedOut', 'signedUp']
        this.state = {}
        console.log('this.props->', JSON.stringify(this.props))
      }

      signIn () {
        const { username, password } = this.inputs
        logger.debug(`username: ${username}`)

        Auth.signIn(username, password)
        .then(user => this.changeState('signedIn', user))
        .catch(err => this.error(err))
      }

      showComponent (theme) {        
        const { hide = [] } = this.props

       if (hide && hide.includes(LoginForm)) {
        return null
       }

       return (
         ...
       )
    }
  }
  export default LoginForm

When I go to the login form the authState is always as loading. I can't make them work properly because of this.

Any help is welcome.

1 Answers

Rather than extending the AuthPiece, you'll need to extend the SignIn component and be sure to import the SignIn component. I haven't done this in react, but I have in react-native, so it should be fairly similar.

import { SignIn } from "aws-amplify-react";

class CustomSignIn extends SignIn {
...
}

And in your app.js

<Authenticator hideDefault={true}>
    <LoginForm override={SignIn}{...this.props} />
    ....
</Authenticator>
Related