Skip to content

Commit bf9ef29

Browse files
author
Aaron Mandle
committed
fix email -> facebook linking
Change-Id: Ia994a5ac41a1c676a324d051d3babc2a0480540d
1 parent 902b3d7 commit bf9ef29

File tree

5 files changed

+142
-44
lines changed

5 files changed

+142
-44
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.firebase.ui.auth.ui;
16+
17+
import com.firebase.ui.auth.provider.FacebookProvider;
18+
import com.firebase.ui.auth.provider.GoogleProvider;
19+
import com.firebase.ui.auth.provider.IDPResponse;
20+
import com.google.firebase.auth.AuthCredential;
21+
import com.google.firebase.auth.FacebookAuthProvider;
22+
import com.google.firebase.auth.GoogleAuthProvider;
23+
24+
public class AuthCredentialHelper {
25+
public static AuthCredential getAuthCredential(IDPResponse idpResponse) {
26+
switch (idpResponse.getProviderType()) {
27+
case GoogleAuthProvider.PROVIDER_ID:
28+
return GoogleProvider.createAuthCredential(idpResponse);
29+
case FacebookAuthProvider.PROVIDER_ID:
30+
return FacebookProvider.createAuthCredential(idpResponse);
31+
default:
32+
return null;
33+
}
34+
}
35+
}

auth/src/main/java/com/firebase/ui/auth/ui/account_link/AccountLinkInitActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void onComplete(@NonNull Task<ProviderQueryResult> task) {
108108
startActivityForResult(WelcomeBackPasswordPrompt.createIntent(
109109
getApplicationContext(),
110110
mActivityHelper.flowParams,
111-
email),
111+
mIdpResponse),
112112
RC_WELCOME_BACK_PASSWORD_PROMPT);
113113
} else {
114114
// existing account but has a different IDP linked

auth/src/main/java/com/firebase/ui/auth/ui/account_link/WelcomeBackIDPPrompt.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.firebase.ui.auth.provider.IDPResponse;
3333
import com.firebase.ui.auth.ui.ActivityHelper;
3434
import com.firebase.ui.auth.ui.AppCompatBase;
35+
import com.firebase.ui.auth.ui.AuthCredentialHelper;
3536
import com.firebase.ui.auth.ui.ExtraConstants;
3637
import com.firebase.ui.auth.ui.FlowParameters;
3738
import com.google.android.gms.tasks.OnCompleteListener;
@@ -52,17 +53,6 @@ public class WelcomeBackIDPPrompt extends AppCompatBase
5253
private IDPResponse mPrevIdpResponse;
5354
private AuthCredential mPrevCredential;
5455

55-
private static AuthCredential getAuthCredential(IDPResponse idpResponse) {
56-
switch (idpResponse.getProviderType()) {
57-
case GoogleAuthProvider.PROVIDER_ID:
58-
return GoogleProvider.createAuthCredential(idpResponse);
59-
case FacebookAuthProvider.PROVIDER_ID:
60-
return FacebookProvider.createAuthCredential(idpResponse);
61-
default:
62-
return null;
63-
}
64-
}
65-
6656

6757
@Override
6858
protected void onCreate(Bundle savedInstanceState) {
@@ -91,7 +81,7 @@ protected void onCreate(Bundle savedInstanceState) {
9181
}
9282

9383
if (mPrevIdpResponse != null) {
94-
mPrevCredential = getAuthCredential(mPrevIdpResponse);
84+
mPrevCredential = AuthCredentialHelper.getAuthCredential(mPrevIdpResponse);
9585
}
9686

9787
if (mIdpProvider == null) {
@@ -154,7 +144,7 @@ private void next(IDPResponse newIdpResponse) {
154144
return; // do nothing
155145
}
156146
AuthCredential newCredential;
157-
newCredential = getAuthCredential(newIdpResponse);
147+
newCredential = AuthCredentialHelper.getAuthCredential(newIdpResponse);
158148
if (newCredential == null) {
159149
Log.e(TAG, "No credential returned");
160150
finish(Activity.RESULT_FIRST_USER, new Intent());

auth/src/main/java/com/firebase/ui/auth/ui/account_link/WelcomeBackPasswordPrompt.java

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import android.content.Context;
1818
import android.content.Intent;
1919
import android.graphics.Typeface;
20+
import android.net.Uri;
2021
import android.os.Bundle;
2122
import android.support.annotation.NonNull;
2223
import android.support.design.widget.TextInputLayout;
@@ -30,8 +31,10 @@
3031
import android.widget.TextView;
3132

3233
import com.firebase.ui.auth.R;
34+
import com.firebase.ui.auth.provider.IDPResponse;
3335
import com.firebase.ui.auth.ui.ActivityHelper;
3436
import com.firebase.ui.auth.ui.AppCompatBase;
37+
import com.firebase.ui.auth.ui.AuthCredentialHelper;
3538
import com.firebase.ui.auth.ui.ExtraConstants;
3639
import com.firebase.ui.auth.ui.FlowParameters;
3740
import com.firebase.ui.auth.ui.email.PasswordToggler;
@@ -41,22 +44,25 @@
4144
import com.google.android.gms.tasks.Task;
4245
import com.google.firebase.auth.AuthCredential;
4346
import com.google.firebase.auth.AuthResult;
44-
import com.google.firebase.auth.EmailAuthProvider;
47+
import com.google.firebase.auth.FirebaseAuth;
4548
import com.google.firebase.auth.FirebaseUser;
4649

4750
public class WelcomeBackPasswordPrompt extends AppCompatBase implements View.OnClickListener {
51+
private static final int RC_YOLO_SAVE = 3;
4852
final StyleSpan bold = new StyleSpan(Typeface.BOLD);
4953
private String mEmail;
5054
private TextInputLayout mPasswordLayout;
5155
private EditText mPasswordField;
56+
private IDPResponse mIdpResponse;
5257

5358
@Override
5459
protected void onCreate(Bundle savedInstanceState) {
5560
super.onCreate(savedInstanceState);
5661
setTitle(R.string.sign_in_title);
5762
setContentView(R.layout.welcome_back_password_prompt_layout);
5863
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();
6066
TextView bodyTextView = (TextView) findViewById(R.id.welcome_back_password_body);
6167
String bodyText = getResources().getString(R.string.welcome_back_password_prompt_body);
6268
bodyText = String.format(bodyText, mEmail);
@@ -88,28 +94,63 @@ public void onClick(View view) {
8894
}
8995
}
9096

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());
108102
}
109103
}
110104

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) {
112153
return ActivityHelper.createBaseIntent(context, WelcomeBackPasswordPrompt.class, flowParams)
113-
.putExtra(ExtraConstants.EXTRA_EMAIL, email);
154+
.putExtra(ExtraConstants.EXTRA_IDP_RESPONSE, response);
114155
}
115156
}

