Skip to content

Commit 12eb538

Browse files
Samed Ozdemirsamtstern
authored andcommitted
Add option in AuthUI.SignInIntentBuilder to disable account creation (#536)
1 parent 6874189 commit 12eb538

File tree

9 files changed

+64
-17
lines changed

9 files changed

+64
-17
lines changed

app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public class AuthUiActivity extends AppCompatActivity {
102102
@BindView(R.id.smartlock_enabled)
103103
CheckBox mEnableSmartLock;
104104

105+
@BindView(R.id.allow_new_email_accounts)
106+
CheckBox mAllowNewEmailAccounts;
107+
105108
@BindView(R.id.facebook_scopes_label)
106109
TextView mFacebookScopesLabel;
107110

@@ -183,6 +186,7 @@ public void signIn(View view) {
183186
.setProviders(getSelectedProviders())
184187
.setTosUrl(getSelectedTosUrl())
185188
.setIsSmartLockEnabled(mEnableSmartLock.isChecked())
189+
.setAllowNewEmailAccounts(mAllowNewEmailAccounts.isChecked())
186190
.build(),
187191
RC_SIGN_IN);
188192
}

app/src/main/java/com/firebase/uidemo/database/ChatActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ public void populateViewHolder(ChatHolder chatView, Chat chat, int position) {
162162
@Override
163163
protected void onDataChanged() {
164164
// if there are no chat messages, show a view that invites the user to add a message
165-
mEmptyListView.setVisibility(mRecyclerViewAdapter.getItemCount() == 0 ? View.VISIBLE : View.INVISIBLE);
165+
mEmptyListView.setVisibility(mRecyclerViewAdapter.getItemCount() == 0 ?
166+
View.VISIBLE : View.INVISIBLE);
166167
}
167168
};
168169

app/src/main/res/layout/auth_ui_layout.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@
235235
android:checked="true"
236236
android:text="@string/enable_smartlock"/>
237237

238+
<CheckBox
239+
android:id="@+id/allow_new_email_accounts"
240+
android:layout_width="wrap_content"
241+
android:layout_height="wrap_content"
242+
android:checked="true"
243+
android:text="@string/allow_new_email_acccount"/>
244+
238245
</LinearLayout>
239246

240247
</ScrollView>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
<string name="drive_file">Drive File</string>
6767

6868
<!-- strings for Auth UI demo activities -->
69-
<string name="start_chatting">No messages. Start chatting at the bottom!</string>
70-
<string name="sign_in_failed">Sign In Failed</string>
7169
<string name="signed_in">Signed In</string>
70+
<string name="sign_in_failed">Sign In Failed</string>
71+
<string name="allow_new_email_acccount">Allow account creation if email does not exist.</string>
72+
<string name="start_chatting">No messages. Start chatting at the bottom!</string>
7273
</resources>

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ public final class SignInIntentBuilder {
517517
private LinkedHashSet<IdpConfig> mProviders = new LinkedHashSet<>();
518518
private String mTosUrl;
519519
private boolean mIsSmartLockEnabled = true;
520+
private boolean mAllowNewEmailAccounts = true;
520521

521522
private SignInIntentBuilder() {
522523
mProviders.add(new IdpConfig.Builder(EMAIL_PROVIDER).build());
@@ -632,7 +633,15 @@ public FlowParameters getFlowParams() {
632633
mTheme,
633634
mLogo,
634635
mTosUrl,
635-
mIsSmartLockEnabled);
636+
mIsSmartLockEnabled,
637+
mAllowNewEmailAccounts);
636638
}
639+
640+
public SignInIntentBuilder setAllowNewEmailAccounts(boolean enabled) {
641+
mAllowNewEmailAccounts = enabled;
642+
return this;
643+
}
644+
645+
637646
}
638647
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,23 @@ public class FlowParameters implements Parcelable {
4949

5050
public final boolean smartLockEnabled;
5151

52+
public final boolean allowNewEmailAccounts;
53+
5254
public FlowParameters(
5355
@NonNull String appName,
5456
@NonNull List<IdpConfig> providerInfo,
5557
@StyleRes int themeId,
5658
@DrawableRes int logoId,
5759
@Nullable String termsOfServiceUrl,
58-
boolean smartLockEnabled) {
60+
boolean smartLockEnabled,
61+
boolean allowNewEmailAccounts) {
5962
this.appName = Preconditions.checkNotNull(appName, "appName cannot be null");
6063
this.providerInfo = Preconditions.checkNotNull(providerInfo, "providerInfo cannot be null");
6164
this.themeId = themeId;
6265
this.logoId = logoId;
6366
this.termsOfServiceUrl = termsOfServiceUrl;
6467
this.smartLockEnabled = smartLockEnabled;
68+
this.allowNewEmailAccounts = allowNewEmailAccounts;
6569
}
6670

6771
@Override
@@ -72,6 +76,7 @@ public void writeToParcel(Parcel dest, int flags) {
7276
dest.writeInt(logoId);
7377
dest.writeString(termsOfServiceUrl);
7478
dest.writeInt(smartLockEnabled ? 1 : 0);
79+
dest.writeInt(allowNewEmailAccounts ? 1 : 0);
7580
}
7681

7782
@Override
@@ -88,15 +93,19 @@ public FlowParameters createFromParcel(Parcel in) {
8893
int logoId = in.readInt();
8994
String termsOfServiceUrl = in.readString();
9095
int smartLockEnabledInt = in.readInt();
96+
int allowNewEmailAccountsInt = in.readInt();
97+
9198
boolean smartLockEnabled = smartLockEnabledInt != 0;
99+
boolean allowNewEmailAccounts = allowNewEmailAccountsInt != 0;
92100

93101
return new FlowParameters(
94102
appName,
95103
providerInfo,
96104
themeId,
97105
logoId,
98106
termsOfServiceUrl,
99-
smartLockEnabled);
107+
smartLockEnabled,
108+
allowNewEmailAccounts);
100109
}
101110

