|
1 |
| -import React from 'react'; |
| 1 | +import React, { Component } from 'react'; |
2 | 2 |
|
3 | 3 | import { AuthUserContext, withAuthorization } from '../Session';
|
| 4 | +import { withFirebase } from '../Firebase'; |
4 | 5 | import { PasswordForgetForm } from '../PasswordForget';
|
5 | 6 | import PasswordChangeForm from '../PasswordChange';
|
6 | 7 |
|
| 8 | +const SIGN_IN_METHODS = [ |
| 9 | + { |
| 10 | + id: 'password', |
| 11 | + provider: null, |
| 12 | + }, |
| 13 | + { |
| 14 | + id: 'google.com', |
| 15 | + provider: 'googleProvider', |
| 16 | + }, |
| 17 | + { |
| 18 | + id: 'facebook.com', |
| 19 | + provider: 'facebookProvider', |
| 20 | + }, |
| 21 | + { |
| 22 | + id: 'twitter.com', |
| 23 | + provider: 'twitterProvider', |
| 24 | + }, |
| 25 | +]; |
| 26 | + |
7 | 27 | const AccountPage = () => (
|
8 | 28 | <AuthUserContext.Consumer>
|
9 | 29 | {authUser => (
|
10 | 30 | <div>
|
11 | 31 | <h1>Account: {authUser.email}</h1>
|
12 | 32 | <PasswordForgetForm />
|
13 | 33 | <PasswordChangeForm />
|
| 34 | + <LoginManagement authUser={authUser} /> |
14 | 35 | </div>
|
15 | 36 | )}
|
16 | 37 | </AuthUserContext.Consumer>
|
17 | 38 | );
|
18 | 39 |
|
| 40 | +class LoginManagementBase extends Component { |
| 41 | + constructor(props) { |
| 42 | + super(props); |
| 43 | + |
| 44 | + this.state = { |
| 45 | + activeSignInMethods: [], |
| 46 | + error: null, |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + componentDidMount() { |
| 51 | + this.fetchSignInMethods(); |
| 52 | + } |
| 53 | + |
| 54 | + fetchSignInMethods = () => { |
| 55 | + this.props.firebase.auth |
| 56 | + .fetchSignInMethodsForEmail(this.props.authUser.email) |
| 57 | + .then(activeSignInMethods => |
| 58 | + this.setState({ activeSignInMethods, error: null }), |
| 59 | + ) |
| 60 | + .catch(error => this.setState({ error })); |
| 61 | + }; |
| 62 | + |
| 63 | + onSocialLoginLink = provider => { |
| 64 | + this.props.firebase.auth.currentUser |
| 65 | + .linkWithPopup(this.props.firebase[provider]) |
| 66 | + .then(this.fetchSignInMethods) |
| 67 | + .catch(error => this.setState({ error })); |
| 68 | + }; |
| 69 | + |
| 70 | + onDefaultLoginLink = password => { |
| 71 | + const credential = this.props.firebase.emailAuthProvider.credential( |
| 72 | + this.props.authUser.email, |
| 73 | + password, |
| 74 | + ); |
| 75 | + |
| 76 | + this.props.firebase.auth.currentUser |
| 77 | + .linkAndRetrieveDataWithCredential(credential) |
| 78 | + .then(this.fetchSignInMethods) |
| 79 | + .catch(error => this.setState({ error })); |
| 80 | + }; |
| 81 | + |
| 82 | + onUnlink = providerId => { |
| 83 | + this.props.firebase.auth.currentUser |
| 84 | + .unlink(providerId) |
| 85 | + .then(this.fetchSignInMethods) |
| 86 | + .catch(error => this.setState({ error })); |
| 87 | + }; |
| 88 | + |
| 89 | + render() { |
| 90 | + const { activeSignInMethods, error } = this.state; |
| 91 | + |
| 92 | + return ( |
| 93 | + <div> |
| 94 | + Sign In Methods: |
| 95 | + <ul> |
| 96 | + {SIGN_IN_METHODS.map(signInMethod => { |
| 97 | + const onlyOneLeft = activeSignInMethods.length === 1; |
| 98 | + const isEnabled = activeSignInMethods.includes( |
| 99 | + signInMethod.id, |
| 100 | + ); |
| 101 | + |
| 102 | + return ( |
| 103 | + <li key={signInMethod.id}> |
| 104 | + {signInMethod.id === 'password' ? ( |
| 105 | + <DefaultLoginToggle |
| 106 | + onlyOneLeft={onlyOneLeft} |
| 107 | + isEnabled={isEnabled} |
| 108 | + signInMethod={signInMethod} |
| 109 | + onLink={this.onDefaultLoginLink} |
| 110 | + onUnlink={this.onUnlink} |
| 111 | + /> |
| 112 | + ) : ( |
| 113 | + <SocialLoginToggle |
| 114 | + onlyOneLeft={onlyOneLeft} |
| 115 | + isEnabled={isEnabled} |
| 116 | + signInMethod={signInMethod} |
| 117 | + onLink={this.onSocialLoginLink} |
| 118 | + onUnlink={this.onUnlink} |
| 119 | + /> |
| 120 | + )} |
| 121 | + </li> |
| 122 | + ); |
| 123 | + })} |
| 124 | + </ul> |
| 125 | + {error && error.message} |
| 126 | + </div> |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +const SocialLoginToggle = ({ |
| 132 | + onlyOneLeft, |
| 133 | + isEnabled, |
| 134 | + signInMethod, |
| 135 | + onLink, |
| 136 | + onUnlink, |
| 137 | +}) => |
| 138 | + isEnabled ? ( |
| 139 | + <button |
| 140 | + type="button" |
| 141 | + onClick={() => onUnlink(signInMethod.id)} |
| 142 | + disabled={onlyOneLeft} |
| 143 | + > |
| 144 | + Deactivate {signInMethod.id} |
| 145 | + </button> |
| 146 | + ) : ( |
| 147 | + <button |
| 148 | + type="button" |
| 149 | + onClick={() => onLink(signInMethod.provider)} |
| 150 | + > |
| 151 | + Link {signInMethod.id} |
| 152 | + </button> |
| 153 | + ); |
| 154 | + |
| 155 | +class DefaultLoginToggle extends Component { |
| 156 | + constructor(props) { |
| 157 | + super(props); |
| 158 | + |
| 159 | + this.state = { passwordOne: '', passwordTwo: '' }; |
| 160 | + } |
| 161 | + |
| 162 | + onSubmit = event => { |
| 163 | + event.preventDefault(); |
| 164 | + |
| 165 | + this.props.onLink(this.state.passwordOne); |
| 166 | + this.setState({ passwordOne: '', passwordTwo: '' }); |
| 167 | + }; |
| 168 | + |
| 169 | + onChange = event => { |
| 170 | + this.setState({ [event.target.name]: event.target.value }); |
| 171 | + }; |
| 172 | + |
| 173 | + render() { |
| 174 | + const { |
| 175 | + onlyOneLeft, |
| 176 | + isEnabled, |
| 177 | + signInMethod, |
| 178 | + onUnlink, |
| 179 | + } = this.props; |
| 180 | + |
| 181 | + const { passwordOne, passwordTwo } = this.state; |
| 182 | + |
| 183 | + const isInvalid = |
| 184 | + passwordOne !== passwordTwo || passwordOne === ''; |
| 185 | + |
| 186 | + return isEnabled ? ( |
| 187 | + <button |
| 188 | + type="button" |
| 189 | + onClick={() => onUnlink(signInMethod.id)} |
| 190 | + disabled={onlyOneLeft} |
| 191 | + > |
| 192 | + Deactivate {signInMethod.id} |
| 193 | + </button> |
| 194 | + ) : ( |
| 195 | + <form onSubmit={this.onSubmit}> |
| 196 | + <input |
| 197 | + name="passwordOne" |
| 198 | + value={passwordOne} |
| 199 | + onChange={this.onChange} |
| 200 | + type="password" |
| 201 | + placeholder="New Password" |
| 202 | + /> |
| 203 | + <input |
| 204 | + name="passwordTwo" |
| 205 | + value={passwordTwo} |
| 206 | + onChange={this.onChange} |
| 207 | + type="password" |
| 208 | + placeholder="Confirm New Password" |
| 209 | + /> |
| 210 | + |
| 211 | + <button disabled={isInvalid} type="submit"> |
| 212 | + Link {signInMethod.id} |
| 213 | + </button> |
| 214 | + </form> |
| 215 | + ); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +const LoginManagement = withFirebase(LoginManagementBase); |
| 220 | + |
19 | 221 | const condition = authUser => !!authUser;
|
20 | 222 |
|
21 | 223 | export default withAuthorization(condition)(AccountPage);
|
0 commit comments