auth/src/main/java/com/firebase/ui/auth/ui/idp/AuthMethodPickerActivity.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import android.content.Context;
1818
import android.content.Intent;
19+
import android.net.Uri;
1920
import android.os.Bundle;
2021
import android.support.annotation.NonNull;
2122
import android.util.Log;
@@ -32,7 +33,9 @@
3233
import com.firebase.ui.auth.provider.IDPResponse;
3334
import com.firebase.ui.auth.ui.ActivityHelper;
3435
import com.firebase.ui.auth.ui.FlowParameters;
36+
import com.firebase.ui.auth.ui.account_link.SaveCredentialsActivity;
3537
import com.firebase.ui.auth.ui.account_link.WelcomeBackIDPPrompt;
38+
import com.firebase.ui.auth.ui.account_link.WelcomeBackPasswordPrompt;
3639
import com.firebase.ui.auth.ui.email.EmailHintContainerActivity;
3740
import com.google.android.gms.tasks.OnCompleteListener;
3841
import com.google.android.gms.tasks.OnFailureListener;
@@ -43,6 +46,7 @@
4346
import com.google.firebase.auth.FacebookAuthProvider;
4447
import com.google.firebase.auth.FirebaseAuth;
4548
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
49+
import com.google.firebase.auth.FirebaseUser;
4650
import com.google.firebase.auth.GoogleAuthProvider;
4751
import com.google.firebase.auth.ProviderQueryResult;
4852

@@ -58,6 +62,7 @@ public class AuthMethodPickerActivity
5862

5963
private static final int RC_EMAIL_FLOW = 2;
6064
private static final int RC_WELCOME_BACK_IDP = 3;
65+
private static final int RC_ACCOUNT_LINK = 4;
6166
private static final String TAG = "AuthMethodPicker";
6267
private ArrayList<IDPProvider> mIdpProviders;
6368

@@ -126,6 +131,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
126131
if (resultCode == RESULT_OK) {
127132
finish(RESULT_OK, new Intent());
128133
}
134+
} else if (requestCode == RC_ACCOUNT_LINK) {
135+
finish(RESULT_OK, new Intent());
129136
} else if (requestCode == RC_WELCOME_BACK_IDP) {
130137
finish(resultCode, new Intent());
131138
} else {
@@ -154,19 +161,44 @@ public void onComplete(@NonNull Task<AuthResult> task) {
154161
@Override
155162
public void onComplete(
156163
@NonNull Task<ProviderQueryResult> task) {
157-
startActivityForResult(WelcomeBackIDPPrompt.createIntent(
158-
mActivityHelper.getApplicationContext(),
159-
mActivityHelper.flowParams,
160-
task.getResult().getProviders().get(0),
161-
response,
162-
email
163-
), RC_WELCOME_BACK_IDP);
164-
164+
String provider = task.getResult().getProviders().get(0);
165+
if (provider.equals(EmailAuthProvider.PROVIDER_ID)) {
166+
startActivityForResult(
167+
WelcomeBackPasswordPrompt.createIntent(
168+
mActivityHelper.getApplicationContext(),
169+
mActivityHelper.flowParams,
170+
response
171+
), RC_WELCOME_BACK_IDP);
172+
173+
} else {
174+
startActivityForResult(
175+
WelcomeBackIDPPrompt.createIntent(
176+
mActivityHelper.getApplicationContext(),
177+
mActivityHelper.flowParams,
178+
task.getResult().getProviders().get(0),
179+
response,
180+
email
181+
), RC_WELCOME_BACK_IDP);
182+
}
165183
}
166184
});
167185
}
168186
} else {
169-
finish(RESULT_OK, new Intent());
187+
FirebaseUser firebaseUser = task.getResult().getUser();
188+
String photoUrl = null;
189+
Uri photoUri = firebaseUser.getPhotoUrl();
190+
if (photoUri != null) {
191+
photoUrl = photoUri.toString();
192+
}
193+
startActivityForResult(SaveCredentialsActivity.createIntent(
194+
mActivityHelper.getApplicationContext(),
195+
mActivityHelper.flowParams,
196+
firebaseUser.getDisplayName(),
197+
firebaseUser.getEmail(),
198+
null,
199+
firebaseUser.getProviderId(),
200+
photoUrl
201+
), RC_ACCOUNT_LINK);
170202
}
171203
}
172204
}

0 commit comments

Comments
 (0)