Skip to content

Refactor error handling #289

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 1 commit into from
Sep 8, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.firebase.ui.auth.ui;

import android.support.annotation.Nullable;

import com.firebase.ui.auth.provider.FacebookProvider;
import com.firebase.ui.auth.provider.GoogleProvider;
import com.firebase.ui.auth.provider.IDPResponse;
Expand All @@ -22,6 +24,8 @@
import com.google.firebase.auth.GoogleAuthProvider;

public class AuthCredentialHelper {

@Nullable
public static AuthCredential getAuthCredential(IDPResponse idpResponse) {
switch (idpResponse.getProviderType()) {
case GoogleAuthProvider.PROVIDER_ID:
Expand All @@ -32,4 +36,5 @@ public static AuthCredential getAuthCredential(IDPResponse idpResponse) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.auth.R;
Expand All @@ -41,18 +38,22 @@
import com.firebase.ui.auth.ui.email.PasswordToggler;
import com.firebase.ui.auth.ui.email.RecoverPasswordActivity;
import com.firebase.ui.auth.util.SmartlockUtil;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

/**
* Activity to link a pre-existing email/password account to a new IDP sign-in by confirming
* the password before initiating a link.
*/
public class WelcomeBackPasswordPrompt extends AppCompatBase implements View.OnClickListener {

private static final int RC_CREDENTIAL_SAVE = 3;
private static final String TAG = "WelcomeBackPassword";
final StyleSpan bold = new StyleSpan(Typeface.BOLD);
private static final StyleSpan BOLD = new StyleSpan(Typeface.BOLD);

private String mEmail;
private TextInputLayout mPasswordLayout;
private EditText mPasswordField;
Expand All @@ -62,29 +63,34 @@ public class WelcomeBackPasswordPrompt extends AppCompatBase implements View.OnC
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_back_password_prompt_layout);

mPasswordLayout = (TextInputLayout) findViewById(R.id.password_layout);
mPasswordField = (EditText) findViewById(R.id.password);

mIdpResponse = getIntent().getParcelableExtra(ExtraConstants.EXTRA_IDP_RESPONSE);
mEmail = mIdpResponse.getEmail();
TextView bodyTextView = (TextView) findViewById(R.id.welcome_back_password_body);

// Create welcome back text with email bolded
String bodyText = getResources().getString(R.string.welcome_back_password_prompt_body);
bodyText = String.format(bodyText, mEmail);
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(bodyText);
int emailStart = bodyText.indexOf(mEmail);
spannableStringBuilder.setSpan(bold, emailStart, emailStart + mEmail.length(), Spannable
spannableStringBuilder.setSpan(BOLD, emailStart, emailStart + mEmail.length(), Spannable
.SPAN_INCLUSIVE_INCLUSIVE);

TextView bodyTextView = ((TextView) findViewById(R.id.welcome_back_password_body));
bodyTextView.setText(spannableStringBuilder);
Button signIn = (Button) findViewById(R.id.button_done);
signIn.setOnClickListener(this);
mPasswordField = (EditText) findViewById(R.id.password);
ImageView toggleImage = (ImageView) findViewById(R.id.toggle_visibility);
toggleImage.setOnClickListener(new PasswordToggler(mPasswordField));
TextView troubleSigningIn = (TextView) findViewById(R.id.trouble_signing_in);
troubleSigningIn.setOnClickListener(this);

// Click listeners
findViewById(R.id.button_done).setOnClickListener(this);
findViewById(R.id.toggle_visibility).setOnClickListener(
new PasswordToggler(mPasswordField));
findViewById(R.id.trouble_signing_in).setOnClickListener(this);
}

@Override
public void onClick(View view) {
int id = view.getId();
final int id = view.getId();
if (id == R.id.button_done) {
mActivityHelper.showLoadingDialog(R.string.progress_dialog_signing_in);
next(mEmail, mPasswordField.getText().toString());
Expand All @@ -108,48 +114,48 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

private void next(String email, final String password) {
final FirebaseAuth firebaseAuth = mActivityHelper.getFirebaseAuth();

// Sign in with known email and the password provided
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnFailureListener(
new TaskFailureLogger(TAG, "Error signing in with email and password"))
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
AuthCredential authCredential =
AuthCredentialHelper.getAuthCredential(mIdpResponse);
task.getResult().getUser().linkWithCredential(authCredential);
firebaseAuth.signOut();

firebaseAuth.signInWithCredential(authCredential)
.addOnFailureListener(
new TaskFailureLogger(TAG, "Error signing in with credential"))
.addOnSuccessListener(
new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
FirebaseUser firebaseUser = authResult.getUser();
String photoUrl = null;
Uri photoUri = firebaseUser.getPhotoUrl();
if (photoUri != null) {
photoUrl = photoUri.toString();
}
mActivityHelper.dismissDialog();

SmartlockUtil.saveCredentialOrFinish(
WelcomeBackPasswordPrompt.this,
RC_CREDENTIAL_SAVE,
mActivityHelper.getFlowParams(),
firebaseUser,
password,
null /* provider */);
}
});
} else {
String error = task.getException().getLocalizedMessage();
mPasswordLayout.setError(error);
}
}
});
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// Get the social AuthCredential from the IDPResponse object, link
// it to the email/password account.
AuthCredential authCredential =
AuthCredentialHelper.getAuthCredential(mIdpResponse);
authResult.getUser().linkWithCredential(authCredential);
firebaseAuth.signOut();

