Skip to content

Commit 623bee5

Browse files
committed
Add more docs and cleanup
Signed-off-by: Alex Saveau <[email protected]>
1 parent aa8e43b commit 623bee5

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@
4242
import com.google.android.gms.tasks.OnCompleteListener;
4343
import com.google.android.gms.tasks.Task;
4444
import com.google.firebase.auth.AuthResult;
45-
import com.google.firebase.auth.EmailAuthProvider;
4645
import com.google.firebase.auth.FirebaseAuth;
47-
import com.google.firebase.auth.GoogleAuthProvider;
4846

4947
import java.util.ArrayList;
5048
import java.util.Arrays;
@@ -172,16 +170,7 @@ public void signIn(View view) {
172170

173171
@OnClick(R.id.sign_in_silent)
174172
public void silentSignIn(View view) {
175-
List<IdpConfig> providers = new ArrayList<>();
176-
for (IdpConfig config : getSelectedProviders()) {
177-
String provider = config.getProviderId();
178-
if (provider.equals(EmailAuthProvider.PROVIDER_ID)
179-
|| provider.equals(GoogleAuthProvider.PROVIDER_ID)) {
180-
providers.add(config);
181-
}
182-
}
183-
184-
AuthUI.getInstance().silentSignIn(this, providers)
173+
AuthUI.getInstance().silentSignIn(this, getSelectedProviders())
185174
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
186175
@Override
187176
public void onComplete(@NonNull Task<AuthResult> task) {

auth/README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ and [Web](https://github.com/firebase/firebaseui-web/).
3333
1. [Configuration](#configuration)
3434
1. [Provider config](#identity-provider-configuration)
3535
1. [Usage instructions](#using-firebaseui-for-authentication)
36-
1. [Sign in](#sign-in)
36+
1. [AuthUI sign-in](#authui-sign-in)
3737
1. [Handling responses](#handling-the-sign-in-response)
38+
1. [Silent sign-in](#silent-sign-in)
3839
1. [Sign out](#sign-out)
3940
1. [Account deletion](#deleting-accounts)
4041
1. [Auth flow chart](#authentication-flow-chart)
@@ -169,7 +170,7 @@ If an alternative app instance is required, call
169170
`AuthUI.getInstance(app)` instead, passing the appropriate `FirebaseApp`
170171
instance.
171172

172-
### Sign in
173+
### AuthUI sign-in
173174

174175
If a user is not currently signed in, as can be determined by checking
175176
`auth.getCurrentUser() != null` (where `auth` is the `FirebaseAuth` instance
@@ -352,7 +353,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
352353
showSnackbar(R.string.no_internet_connection);
353354
return;
354355
}
355-
356+
356357
showSnackbar(R.string.unknown_error);
357358
Log.e(TAG, "Sign-in error: ", response.getError());
358359
}
@@ -399,6 +400,40 @@ if (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp()) {
399400
}
400401
```
401402

403+
### Silent sign-in
404+
405+
If a user is not currently signed in, then a silent sign-in process can be started first before
406+
displaying any UI to provide a seamless experience. Silent sign-in uses saved Smart Lock credentials
407+
and returns a successful `Task` only if the user has been fully signed in with Firebase.
408+
409+
Here's an example of how you would use silent sign-in paired with anonymous users to get your users
410+
up and running as fast as possible:
411+
412+
```java
413+
List<IdpConfig> providers = getSelectedProviders();
414+
AuthUI.getInstance().silentSignIn(this, providers)
415+
.continueWithTask(new Continuation<AuthResult, Task<AuthResult>>() {
416+
@Override
417+
public Task<AuthResult> then(@NonNull Task<AuthResult> task) {
418+
if (task.isSuccessful()) {
419+
return task;
420+
} else {
421+
// Ignore any exceptions since we don't care about credential fetch errors.
422+
return FirebaseAuth.getInstance().signInAnonymously();
423+
}
424+
}
425+
}).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
426+
@Override
427+
public void onComplete(@NonNull Task<AuthResult> task) {
428+
if (task.isSuccessful()) {
429+
// Signed in! Start loading data
430+
} else {
431+
// Uh oh, show error message
432+
}
433+
}
434+
});
435+
```
436+
402437
### Sign out
403438

404439
With the integrations provided by AuthUI, signing out a user is a multi-stage process:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ public Task<AuthResult> silentSignIn(@NonNull Context context,
295295
}
296296
}
297297

298+
if (configs.isEmpty()) {
299+
throw new IllegalArgumentException("No supported providers were supplied.");
300+
}
301+
298302
final Context appContext = context.getApplicationContext();
299303
final IdpConfig google =
300304
ProviderUtils.getConfigFromIdps(configs, GoogleAuthProvider.PROVIDER_ID);

0 commit comments

Comments
 (0)