/* * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0/ * * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ import * as React from 'react'; import { I18n, ConsoleLogger as Logger } from '@aws-amplify/core'; import { Auth } from '@aws-amplify/auth'; import { AuthPiece, IAuthPieceProps, IAuthPieceState } from './AuthPiece'; import AmplifyTheme from '../Amplify-UI/Amplify-UI-Theme'; import { FormSection, SectionHeader, SectionBody, SectionFooter, Input, InputLabel, Button, FormField, Link, SectionFooterPrimaryContent, SectionFooterSecondaryContent, } from '../Amplify-UI/Amplify-UI-Components-React'; import { auth } from '../Amplify-UI/data-test-attributes'; const logger = new Logger('ForgotPassword'); export interface IForgotPasswordState extends IAuthPieceState { delivery: any; } export class ForgotPassword extends AuthPiece< IAuthPieceProps, IForgotPasswordState > { constructor(props: IAuthPieceProps) { super(props); this.send = this.send.bind(this); this.submit = this.submit.bind(this); this._validAuthStates = ['forgotPassword']; this.state = { delivery: null }; } send() { const { authData = {} } = this.props; const username = this.getUsernameFromInput() || authData.username; if (!Auth || typeof Auth.forgotPassword !== 'function') { throw new Error( 'No Auth module found, please ensure @aws-amplify/auth is imported' ); } Auth.forgotPassword(username) .then(data => { logger.debug(data); this.setState({ delivery: data.CodeDeliveryDetails }); }) .catch(err => this.error(err)); } submit() { const { authData = {} } = this.props; const { code, password } = this.inputs; const username = this.getUsernameFromInput() || authData.username; if (!Auth || typeof Auth.forgotPasswordSubmit !== 'function') { throw new Error( 'No Auth module found, please ensure @aws-amplify/auth is imported' ); } Auth.forgotPasswordSubmit(username, code, password) .then(data => { logger.debug(data); this.changeState('signIn'); this.setState({ delivery: null }); }) .catch(err => this.error(err)); } sendView() { const theme = this.props.theme || AmplifyTheme; return
{this.renderUsernameField(theme)}
; } submitView() { const theme = this.props.theme || AmplifyTheme; return (
{I18n.get('Code')} * {I18n.get('New Password')} *
); } showComponent(theme) { const { authState, hide, authData = {} } = this.props; if (hide && hide.includes(ForgotPassword)) { return null; } return ( {I18n.get('Reset your password')} {this.state.delivery || authData.username ? this.submitView() : this.sendView()} {this.state.delivery || authData.username ? ( ) : ( )} {this.state.delivery || authData.username ? ( {I18n.get('Resend Code')} ) : ( this.changeState('signIn')} data-test={auth.forgotPassword.backToSignInLink} > {I18n.get('Back to Sign In')} )} ); } } /** * @deprecated use named import */ export default ForgotPassword;