// Sign in with the credential
firebaseAuth.signInWithCredential(authCredential)
.addOnFailureListener(
new TaskFailureLogger(TAG, "Error signing in with credential"))
.addOnSuccessListener(
new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
mActivityHelper.dismissDialog();
SmartlockUtil.saveCredentialOrFinish(
WelcomeBackPasswordPrompt.this,
RC_CREDENTIAL_SAVE,
mActivityHelper.getFlowParams(),
authResult.getUser(),
password,
null /* provider */);
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String error = e.getLocalizedMessage();
mPasswordLayout.setError(error);
}
});
}

public static Intent createIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.firebase.ui.auth.R;
import com.firebase.ui.auth.ui.ActivityHelper;
import com.firebase.ui.auth.ui.AppCompatBase;
import com.firebase.ui.auth.ui.ExtraConstants;
import com.firebase.ui.auth.ui.FlowParameters;

public class ConfirmRecoverPasswordActivity extends android.support.v7.app.AppCompatActivity
/**
* Dialog activity to confirm successful email sending in {@link RecoverPasswordActivity}.
*/
public class ConfirmRecoverPasswordActivity extends AppCompatActivity
implements View.OnClickListener {

private ActivityHelper mActivityHelper;

@Override
Expand All @@ -38,17 +42,12 @@ protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.confirm_recovery_layout);
String email = getIntent().getStringExtra(ExtraConstants.EXTRA_EMAIL);
boolean isSuccess = getIntent().getBooleanExtra(ExtraConstants.EXTRA_SUCCESS, true);

if (isSuccess) {
String text = String.format(
getResources().getString(R.string.confirm_recovery_body),
email
);
((TextView) findViewById(R.id.body_text)).setText(text);
} else {
((TextView) findViewById(R.id.body_text)).setText(R.string.recovery_fail_body);
}
String text = String.format(
getResources().getString(R.string.confirm_recovery_body),
email);
((TextView) findViewById(R.id.body_text)).setText(text);

findViewById(R.id.button_done).setOnClickListener(this);
}

Expand All @@ -62,11 +61,9 @@ public void onClick(View view) {
public static Intent createIntent(
Context context,
FlowParameters flowParams,
boolean success,
String email) {
return ActivityHelper.createBaseIntent(context, ConfirmRecoverPasswordActivity.class,
flowParams)
.putExtra(ExtraConstants.EXTRA_SUCCESS, success)
.putExtra(ExtraConstants.EXTRA_EMAIL, email);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.firebase.ui.auth.R;
Expand All @@ -30,13 +29,18 @@
import com.firebase.ui.auth.ui.FlowParameters;
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.ui.email.field_validators.EmailFieldValidator;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidUserException;

/**
* Activity to initiate the "forgot password" flow by asking for the user's email.
*/
public class RecoverPasswordActivity extends AppCompatBase implements View.OnClickListener {
private static final String TAG = "RecoverPasswordActivity";
private static final int RC_CONFIRM = 3;

private EditText mEmailEditText;
private EmailFieldValidator mEmailFieldValidator;

Expand All @@ -46,34 +50,44 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.forgot_password_layout);
String email = getIntent().getStringExtra(ExtraConstants.EXTRA_EMAIL);

mEmailFieldValidator = new EmailFieldValidator((TextInputLayout) findViewById(R.id
.email_layout));
mEmailFieldValidator = new EmailFieldValidator(
(TextInputLayout) findViewById(R.id.email_layout));

mEmailEditText = (EditText) findViewById(R.id.email);
Button nextButton = (Button) findViewById(R.id.button_done);

if (email != null) {
mEmailEditText.setText(email);
}
nextButton.setOnClickListener(this);

findViewById(R.id.button_done).setOnClickListener(this);
}

private void next(final String email) {
FirebaseAuth firebaseAuth = mActivityHelper.getFirebaseAuth();
firebaseAuth.sendPasswordResetEmail(email)
.addOnFailureListener(
new TaskFailureLogger(TAG, "Error sending password reset email"))
.addOnCompleteListener(new OnCompleteListener<Void>() {
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
public void onSuccess(Void aVoid) {
mActivityHelper.dismissDialog();

Intent confirmIntent = ConfirmRecoverPasswordActivity.createIntent(
RecoverPasswordActivity.this,
mActivityHelper.getFlowParams(),
task.isSuccessful(),
email);
startActivityForResult(confirmIntent, RC_CONFIRM);
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
mActivityHelper.dismissDialog();

if (e instanceof FirebaseAuthInvalidUserException) {
// No FirebaseUser exists with this email address, show error.
mEmailEditText.setError(getString(R.string.error_email_does_not_exist));
}
}
});
}

Expand Down
Loading