Skip to content

Add new user information #1355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class SignedInActivity extends AppCompatActivity {
@BindView(R.id.user_display_name) TextView mUserDisplayName;
@BindView(R.id.user_phone_number) TextView mUserPhoneNumber;
@BindView(R.id.user_enabled_providers) TextView mEnabledProviders;
@BindView(R.id.user_is_new) TextView mIsNewUser;

public static Intent createIntent(Context context, IdpResponse idpResponse) {
return new Intent().setClass(context, SignedInActivity.class)
Expand All @@ -85,7 +86,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {

setContentView(R.layout.signed_in_layout);
ButterKnife.bind(this);
populateProfile();
populateProfile(response);
populateIdpToken(response);
}

Expand Down Expand Up @@ -137,7 +138,7 @@ public void onComplete(@NonNull Task<Void> task) {
});
}

private void populateProfile() {
private void populateProfile(@Nullable IdpResponse response) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user.getPhotoUrl() != null) {
GlideApp.with(this)
Expand All @@ -153,6 +154,13 @@ private void populateProfile() {
mUserDisplayName.setText(
TextUtils.isEmpty(user.getDisplayName()) ? "No display name" : user.getDisplayName());

if (response == null) {
mIsNewUser.setVisibility(View.GONE);
} else {
mIsNewUser.setVisibility(View.VISIBLE);
mIsNewUser.setText(response.isNewUser() ? "New user" : "Existing user");
}

List<String> providers = new ArrayList<>();
if (user.getProviderData().isEmpty()) {
providers.add("Anonymous");
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/signed_in_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
android:layout_height="wrap_content"
android:textIsSelectable="true" />

<TextView
android:id="@+id/user_is_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false" />

</LinearLayout>

</LinearLayout>
Expand Down
53 changes: 49 additions & 4 deletions auth/src/main/java/com/firebase/ui/auth/IdpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.firebase.ui.auth.data.model.User;
import com.firebase.ui.auth.util.ExtraConstants;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.auth.TwitterAuthProvider;

Expand All @@ -42,6 +43,7 @@ public IdpResponse createFromParcel(Parcel in) {
in.<User>readParcelable(User.class.getClassLoader()),
in.readString(),
in.readString(),
in.readInt() == 1,
(FirebaseUiException) in.readSerializable()
);
}
Expand All @@ -56,28 +58,32 @@ public IdpResponse[] newArray(int size) {

private final String mToken;
private final String mSecret;
private final boolean mIsNewUser;

private final FirebaseUiException mException;

private IdpResponse(@NonNull FirebaseUiException e) {
this(null, null, null, e);
this(null, null, null, false, e);
}

private IdpResponse(
@NonNull User user,
@Nullable String token,
@Nullable String secret) {
this(user, token, secret, null);
@Nullable String secret,
boolean isNewUser) {
this(user, token, secret, isNewUser, null);
}

private IdpResponse(
User user,
String token,
String secret,
boolean isNewUser,
FirebaseUiException e) {
mUser = user;
mToken = token;
mSecret = secret;
mIsNewUser = isNewUser;
mException = e;
}

Expand All @@ -96,6 +102,12 @@ public static IdpResponse fromResultIntent(@Nullable Intent resultIntent) {
}
}

@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public IdpResponse withResult(AuthResult result) {
return mutate().setNewUser(result.getAdditionalUserInfo().isNewUser()).build();
}

@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public static IdpResponse from(@NonNull Exception e) {
Expand All @@ -120,6 +132,15 @@ public Intent toIntent() {
return new Intent().putExtra(ExtraConstants.IDP_RESPONSE, this);
}

@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public Builder mutate() {
if (!isSuccessful()) {
throw new IllegalStateException("Cannot mutate an unsuccessful response.");
}
return new Builder(this);
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public boolean isSuccessful() {
return mException == null;
Expand All @@ -139,6 +160,13 @@ public String getProviderType() {
return mUser.getProviderId();
}

/**
* Returns true if this user has just signed up, false otherwise.
*/
public boolean isNewUser() {
return mIsNewUser;
}

/**
* Get the email used to sign in.
*/
Expand Down Expand Up @@ -189,6 +217,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(mUser, flags);
dest.writeString(mToken);
dest.writeString(mSecret);
dest.writeInt(mIsNewUser ? 1 : 0);

ObjectOutputStream oos = null;
try {
Expand Down Expand Up @@ -224,6 +253,7 @@ public boolean equals(Object o) {
return (mUser == null ? response.mUser == null : mUser.equals(response.mUser))
&& (mToken == null ? response.mToken == null : mToken.equals(response.mToken))
&& (mSecret == null ? response.mSecret == null : mSecret.equals(response.mSecret))
&& (mIsNewUser == response.mIsNewUser)
&& (mException == null ? response.mException == null : mException.equals(response.mException));
}

Expand All @@ -232,6 +262,7 @@ public int hashCode() {
int result = mUser == null ? 0 : mUser.hashCode();
result = 31 * result + (mToken == null ? 0 : mToken.hashCode());
result = 31 * result + (mSecret == null ? 0 : mSecret.hashCode());
result = 31 * result + (mIsNewUser ? 1 : 0);
result = 31 * result + (mException == null ? 0 : mException.hashCode());
return result;
}
Expand All @@ -242,6 +273,7 @@ public String toString() {
"mUser=" + mUser +
", mToken='" + mToken + '\'' +
", mSecret='" + mSecret + '\'' +
", mIsNewUser='" + mIsNewUser + '\'' +
", mException=" + mException +
'}';
}
Expand All @@ -252,11 +284,24 @@ public static class Builder {

private String mToken;
private String mSecret;
private boolean mIsNewUser;

public Builder(@NonNull User user) {
mUser = user;
}

public Builder(@NonNull IdpResponse response) {
mUser = response.mUser;
mToken = response.mToken;
mSecret = response.mSecret;
mIsNewUser = response.mIsNewUser;
}

public Builder setNewUser(boolean newUser) {
mIsNewUser = newUser;
return this;
}

public Builder setToken(String token) {
mToken = token;
return this;
Expand All @@ -282,7 +327,7 @@ public IdpResponse build() {
"Secret cannot be null when using the Twitter provider.");
}

return new IdpResponse(mUser, mToken, mSecret);
return new IdpResponse(mUser, mToken, mSecret, mIsNewUser);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.util.GoogleApiUtils;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.viewmodel.AuthViewModelBase;
import com.firebase.ui.auth.viewmodel.RequestCodes;
import com.firebase.ui.auth.viewmodel.SignInViewModelBase;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.CredentialRequest;
import com.google.android.gms.auth.api.credentials.CredentialRequestResponse;
Expand All @@ -46,7 +46,7 @@
import java.util.ArrayList;
import java.util.List;

public class SignInKickstarter extends AuthViewModelBase<IdpResponse> {
public class SignInKickstarter extends SignInViewModelBase {
public SignInKickstarter(Application application) {
super(application);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ private void handleCredential(final Credential credential) {
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult result) {
setResult(Resource.forSuccess(response));
handleSuccess(response, result);
}
})
.addOnFailureListener(new OnFailureListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.firebase.ui.auth.viewmodel;

import android.app.Application;
import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;

import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.data.model.Resource;
import com.google.firebase.auth.AuthResult;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public abstract class SignInViewModelBase extends AuthViewModelBase<IdpResponse> {
protected SignInViewModelBase(Application application) {
super(application);
}

@Override
protected void setResult(Resource<IdpResponse> output) {
super.setResult(output);
}

protected void handleSuccess(@NonNull IdpResponse response, @NonNull AuthResult result) {
setResult(Resource.forSuccess(response.withResult(result)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
import com.firebase.ui.auth.ui.idp.WelcomeBackIdpPrompt;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.firebase.ui.auth.viewmodel.AuthViewModelBase;
import com.firebase.ui.auth.viewmodel.RequestCodes;
import com.firebase.ui.auth.viewmodel.SignInViewModelBase;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class EmailProviderResponseHandler extends AuthViewModelBase<IdpResponse> {
public class EmailProviderResponseHandler extends SignInViewModelBase {
private static final String TAG = "EmailProviderResponseHa";

public EmailProviderResponseHandler(Application application) {
Expand All @@ -47,7 +47,7 @@ public void startSignIn(@NonNull final IdpResponse response, @NonNull String pas
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult result) {
setResult(Resource.forSuccess(response));
handleSuccess(response, result);
}
})
.addOnFailureListener(new OnFailureListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.firebase.ui.auth.data.model.User;
import com.firebase.ui.auth.data.remote.ProfileMerger;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.firebase.ui.auth.viewmodel.AuthViewModelBase;
import com.firebase.ui.auth.viewmodel.SignInViewModelBase;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
Expand All @@ -25,7 +25,7 @@
* SmartLock.
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class WelcomeBackPasswordHandler extends AuthViewModelBase<IdpResponse> {
public class WelcomeBackPasswordHandler extends SignInViewModelBase {
private static final String TAG = "WBPasswordHandler";

private String mPendingPassword;
Expand Down Expand Up @@ -88,7 +88,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
return;
}

setResult(Resource.forSuccess(outputResponse));
handleSuccess(outputResponse, task.getResult());
}
})
.addOnFailureListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.data.model.Resource;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.viewmodel.AuthViewModelBase;
import com.firebase.ui.auth.viewmodel.SignInViewModelBase;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
Expand All @@ -19,7 +19,7 @@
import com.google.firebase.auth.FirebaseUser;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class LinkingSocialProviderResponseHandler extends AuthViewModelBase<IdpResponse> {
public class LinkingSocialProviderResponseHandler extends SignInViewModelBase {
private AuthCredential mRequestedSignInCredential;

public LinkingSocialProviderResponseHandler(Application application) {
Expand All @@ -45,31 +45,35 @@ public void startSignIn(@NonNull final IdpResponse response) {
FirebaseUser currentUser = getCurrentUser();
if (currentUser == null) {
getAuth().signInWithCredential(credential)
.continueWithTask(new Continuation<AuthResult, Task<Void>>() {
.continueWithTask(new Continuation<AuthResult, Task<AuthResult>>() {
@Override
public Task<Void> then(@NonNull Task<AuthResult> task) {
AuthResult result = task.getResult();
public Task<AuthResult> then(@NonNull Task<AuthResult> task) {
final AuthResult result = task.getResult();
if (mRequestedSignInCredential == null) {
return Tasks.forResult(null);
return Tasks.forResult(result);
} else {
return result.getUser()
.linkWithCredential(mRequestedSignInCredential)
.continueWith(new Continuation<AuthResult, Void>() {
.continueWith(new Continuation<AuthResult, AuthResult>() {
@Override
public Void then(@NonNull Task<AuthResult> task) {
// Since we've already signed in, it's too late to
// backtrack so we just ignore any errors.
return null;
public AuthResult then(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
return task.getResult();
} else {
// Since we've already signed in, it's too late
// to backtrack so we just ignore any errors.
return result;
}
}
});
}
}
})
.addOnCompleteListener(new OnCompleteListener<Void>() {
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
setResult(Resource.forSuccess(response));
handleSuccess(response, task.getResult());
} else {
setResult(Resource.<IdpResponse>forFailure(task.getException()));
}
Expand All @@ -82,7 +86,7 @@ public void onComplete(@NonNull Task<Void> task) {
public void onComplete(@NonNull Task<AuthResult> task) {
// I'm not sure why we ignore failures here, but this mirrors previous
// behavior.
setResult(Resource.forSuccess(response));
handleSuccess(response, task.getResult());
}
});
}
Expand Down
Loading