Skip to content

Commit 0dd5ea5

Browse files
committed
Improve silent sign-in API
Signed-off-by: Alex Saveau <[email protected]>
1 parent cb25def commit 0dd5ea5

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,36 +271,39 @@ public static int getDefaultTheme() {
271271
* Signs the user in without any UI if possible. If this operation fails, you can safely start a
272272
* UI-based sign-in flow knowing it is required.
273273
*
274-
* @param context requesting the user be signed in
275-
* @param configs to use for silent sign in
274+
* @param context requesting the user be signed in
275+
* @param desiredConfigs to use for silent sign in. Only Google and email are currently
276+
* supported, the rest will be ignored.
276277
* @return a task which indicates whether or not the user was successfully signed in.
277278
*/
278279
@NonNull
279-
public Task<AuthResult> silentSignIn(@NonNull final Context context,
280-
@NonNull List<IdpConfig> configs) {
281-
if (configs.isEmpty()) {
280+
public Task<AuthResult> silentSignIn(@NonNull Context context,
281+
@NonNull List<IdpConfig> desiredConfigs) {
282+
if (mAuth.getCurrentUser() != null) {
283+
throw new IllegalArgumentException("User already signed in!");
284+
}
285+
if (desiredConfigs.isEmpty()) {
282286
throw new IllegalArgumentException("At least one provider must be specified.");
283287
}
284-
for (IdpConfig config : configs) {
288+
289+
List<IdpConfig> configs = new ArrayList<>();
290+
for (IdpConfig config : desiredConfigs) {
285291
String provider = config.getProviderId();
286-
if (!provider.equals(EmailAuthProvider.PROVIDER_ID)
287-
&& !provider.equals(GoogleAuthProvider.PROVIDER_ID)) {
288-
throw new IllegalArgumentException("Only email and google providers are supported " +
289-
"for silent sign-in.");
292+
if (provider.equals(EmailAuthProvider.PROVIDER_ID)
293+
|| provider.equals(GoogleAuthProvider.PROVIDER_ID)) {
294+
configs.add(config);
290295
}
291296
}
292-
if (mAuth.getCurrentUser() != null) {
293-
throw new IllegalArgumentException("User already signed in!");
294-
}
295297

298+
final Context appContext = context.getApplicationContext();
296299
final IdpConfig google =
297300
ProviderUtils.getConfigFromIdps(configs, GoogleAuthProvider.PROVIDER_ID);
298301
final IdpConfig email =
299302
ProviderUtils.getConfigFromIdps(configs, EmailAuthProvider.PROVIDER_ID);
300303

301304
GoogleSignInOptions googleOptions = null;
302305
if (google != null) {
303-
GoogleSignInAccount last = GoogleSignIn.getLastSignedInAccount(context);
306+
GoogleSignInAccount last = GoogleSignIn.getLastSignedInAccount(appContext);
304307
if (last != null && last.getIdToken() != null) {
305308
return mAuth.signInWithCredential(GoogleAuthProvider.getCredential(
306309
last.getIdToken(), null));
@@ -325,7 +328,7 @@ public Task<AuthResult> then(@NonNull Task<CredentialRequestResponse> task) {
325328
String password = credential.getPassword();
326329

327330
if (TextUtils.isEmpty(password)) {
328-
return GoogleSignIn.getClient(context,
331+
return GoogleSignIn.getClient(appContext,
329332
new GoogleSignInOptions.Builder(finalGoogleOptions)
330333
.setAccountName(email)
331334
.build())

0 commit comments

Comments
 (0)