Skip to content

Commit f3f15a3

Browse files
authored
Make name optional (#1108)
1 parent ff16d0d commit f3f15a3

File tree

17 files changed

+164
-71
lines changed

17 files changed

+164
-71
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
@@ -119,6 +119,9 @@ public class AuthUiActivity extends AppCompatActivity {
119119
@BindView(R.id.allow_new_email_accounts)
120120
CheckBox mAllowNewEmailAccounts;
121121

122+
@BindView(R.id.require_name)
123+
CheckBox mRequireName;
124+
122125
@BindView(R.id.facebook_scopes_label)
123126
TextView mFacebookScopesLabel;
124127

@@ -341,6 +344,7 @@ private List<IdpConfig> getSelectedProviders() {
341344

342345
if (mUseEmailProvider.isChecked()) {
343346
selectedProviders.add(new IdpConfig.EmailBuilder()
347+
.setRequireName(mRequireName.isChecked())
344348
.setAllowNewAccounts(mAllowNewEmailAccounts.isChecked())
345349
.build());
346350
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@
284284
android:checked="true"
285285
android:text="@string/allow_new_email_acccount" />
286286

287+
<CheckBox
288+
android:id="@+id/require_name"
289+
android:layout_width="wrap_content"
290+
android:layout_height="wrap_content"
291+
android:checked="true"
292+
android:text="@string/require_name" />
293+
287294
</LinearLayout>
288295

289296
</ScrollView>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<string name="enable_credential_selector">Enable Smart Lock\'s credential selector</string>
6060
<string name="enable_hint_selector">Enable Smart Lock\'s hint selector</string>
6161
<string name="allow_new_email_acccount">Allow account creation if email does not exist.</string>
62+
<string name="require_name">Require first/last name with email accounts.</string>
6263

6364
<string name="unknown_response">Unexpected onActivityResult response code</string>
6465
<string name="unknown_sign_in_response">Unknown response from AuthUI sign-in</string>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,18 @@ public EmailBuilder setAllowNewAccounts(boolean allow) {
604604
getParams().putBoolean(ExtraConstants.EXTRA_ALLOW_NEW_EMAILS, allow);
605605
return this;
606606
}
607+
608+
/**
609+
* Configures the requirement for the user to enter first and last name
610+
* in the email sign up flow.
611+
* <p>
612+
* Name is required by default.
613+
*/
614+
@NonNull
615+
public EmailBuilder setRequireName(boolean requireName) {
616+
getParams().putBoolean(ExtraConstants.EXTRA_REQUIRE_NAME, requireName);
617+
return this;
618+
}
607619
}
608620

609621
/**

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.widget.TextView;
1515
import android.widget.Toast;
1616

17+
import com.firebase.ui.auth.AuthUI;
1718
import com.firebase.ui.auth.IdpResponse;
1819
import com.firebase.ui.auth.R;
1920
import com.firebase.ui.auth.data.model.FlowParameters;
@@ -28,7 +29,9 @@
2829
import com.firebase.ui.auth.util.signincontainer.SaveSmartLock;
2930
import com.firebase.ui.auth.util.ui.ImeHelper;
3031
import com.firebase.ui.auth.util.ui.PreambleHandler;
32+
import com.firebase.ui.auth.util.ui.fieldvalidators.BaseValidator;
3133
import com.firebase.ui.auth.util.ui.fieldvalidators.EmailFieldValidator;
34+
import com.firebase.ui.auth.util.ui.fieldvalidators.NoOpValidator;
3235
import com.firebase.ui.auth.util.ui.fieldvalidators.PasswordFieldValidator;
3336
import com.firebase.ui.auth.util.ui.fieldvalidators.RequiredFieldValidator;
3437
import com.google.android.gms.tasks.OnCompleteListener;
@@ -57,12 +60,13 @@ public class RegisterEmailFragment extends FragmentBase implements
5760
private EditText mNameEditText;
5861
private EditText mPasswordEditText;
5962
private TextView mAgreementText;
63+
private TextInputLayout mNameInput;
6064
private TextInputLayout mEmailInput;
6165
private TextInputLayout mPasswordInput;
6266

6367
private EmailFieldValidator mEmailFieldValidator;
6468
private PasswordFieldValidator mPasswordFieldValidator;
65-
private RequiredFieldValidator mNameValidator;
69+
private BaseValidator mNameValidator;
6670
private SaveSmartLock mSaveSmartLock;
6771

6872
private User mUser;
@@ -96,18 +100,26 @@ public View onCreateView(LayoutInflater inflater,
96100

97101
View v = inflater.inflate(R.layout.fui_register_email_layout, container, false);
98102

103+
// Get configuration
104+
AuthUI.IdpConfig emailConfig = ProviderUtils.getConfigFromIdps(
105+
getFlowParams().providerInfo, EmailAuthProvider.PROVIDER_ID);
106+
boolean requireName = emailConfig.getParams()
107+
.getBoolean(ExtraConstants.EXTRA_REQUIRE_NAME, true);
108+
99109
mEmailEditText = v.findViewById(R.id.email);
100110
mNameEditText = v.findViewById(R.id.name);
101111
mPasswordEditText = v.findViewById(R.id.password);
102112
mAgreementText = v.findViewById(R.id.create_account_text);
103113
mEmailInput = v.findViewById(R.id.email_layout);
114+
mNameInput = v.findViewById(R.id.name_layout);
104115
mPasswordInput = v.findViewById(R.id.password_layout);
105116

106117
mPasswordFieldValidator = new PasswordFieldValidator(
107118
mPasswordInput,
108119
getResources().getInteger(R.integer.fui_min_password_length));
109-
mNameValidator = new RequiredFieldValidator(
110-
(TextInputLayout) v.findViewById(R.id.name_layout));
120+
mNameValidator = requireName
121+
? new RequiredFieldValidator(mNameInput)
122+
: new NoOpValidator(mNameInput);
111123
mEmailFieldValidator = new EmailFieldValidator(mEmailInput);
112124

113125
ImeHelper.setImeOnDoneListener(mPasswordEditText, this);
@@ -117,6 +129,13 @@ public View onCreateView(LayoutInflater inflater,
117129
mPasswordEditText.setOnFocusChangeListener(this);
118130
v.findViewById(R.id.button_create).setOnClickListener(this);
119131

132+
// Only show the name field if required
133+
if (requireName) {
134+
mNameInput.setVisibility(View.VISIBLE);
135+
} else {
136+
mNameInput.setVisibility(View.GONE);
137+
}
138+
120139
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && getFlowParams().enableCredentials) {
121140
mEmailEditText.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
122141
}
@@ -138,7 +157,7 @@ public View onCreateView(LayoutInflater inflater,
138157
}
139158

140159
// See http://stackoverflow.com/questions/11082341/android-requestfocus-ineffective#comment51774752_11082523
141-
if (!TextUtils.isEmpty(mNameEditText.getText())) {
160+
if (!requireName || !TextUtils.isEmpty(mNameEditText.getText())) {
142161
safeRequestFocus(mPasswordEditText);
143162
} else if (!TextUtils.isEmpty(mEmailEditText.getText())) {
144163
safeRequestFocus(mNameEditText);
@@ -169,7 +188,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
169188
}
170189

171190
mActivity = (HelperActivityBase) getActivity();
172-
mSaveSmartLock = getAuthHelper().getSaveSmartLockInstance(mActivity);
173191
PreambleHandler.setup(getContext(),
174192
getFlowParams(),
175193
R.string.fui_button_text_save,
@@ -226,6 +244,14 @@ private void validateAndRegisterUser() {
226244
}
227245
}
228246

247+
private SaveSmartLock getSaveSmartLock() {
248+
if (mSaveSmartLock == null) {
249+
mSaveSmartLock = SaveSmartLock.getInstance(mActivity);
250+
}
251+
252+
return mSaveSmartLock;
253+
}
254+
229255
private void registerUser(final String email, final String name, final String password) {
230256
final IdpResponse response = new IdpResponse.Builder(
231257
new User.Builder(EmailAuthProvider.PROVIDER_ID, email)
@@ -241,8 +267,9 @@ private void registerUser(final String email, final String name, final String pa
241267
.addOnSuccessListener(getActivity(), new OnSuccessListener<AuthResult>() {
242268
@Override
243269
public void onSuccess(AuthResult authResult) {
270+
SaveSmartLock saveSmartLock = getSaveSmartLock();
244271
mActivity.saveCredentialsOrFinish(
245-
mSaveSmartLock,
272+
saveSmartLock,
246273
authResult.getUser(),
247274
password,
248275
response);

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public static Intent createIntent(Context context, FlowParameters flowParams) {
8282
protected void onCreate(Bundle savedInstanceState) {
8383
super.onCreate(savedInstanceState);
8484
setContentView(R.layout.fui_auth_method_picker_layout);
85-
mSaveSmartLock = getAuthHelper().getSaveSmartLockInstance(this);
8685

8786
populateIdpList(getFlowParams().providerInfo);
8887

@@ -102,6 +101,14 @@ protected void onCreate(Bundle savedInstanceState) {
102101
}
103102
}
104103

104+
private SaveSmartLock getSaveSmartLock() {
105+
if (mSaveSmartLock == null) {
106+
mSaveSmartLock = SaveSmartLock.getInstance(this);
107+
}
108+
109+
return mSaveSmartLock;
110+
}
111+
105112
private void populateIdpList(List<IdpConfig> providers) {
106113
mProviders = new ArrayList<>();
107114
for (IdpConfig idpConfig : providers) {
@@ -168,7 +175,7 @@ public void onSuccess(IdpResponse response) {
168175
.signInWithCredential(credential)
169176
.addOnCompleteListener(new CredentialSignInHandler(
170177
this,
171-
mSaveSmartLock,
178+
getSaveSmartLock(),
172179
RC_ACCOUNT_LINK,
173180
response))
174181
.addOnFailureListener(

auth/src/main/java/com/firebase/ui/auth/util/AuthHelper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import android.support.annotation.RestrictTo;
55

66
import com.firebase.ui.auth.data.model.FlowParameters;
7-
import com.firebase.ui.auth.ui.HelperActivityBase;
8-
import com.firebase.ui.auth.util.signincontainer.SaveSmartLock;
97
import com.google.android.gms.auth.api.Auth;
108
import com.google.android.gms.auth.api.credentials.CredentialsApi;
119
import com.google.firebase.FirebaseApp;
@@ -38,10 +36,6 @@ public FirebaseUser getCurrentUser() {
3836
return getFirebaseAuth().getCurrentUser();
3937
}
4038

41-
public SaveSmartLock getSaveSmartLockInstance(HelperActivityBase activity) {
42-
return SaveSmartLock.getInstance(activity);
43-
}
44-
4539
public PhoneAuthProvider getPhoneAuthProvider() {
4640
return PhoneAuthProvider.getInstance(getFirebaseAuth());
4741
}

auth/src/main/java/com/firebase/ui/auth/util/CredentialsUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public static Credential buildCredential(@Nullable String email,
5252
}
5353

5454
Credential.Builder builder = new Credential.Builder(email);
55-
builder.setPassword(password);
55+
if (!TextUtils.isEmpty(password)) {
56+
builder.setPassword(password);
57+
}
5658
if (password == null && idpResponse != null) {
5759
String translatedProvider =
5860
ProviderUtils.providerIdToAccountType(idpResponse.getProviderType());

auth/src/main/java/com/firebase/ui/auth/util/ExtraConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public final class ExtraConstants {
2727

2828
public static final String EXTRA_EMAIL = "extra_email";
2929
public static final String EXTRA_ALLOW_NEW_EMAILS = "extra_allow_new_emails";
30+
public static final String EXTRA_REQUIRE_NAME = "extra_require_name";
3031
public static final String EXTRA_GOOGLE_SIGN_IN_OPTIONS = "extra_google_sign_in_options";
3132
public static final String EXTRA_FACEBOOK_PERMISSIONS = "extra_facebook_permissions";
3233

auth/src/main/java/com/firebase/ui/auth/util/signincontainer/IdpSignInContainer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ public void onAttach(Context context) {
9696
@Override
9797
public void onCreate(@Nullable Bundle savedInstanceState) {
9898
super.onCreate(savedInstanceState);
99-
mSaveSmartLock = getAuthHelper().getSaveSmartLockInstance(mActivity);
100-
10199
User user = User.getUser(getArguments());
102100
String provider = user.getProviderId();
103101

@@ -134,7 +132,7 @@ public void onSuccess(IdpResponse response) {
134132
.signInWithCredential(credential)
135133
.addOnCompleteListener(new CredentialSignInHandler(
136134
mActivity,
137-
mSaveSmartLock,
135+
getSaveSmartLock(),
138136
RC_WELCOME_BACK_IDP,
139137
response))
140138
.addOnFailureListener(
@@ -156,4 +154,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
156154
mIdpProvider.onActivityResult(requestCode, resultCode, data);
157155
}
158156
}
157+
158+
private SaveSmartLock getSaveSmartLock() {
159+
if (mSaveSmartLock == null) {
160+
mSaveSmartLock = SaveSmartLock.getInstance(mActivity);
161+
}
162+
163+
return mSaveSmartLock;
164+
}
159165
}

0 commit comments

Comments
 (0)