|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import AuthUserContext from './context'; |
| 4 | +import { withFirebase } from '../Firebase'; |
| 5 | + |
| 6 | +const needsEmailVerification = authUser => |
| 7 | + authUser && |
| 8 | + !authUser.emailVerified && |
| 9 | + authUser.providerData |
| 10 | + .map(provider => provider.providerId) |
| 11 | + .includes('password'); |
| 12 | + |
| 13 | +const withEmailVerification = Component => { |
| 14 | + class WithEmailVerification extends React.Component { |
| 15 | + constructor(props) { |
| 16 | + super(props); |
| 17 | + |
| 18 | + this.state = { isSent: false }; |
| 19 | + } |
| 20 | + |
| 21 | + onSendEmailVerification = () => { |
| 22 | + this.props.firebase |
| 23 | + .doSendEmailVerification() |
| 24 | + .then(() => this.setState({ isSent: true })); |
| 25 | + }; |
| 26 | + |
| 27 | + render() { |
| 28 | + return ( |
| 29 | + <AuthUserContext.Consumer> |
| 30 | + {authUser => |
| 31 | + needsEmailVerification(authUser) ? ( |
| 32 | + <div> |
| 33 | + {this.state.isSent ? ( |
| 34 | + <p> |
| 35 | + E-Mail confirmation sent: Check you E-Mails (Spam |
| 36 | + folder included) for a confirmation E-Mail. |
| 37 | + Refresh this page once you confirmed your E-Mail. |
| 38 | + </p> |
| 39 | + ) : ( |
| 40 | + <p> |
| 41 | + Verify your E-Mail: Check you E-Mails (Spam folder |
| 42 | + included) for a confirmation E-Mail or send |
| 43 | + another confirmation E-Mail. |
| 44 | + </p> |
| 45 | + )} |
| 46 | + |
| 47 | + <button |
| 48 | + type="button" |
| 49 | + onClick={this.onSendEmailVerification} |
| 50 | + disabled={this.state.isSent} |
| 51 | + > |
| 52 | + Send confirmation E-Mail |
| 53 | + </button> |
| 54 | + </div> |
| 55 | + ) : ( |
| 56 | + <Component {...this.props} /> |
| 57 | + ) |
| 58 | + } |
| 59 | + </AuthUserContext.Consumer> |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return withFirebase(WithEmailVerification); |
| 65 | +}; |
| 66 | + |
| 67 | +export default withEmailVerification; |
0 commit comments