Skip to content

Commit c52387e

Browse files
SUPERCILEXsamtstern
authored andcommitted
Request Twitter email (#452)
1 parent d1d678e commit c52387e

File tree

11 files changed

+88
-168
lines changed

11 files changed

+88
-168
lines changed

auth/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Twitter app as reported by the [Twitter application manager](https://apps.twitte
9191
</resources>
9292
```
9393

94-
In addition, if you are using Smart Lock or require a user's email, you must enable the
95-
"Request email addresses from users" permission in the "Permissions" tab of your app.
94+
In addition, you must enable the "Request email addresses from users" permission
95+
in the "Permissions" tab of your Twitter app.
9696

9797
## Using FirebaseUI for Authentication
9898

auth/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ dependencies {
4444
compile "com.google.android.gms:play-services-auth:$firebase_version"
4545

4646
compile 'com.facebook.android:facebook-android-sdk:4.18.0'
47-
compile("com.twitter.sdk.android:twitter:2.2.0@aar") { transitive = true }
47+
compile("com.twitter.sdk.android:twitter:2.3.0@aar") { transitive = true }
4848

4949
// The following libraries are needed to prevent incompatibilities with the facebook
5050
// library when updating com.android.support libraries:

auth/src/main/java/com/firebase/ui/auth/IdpResponse.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import android.content.Intent;
1818
import android.os.Parcel;
1919
import android.os.Parcelable;
20+
import android.support.annotation.NonNull;
2021
import android.support.annotation.Nullable;
2122

2223
import com.firebase.ui.auth.ui.ExtraConstants;
@@ -25,39 +26,37 @@
2526
* A container that encapsulates the result of authenticating with an Identity Provider.
2627
*/
2728
public class IdpResponse implements Parcelable {
28-
2929
private final String mProviderId;
30-
@Nullable
3130
private final String mEmail;
3231
private final String mToken;
3332
private final String mSecret;
3433
private final int mErrorCode;
3534

36-
public IdpResponse(int errorCode) {
35+
private IdpResponse(int errorCode) {
3736
this(null, null, null, null, errorCode);
3837
}
3938

40-
public IdpResponse(String providerId, String email) {
41-
this(providerId, email, null, null);
39+
public IdpResponse(@NonNull String providerId, @NonNull String email) {
40+
this(providerId, email, null, null, ResultCodes.OK);
4241
}
4342

44-
public IdpResponse(String providerId, @Nullable String email, @Nullable String token) {
45-
this(providerId, email, token, null);
43+
public IdpResponse(@NonNull String providerId, @NonNull String email, @NonNull String token) {
44+
this(providerId, email, token, null, ResultCodes.OK);
4645
}
4746

4847
public IdpResponse(
49-
String providerId,
50-
@Nullable String email,
51-
@Nullable String token,
52-
@Nullable String secret) {
48+
@NonNull String providerId,
49+
@NonNull String email,
50+
@NonNull String token,
51+
@NonNull String secret) {
5352
this(providerId, email, token, secret, ResultCodes.OK);
5453
}
5554

56-
public IdpResponse(
55+
private IdpResponse(
5756
String providerId,
58-
@Nullable String email,
59-
@Nullable String token,
60-
@Nullable String secret,
57+
String email,
58+
String token,
59+
String secret,
6160
int errorCode) {
6261
mProviderId = providerId;
6362
mEmail = email;
@@ -87,11 +86,17 @@ public IdpResponse[] newArray(int size) {
8786
/**
8887
* Get the type of provider. e.g. {@link AuthUI#GOOGLE_PROVIDER}
8988
*/
90-
@Nullable
9189
public String getProviderType() {
9290
return mProviderId;
9391
}
9492

93+
/**
94+
* Get the email used to sign in.
95+
*/
96+
public String getEmail() {
97+
return mEmail;
98+
}
99+
95100
/**
96101
* Get the token received as a result of logging in with the specified IDP
97102
*/
@@ -108,14 +113,6 @@ public String getIdpSecret() {
108113
return mSecret;
109114
}
110115

111-
/**
112-
* Get the email used to sign in.
113-
*/
114-
@Nullable
115-
public String getEmail() {
116-
return mEmail;
117-
}
118-
119116
/**
120117
* Get the error code for a failed sign in
121118
*/
@@ -157,6 +154,6 @@ public static Intent getIntent(IdpResponse response) {
157154
}
158155

159156
public static Intent getErrorCodeIntent(int errorCode) {
160-
return new Intent().putExtra(ExtraConstants.EXTRA_IDP_RESPONSE, new IdpResponse(errorCode));
157+
return getIntent(new IdpResponse(errorCode));
161158
}
162159
}

auth/src/main/java/com/firebase/ui/auth/provider/FacebookProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void startLogin(Activity activity) {
108108

109109
@Override
110110
public void setAuthenticationCallback(IdpCallback callback) {
111-
this.mCallbackObject = callback;
111+
mCallbackObject = callback;
112112
}
113113

114114
@Override

auth/src/main/java/com/firebase/ui/auth/provider/TwitterProvider.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.twitter.sdk.android.core.TwitterSession;
1919
import com.twitter.sdk.android.core.identity.TwitterAuthClient;
2020

21+
import java.lang.ref.WeakReference;
22+
2123
import io.fabric.sdk.android.Fabric;
2224

2325
public class TwitterProvider extends Callback<TwitterSession> implements IdpProvider {
@@ -46,7 +48,7 @@ public String getProviderId() {
4648

4749
@Override
4850
public void setAuthenticationCallback(IdpCallback callback) {
49-
this.mCallbackObject = callback;
51+
mCallbackObject = callback;
5052
}
5153

5254
@Override
@@ -61,7 +63,7 @@ public void startLogin(Activity activity) {
6163

6264
@Override
6365
public void success(Result<TwitterSession> result) {
64-
mCallbackObject.onSuccess(createIdpResponse(result.data));
66+
mTwitterAuthClient.requestEmail(result.data, new EmailCallback(result.data, mCallbackObject));
6567
}
6668

6769
@Override
@@ -70,21 +72,47 @@ public void failure(TwitterException exception) {
7072
mCallbackObject.onFailure(new Bundle());
7173
}
7274

73-
private IdpResponse createIdpResponse(TwitterSession twitterSession) {
74-
return new IdpResponse(
75-
TwitterAuthProvider.PROVIDER_ID,
76-
null,
77-
twitterSession.getAuthToken().token,
78-
twitterSession.getAuthToken().secret);
79-
}
75+
private static class EmailCallback extends Callback<String> {
76+
private TwitterSession mTwitterSession;
77+
private WeakReference<IdpCallback> mCallbackObject;
78+
79+
public EmailCallback(TwitterSession session, IdpCallback callbackObject) {
80+
mTwitterSession = session;
81+
mCallbackObject = new WeakReference<>(callbackObject);
82+
}
83+
84+
@Override
85+
public void success(Result<String> emailResult) {
86+
onSuccess(createIdpResponse(emailResult.data));
87+
}
8088

89+
@Override
90+
public void failure(TwitterException exception) {
91+
Log.e(TAG, "Failure retrieving Twitter email. " + exception.getMessage());
92+
// If retrieving the email fails, we should still be able to sign in, but Smart Lock
93+
// and account linking won't work.
94+
onSuccess(createIdpResponse(null));
95+
}
96+
97+
private void onSuccess(IdpResponse response) {
98+
if (mCallbackObject != null) {
99+
mCallbackObject.get().onSuccess(response);
100+
}
101+
}
102+
103+
private IdpResponse createIdpResponse(String email) {
104+
return new IdpResponse(
105+
TwitterAuthProvider.PROVIDER_ID,
106+
email,
107+
mTwitterSession.getAuthToken().token,
108+
mTwitterSession.getAuthToken().secret);
109+
}
110+
}
81111

82112
public static AuthCredential createAuthCredential(IdpResponse response) {
83-
if (!response.getProviderType().equalsIgnoreCase(TwitterAuthProvider.PROVIDER_ID)){
113+
if (!response.getProviderType().equalsIgnoreCase(TwitterAuthProvider.PROVIDER_ID)) {
84114
return null;
85115
}
86-
return TwitterAuthProvider.getCredential(
87-
response.getIdpToken(),
88-
response.getIdpSecret());
116+
return TwitterAuthProvider.getCredential(response.getIdpToken(), response.getIdpSecret());
89117
}
90118
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ protected void onDestroy() {
197197
}
198198
}
199199

200-
public static Intent createIntent(
201-
Context context,
202-
FlowParameters flowParams) {
200+
public static Intent createIntent(Context context, FlowParameters flowParams) {
203201
return BaseHelper.createBaseIntent(context, AuthMethodPickerActivity.class, flowParams);
204202
}
205203
}

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,27 @@ public void onComplete(@NonNull Task<AuthResult> task) {
6969
} else {
7070
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
7171
final String email = mResponse.getEmail();
72-
mHelper.getFirebaseAuth()
73-
.fetchProvidersForEmail(email)
74-
.addOnFailureListener(new TaskFailureLogger(
75-
TAG, "Error fetching providers for email"))
76-
.addOnSuccessListener(new StartWelcomeBackFlow(email))
77-
.addOnFailureListener(new OnFailureListener() {
78-
@Override
79-
public void onFailure(@NonNull Exception e) {
80-
// TODO: What to do when signing in with Credential fails
81-
// and we can't continue to Welcome back flow without
82-
// knowing providers?
83-
}
84-
});
72+
if (email != null) {
73+
mHelper.getFirebaseAuth()
74+
.fetchProvidersForEmail(email)
75+
.addOnFailureListener(new TaskFailureLogger(
76+
TAG, "Error fetching providers for email"))
77+
.addOnSuccessListener(new StartWelcomeBackFlow(email))
78+
.addOnFailureListener(new OnFailureListener() {
79+
@Override
80+
public void onFailure(@NonNull Exception e) {
81+
// TODO: What to do when signing in with Credential fails
82+
// and we can't continue to Welcome back flow without
83+
// knowing providers?
84+
}
85+
});
86+
return;
87+
}
8588
} else {
86-
mHelper.dismissDialog();
8789
Log.e(TAG, "Unexpected exception when signing in with credential",
8890
task.getException());
8991
}
92+
mHelper.dismissDialog();
9093
}
9194
}
9295

auth/src/test/java/com/firebase/ui/auth/testhelpers/FakeAuthResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.google.firebase.auth.FirebaseUser;
1919

2020
public class FakeAuthResult implements AuthResult {
21-
FirebaseUser mFirebaseUser;
21+
private FirebaseUser mFirebaseUser;
2222

2323
public FakeAuthResult(FirebaseUser firebaseUser) {
2424
mFirebaseUser = firebaseUser;

auth/src/test/java/com/firebase/ui/auth/testhelpers/FakeProviderQueryResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.List;
2222

2323
public class FakeProviderQueryResult implements ProviderQueryResult {
24-
List<String> mProviders;
24+
private List<String> mProviders;
2525

2626
public FakeProviderQueryResult(List<String> providers) {
2727
mProviders = providers;

auth/src/test/java/com/firebase/ui/auth/ui/provider/TwitterProviderTest.java

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)