Skip to content

Commit 0fb425e

Browse files
lsiracsamtstern
authored andcommitted
Email Link Sign In Documentation + Backstack fixes (#1503)
* Added EmailLinkFragment to the backstack when starting TroubleSigningInFragment and added documentation for email link sign in * Fixed rotation bug, and backstack bug * Added a clarifying comment when popping off the back stack
1 parent a9469ba commit 0fb425e

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

auth/README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,61 @@ startActivityForResult(
337337
RC_SIGN_IN);
338338
```
339339

340+
##### Configuring Email Link Sign In
341+
342+
To use email link sign in, you will first need to enable it in the Firebase Console. Additionally, you will
343+
also have to enable Firebase Dynamic Links.
344+
345+
You can enable email link sign in by calling the `enableEmailLinkSignIn` on an `EmailBuilder` instance. You will also need
346+
to provide a valid `ActionCodeSettings` object with `setHandleCodeInApp` set to true. Additionally, you need to whitelist the
347+
URL you pass to `setUrl`; you can do so in the Firebase Console (Authentication -> Sign in Methods -> Authorized domains).
348+
349+
```java
350+
351+
ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder()
352+
.setAndroidPackageName(/*yourPackageName*/, /*installIfNotAvailable*/true, /*minimumVersion*/null)
353+
.setHandleCodeInApp(true)
354+
.setUrl("https://google.com") // This URL needs to be whitelisted
355+
.build();
356+
357+
startActivityForResult(
358+
AuthUI.getInstance()
359+
.createSignInIntentBuilder()
360+
.setAvailableProviders(Arrays.asList(
361+
new AuthUI.IdpConfig.EmailBuilder().enableEmailLinkSignIn()
362+
.setActionCodeSettings(actionCodeSettings).build())
363+
.build(),
364+
RC_SIGN_IN);
365+
366+
```
367+
368+
If you want to catch the link in a specific activity, please follow the steps outlined [here](https://firebase.google.com/docs/auth/android/email-link-auth).
369+
Otherwise, the link will redirect to your launcher activity.
370+
371+
Once you catch the deep link, you will need to call verify that we can handle it for you. If we can, you need to then
372+
pass it to us via `setEmailLink`.
373+
374+
```java
375+
if (AuthUI.canHandleIntent(getIntent())) {
376+
if (getIntent().getExtras() != null) {
377+
return;
378+
}
379+
String link = getIntent().getExtras().getString(ExtraConstants.EMAIL_LINK_SIGN_IN);
380+
if (link != null) {
381+
startActivityForResult(
382+
AuthUI.getInstance()
383+
.createSignInIntentBuilder()
384+
.setEmailLink(link)
385+
.setAvailableProviders(getAvailableProviders())
386+
.build(),
387+
RC_SIGN_IN);
388+
}
389+
}
390+
```
391+
392+
Note that email link sign in is currently only supported for the same device. Finishing the flow on a different device will result
393+
in the user being shown an error.
394+
340395
##### Adding a ToS and privacy policy
341396

342397
A terms of service URL and privacy policy URL are generally required:
@@ -469,7 +524,6 @@ This change is purely UI based. We do not restrict users from signing in with th
469524
They will simply be unable to choose their country in the selector, but there may be another country
470525
sharing the same country code (e.g. US and CA are +1).
471526
472-
473527
#####
474528
475529
### Handling the sign-in response

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,19 @@ public void onNewUser(User user) {
173173
public void onTroubleSigningIn(String email) {
174174
TroubleSigningInFragment troubleSigningInFragment = TroubleSigningInFragment.newInstance
175175
(email);
176-
switchFragment(troubleSigningInFragment, TroubleSigningInFragment.TAG, true);
176+
switchFragment(troubleSigningInFragment, TroubleSigningInFragment.TAG, true, true);
177177
}
178178

179179
@Override
180180
public void onClickResendEmail(String email) {
181+
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
182+
// We're assuming that to get to the TroubleSigningInFragment, we went through
183+
// the EmailLinkFragment, which was added to the fragment back stack.
184+
// From here, we're going to register the EmailLinkFragment again, meaning we'd have to
185+
// pop off the back stack twice to return to the nascar screen. To avoid this,
186+
// we pre-emptively pop off the last EmailLinkFragment here.
187+
getSupportFragmentManager().popBackStack();
188+
}
181189
AuthUI.IdpConfig emailConfig = ProviderUtils.getConfigFromIdpsOrThrow(
182190
getFlowParams().providers, EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD);
183191
showRegisterEmailLinkFragment(
@@ -214,16 +222,24 @@ private void showRegisterEmailLinkFragment(AuthUI.IdpConfig emailConfig,
214222
}
215223

216224

217-
private void switchFragment(Fragment fragment, String tag, boolean withTransition) {
225+
private void switchFragment(Fragment fragment,
226+
String tag,
227+
boolean withTransition,
228+
boolean addToBackStack) {
218229
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
219230
if (withTransition) {
220231
ft.setCustomAnimations(R.anim.fui_slide_in_right, R.anim.fui_slide_out_left);
221232
}
222-
ft.replace(R.id.fragment_register_email, fragment, tag).disallowAddToBackStack().commit();
233+
ft.replace(R.id.fragment_register_email, fragment, tag);
234+
if (addToBackStack) {
235+
ft.addToBackStack(null).commit();
236+
} else {
237+
ft.disallowAddToBackStack().commit();
238+
}
223239
}
224240

225241
private void switchFragment(Fragment fragment, String tag) {
226-
switchFragment(fragment, tag, false);
242+
switchFragment(fragment, tag, false, false);
227243
}
228244

229245
@Override

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public class EmailLinkFragment extends InvisibleFragmentBase {
3434
private TroubleSigningInListener mListener;
3535
private ScrollView mTopLevelView;
3636

37+
// Used to avoid sending a new email when popping off the fragment backstack
38+
private boolean mEmailSent;
39+
3740
public static EmailLinkFragment newInstance(@NonNull final String email,
3841
@NonNull final ActionCodeSettings
3942
actionCodeSettings) {
@@ -65,15 +68,20 @@ public View onCreateView(@NonNull LayoutInflater inflater,
6568
@Override
6669
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
6770
super.onViewCreated(view, savedInstanceState);
68-
// We need to hide the top level view until we know that the email link has been sent
71+
if (savedInstanceState != null) {
72+
mEmailSent = savedInstanceState.getBoolean(EMAIL_SENT);
73+
}
74+
6975
mTopLevelView = view.findViewById(R.id.top_level_view);
70-
mTopLevelView.setVisibility(View.GONE);
76+
if (!mEmailSent) {
77+
// We need to hide the top level view until we know that the email link has been sent
78+
mTopLevelView.setVisibility(View.GONE);
79+
}
7180

7281
String email = getArguments().getString(ExtraConstants.EMAIL);
7382
setBodyText(view, email);
7483
setOnClickListeners(view, email);
7584
setPrivacyFooter(view);
76-
7785
}
7886

7987
@Override
@@ -85,7 +93,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
8593
ActionCodeSettings actionCodeSettings = getArguments().getParcelable(ExtraConstants
8694
.ACTION_CODE_SETTINGS);
8795

88-
if (savedInstanceState == null || !savedInstanceState.getBoolean(EMAIL_SENT)) {
96+
if (!mEmailSent) {
8997
mEmailLinkSendEmailHandler.sendSignInLinkToEmail(email, actionCodeSettings);
9098
}
9199
}
@@ -106,6 +114,7 @@ public void run() {
106114
mTopLevelView.setVisibility(View.VISIBLE);
107115
}
108116
});
117+
mEmailSent = true;
109118
}
110119

111120
@Override
@@ -142,7 +151,7 @@ private void setPrivacyFooter(View view) {
142151
@Override
143152
public void onSaveInstanceState(Bundle state) {
144153
super.onSaveInstanceState(state);
145-
state.putBoolean(EMAIL_SENT, true);
154+
state.putBoolean(EMAIL_SENT, mEmailSent);
146155
}
147156

148157
interface TroubleSigningInListener {

0 commit comments

Comments
 (0)