102111
@Override

auth/src/main/java/com/firebase/ui/auth/ui/email/CheckEmailFragment.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ interface CheckEmailListener {
7575
private static final int RC_SIGN_IN = 16;
7676

7777
private EditText mEmailEditText;
78+
private TextInputLayout mEmailLayout;
7879

7980
private EmailFieldValidator mEmailFieldValidator;
8081
private CheckEmailListener mListener;
@@ -100,9 +101,11 @@ public View onCreateView(LayoutInflater inflater,
100101
View v = inflater.inflate(R.layout.check_email_layout, container, false);
101102

102103
// Email field and validator
104+
mEmailLayout = (TextInputLayout) v.findViewById(R.id.email_layout);
103105
mEmailEditText = (EditText) v.findViewById(R.id.email);
104-
mEmailFieldValidator = new EmailFieldValidator(
105-
(TextInputLayout) v.findViewById(R.id.email_layout));
106+
mEmailFieldValidator = new EmailFieldValidator(mEmailLayout);
107+
mEmailLayout.setOnClickListener(this);
108+
mEmailEditText.setOnClickListener(this);
106109

107110
// "Next" button
108111
v.findViewById(R.id.button_next).setOnClickListener(this);
@@ -257,6 +260,8 @@ public void onClick(View view) {
257260

258261
if (id == R.id.button_next) {
259262
validateAndProceed();
263+
} else if (id == R.id.email_layout || id == R.id.email) {
264+
mEmailLayout.setError(null);
260265
}
261266
}
262267
}

auth/src/main/java/com/firebase/ui/auth/ui/email/RegisterEmailActivity.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.content.Intent;
1919
import android.os.Bundle;
2020
import android.support.annotation.RestrictTo;
21+
import android.support.design.widget.TextInputLayout;
2122
import android.support.v4.app.FragmentTransaction;
2223
import android.view.View;
2324

@@ -118,17 +119,26 @@ public void onExistingIdpUser(User user) {
118119

119120
@Override
120121
public void onNewUser(User user) {
121-
// New user, direct them to create an account with email/password.
122-
RegisterEmailFragment fragment = RegisterEmailFragment.getInstance(
123-
mActivityHelper.getFlowParams(),
124-
user);
125-
FragmentTransaction ft = getSupportFragmentManager().beginTransaction()
126-
.replace(R.id.fragment_register_email, fragment, RegisterEmailFragment.TAG);
122+
// New user, direct them to create an account with email/password
123+
// if account creation is enabled in SignInIntentBuilder
124+
125+
boolean createAccount = mActivityHelper.getFlowParams().allowNewEmailAccounts;
126+
127+
TextInputLayout mEmailLayout = (TextInputLayout) findViewById(R.id.email_layout);
127128

128-
View v = findViewById(R.id.email_layout);
129-
if (v != null) ft.addSharedElement(v, "email_field");
129+
if (createAccount) {
130+
RegisterEmailFragment fragment = RegisterEmailFragment.getInstance(
131+
mActivityHelper.getFlowParams(),
132+
user);
133+
FragmentTransaction ft = getSupportFragmentManager().beginTransaction()
134+
.replace(R.id.fragment_register_email, fragment, RegisterEmailFragment.TAG);
130135

131-
ft.disallowAddToBackStack().commit();
136+
if (mEmailLayout != null) ft.addSharedElement(mEmailLayout, "email_field");
137+
138+
ft.disallowAddToBackStack().commit();
139+
} else {
140+
mEmailLayout.setError(getString(R.string.error_email_does_not_exist));
141+
}
132142
}
133143

134144
private void setSlideAnimation() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static FlowParameters getFlowParameters(List<String> providerIds) {
6565
AuthUI.getDefaultTheme(),
6666
AuthUI.NO_LOGO,
6767
null,
68+
true,
6869
true);
6970
}
7071

0 commit comments

Comments
 (0)