Skip to content

Check for null password or lack of network #308

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 23, 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 @@ -29,6 +29,7 @@
import android.widget.RadioButton;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ui.ResultCodes;
import com.firebase.uidemo.R;
import com.google.firebase.auth.FirebaseAuth;

Expand Down Expand Up @@ -172,6 +173,11 @@ private void handleSignInResponse(int resultCode, Intent data) {
return;
}

if (resultCode == ResultCodes.RESULT_NO_NETWORK) {
showSnackbar(R.string.no_internet_connection);
return;
}

showSnackbar(R.string.unknown_sign_in_response);
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="ok_label">OK</string>
<string name="unknown_sign_in_response">Unknown response from AuthUI sign-in</string>
<string name="sign_in_cancelled">Sign in cancelled</string>
<string name="no_internet_connection">No internet connection</string>
<string name="signed_in_header">You are signed in!</string>
<string name="sign_out">Sign out</string>
<string name="delete_account_label">Delete account</string>
Expand Down
2 changes: 2 additions & 0 deletions auth/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.firebase.ui.auth">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application>

<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
Expand Down Expand Up @@ -46,6 +47,8 @@

import java.util.List;

import static com.firebase.ui.auth.ui.ResultCodes.RESULT_NO_NETWORK;

/**
* Attempts to acquire a credential from Smart Lock for Passwords to sign in
* an existing account. If this succeeds, an attempt is made to sign the user in
Expand All @@ -57,6 +60,7 @@
*/
public class ChooseAccountActivity extends ActivityBase {
private static final String TAG = "ChooseAccountActivity";

private static final int RC_CREDENTIALS_READ = 2;
private static final int RC_IDP_SIGNIN = 3;
private static final int RC_AUTH_METHOD_PICKER = 4;
Expand All @@ -70,6 +74,13 @@ public class ChooseAccountActivity extends ActivityBase {
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);

if (!hasNetworkConnection()) {
Log.d(TAG, "No network connection");

finish(RESULT_NO_NETWORK, new Intent());
return;
}

// Make Google Play Services available at the correct version, if possible
mPlayServicesHelper = PlayServicesHelper.getInstance(this);
boolean madeAvailable = mPlayServicesHelper
Expand Down Expand Up @@ -112,6 +123,18 @@ protected void onStop() {
}
}

/**
* Check if there is an active or soon-to-be-active network connection.
*/
private boolean hasNetworkConnection() {
ConnectivityManager manager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

return manager != null
&& manager.getActiveNetworkInfo() != null
&& manager.getActiveNetworkInfo().isConnectedOrConnecting();
}

/**
* Called when the Credentials API connects.
*/
Expand Down
14 changes: 14 additions & 0 deletions auth/src/main/java/com/firebase/ui/auth/ui/ResultCodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.firebase.ui.auth.ui;

import com.firebase.ui.auth.AuthUI;

/**
* Result codes returned when using {@link AuthUI.SignInIntentBuilder#build()} with
* {@code startActivityForResult}.
*/
public class ResultCodes {

/** Sign in failed due to lack of network connection **/
public static final int RESULT_NO_NETWORK = 10;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.support.design.widget.TextInputLayout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.EditText;
Expand Down Expand Up @@ -115,6 +116,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
private void next(String email, final String password) {
final FirebaseAuth firebaseAuth = mActivityHelper.getFirebaseAuth();

// Check for null or empty password
if (TextUtils.isEmpty(password)) {
mPasswordField.setError(getString(R.string.required_field));
return;
} else {
mPasswordField.setError(null);
}

// Sign in with known email and the password provided
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnFailureListener(
Expand Down