|
17 | 17 | import android.content.Context;
|
18 | 18 | import android.content.Intent;
|
19 | 19 | import android.graphics.Typeface;
|
| 20 | +import android.net.Uri; |
20 | 21 | import android.os.Bundle;
|
21 | 22 | import android.support.annotation.NonNull;
|
22 | 23 | import android.support.design.widget.TextInputLayout;
|
|
30 | 31 | import android.widget.TextView;
|
31 | 32 |
|
32 | 33 | import com.firebase.ui.auth.R;
|
| 34 | +import com.firebase.ui.auth.provider.IDPResponse; |
33 | 35 | import com.firebase.ui.auth.ui.ActivityHelper;
|
34 | 36 | import com.firebase.ui.auth.ui.AppCompatBase;
|
| 37 | +import com.firebase.ui.auth.ui.AuthCredentialHelper; |
35 | 38 | import com.firebase.ui.auth.ui.ExtraConstants;
|
36 | 39 | import com.firebase.ui.auth.ui.FlowParameters;
|
37 | 40 | import com.firebase.ui.auth.ui.email.PasswordToggler;
|
|
41 | 44 | import com.google.android.gms.tasks.Task;
|
42 | 45 | import com.google.firebase.auth.AuthCredential;
|
43 | 46 | import com.google.firebase.auth.AuthResult;
|
44 |
| -import com.google.firebase.auth.EmailAuthProvider; |
| 47 | +import com.google.firebase.auth.FirebaseAuth; |
45 | 48 | import com.google.firebase.auth.FirebaseUser;
|
46 | 49 |
|
47 | 50 | public class WelcomeBackPasswordPrompt extends AppCompatBase implements View.OnClickListener {
|
| 51 | + private static final int RC_YOLO_SAVE = 3; |
48 | 52 | final StyleSpan bold = new StyleSpan(Typeface.BOLD);
|
49 | 53 | private String mEmail;
|
50 | 54 | private TextInputLayout mPasswordLayout;
|
51 | 55 | private EditText mPasswordField;
|
| 56 | + private IDPResponse mIdpResponse; |
52 | 57 |
|
53 | 58 | @Override
|
54 | 59 | protected void onCreate(Bundle savedInstanceState) {
|
55 | 60 | super.onCreate(savedInstanceState);
|
56 | 61 | setTitle(R.string.sign_in_title);
|
57 | 62 | setContentView(R.layout.welcome_back_password_prompt_layout);
|
58 | 63 | mPasswordLayout = (TextInputLayout) findViewById(R.id.password_layout);
|
59 |
| - mEmail = getIntent().getStringExtra(ExtraConstants.EXTRA_EMAIL); |
| 64 | + mIdpResponse = getIntent().getParcelableExtra(ExtraConstants.EXTRA_IDP_RESPONSE); |
| 65 | + mEmail = mIdpResponse.getEmail(); |
60 | 66 | TextView bodyTextView = (TextView) findViewById(R.id.welcome_back_password_body);
|
61 | 67 | String bodyText = getResources().getString(R.string.welcome_back_password_prompt_body);
|
62 | 68 | bodyText = String.format(bodyText, mEmail);
|
@@ -88,28 +94,63 @@ public void onClick(View view) {
|
88 | 94 | }
|
89 | 95 | }
|
90 | 96 |
|
91 |
| - private void next(String email, String password) { |
92 |
| - FirebaseUser currentUser = mActivityHelper.getCurrentUser(); |
93 |
| - AuthCredential emailCredential = EmailAuthProvider.getCredential(email, password); |
94 |
| - if (currentUser != null) { |
95 |
| - Task<AuthResult> authResultTask = currentUser.linkWithCredential(emailCredential); |
96 |
| - authResultTask.addOnCompleteListener(new OnCompleteListener<AuthResult>() { |
97 |
| - @Override |
98 |
| - public void onComplete(@NonNull Task<AuthResult> task) { |
99 |
| - finish(RESULT_OK, new Intent()); |
100 |
| - } |
101 |
| - }); |
102 |
| - authResultTask.addOnFailureListener(new OnFailureListener() { |
103 |
| - @Override |
104 |
| - public void onFailure(@NonNull Exception ex) { |
105 |
| - mPasswordLayout.setError(ex.getLocalizedMessage()); |
106 |
| - } |
107 |
| - }); |
| 97 | + @Override |
| 98 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 99 | + super.onActivityResult(requestCode, resultCode, data); |
| 100 | + if (requestCode == RC_YOLO_SAVE) { |
| 101 | + finish(RESULT_OK, new Intent()); |
108 | 102 | }
|
109 | 103 | }
|
110 | 104 |
|
111 |
| - public static Intent createIntent(Context context, FlowParameters flowParams, String email) { |
| 105 | + private void next(String email, final String password) { |
| 106 | + final FirebaseAuth firebaseAuth = mActivityHelper.getFirebaseAuth(); |
| 107 | + firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener( |
| 108 | + new OnCompleteListener<AuthResult>() { |
| 109 | + @Override |
| 110 | + public void onComplete(@NonNull Task<AuthResult> task) { |
| 111 | + if (task.isSuccessful()) { |
| 112 | + AuthCredential authCredential = |
| 113 | + AuthCredentialHelper.getAuthCredential(mIdpResponse); |
| 114 | + task.getResult().getUser().linkWithCredential(authCredential); |
| 115 | + firebaseAuth.signOut(); |
| 116 | + |
| 117 | + firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener( |
| 118 | + new OnCompleteListener<AuthResult>() { |
| 119 | + @Override |
| 120 | + public void onComplete(@NonNull Task<AuthResult> task) { |
| 121 | + FirebaseUser firebaseUser = task.getResult().getUser(); |
| 122 | + String photoUrl = null; |
| 123 | + Uri photoUri = firebaseUser.getPhotoUrl(); |
| 124 | + if (photoUri != null) { |
| 125 | + photoUrl = photoUri.toString(); |
| 126 | + } |
| 127 | + |
| 128 | + startActivityForResult( |
| 129 | + SaveCredentialsActivity.createIntent( |
| 130 | + mActivityHelper.getApplicationContext(), |
| 131 | + mActivityHelper.flowParams, |
| 132 | + firebaseUser.getDisplayName(), |
| 133 | + firebaseUser.getEmail(), |
| 134 | + password, |
| 135 | + null, |
| 136 | + photoUrl |
| 137 | + ), RC_YOLO_SAVE); |
| 138 | + } |
| 139 | + } |
| 140 | + ); |
| 141 | + } else { |
| 142 | + String error = task.getException().getLocalizedMessage(); |
| 143 | + mPasswordLayout.setError(error); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + public static Intent createIntent( |
| 150 | + Context context, |
| 151 | + FlowParameters flowParams, |
| 152 | + IDPResponse response) { |
112 | 153 | return ActivityHelper.createBaseIntent(context, WelcomeBackPasswordPrompt.class, flowParams)
|
113 |
| - .putExtra(ExtraConstants.EXTRA_EMAIL, email); |
| 154 | + .putExtra(ExtraConstants.EXTRA_IDP_RESPONSE, response); |
114 | 155 | }
|
115 | 156 | }
|
0 commit comments