Skip to content

Commit 345720e

Browse files
committed
update
1 parent 32e3034 commit 345720e

File tree

6 files changed

+49
-248
lines changed

6 files changed

+49
-248
lines changed

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

+1-14
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
140140
});
141141

142142
mBinding.signIn.setOnClickListener(view -> signIn());
143-
144-
mBinding.signInSilent.setOnClickListener(view -> silentSignIn());
145-
143+
146144
if (ConfigurationUtils.isGoogleMisconfigured(this)
147145
|| ConfigurationUtils.isFacebookMisconfigured(this)) {
148146
showSnackbar(R.string.configuration_required);
@@ -229,17 +227,6 @@ private Intent getSignInIntent(@Nullable String link) {
229227
return builder.build();
230228
}
231229

232-
public void silentSignIn() {
233-
getAuthUI().silentSignIn(this, getSelectedProviders())
234-
.addOnCompleteListener(this, task -> {
235-
if (task.isSuccessful()) {
236-
startSignedInActivity(null);
237-
} else {
238-
showSnackbar(R.string.sign_in_failed);
239-
}
240-
});
241-
}
242-
243230
@Override
244231
protected void onResume() {
245232
super.onResume();

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

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.coordinatorlayout.widget.CoordinatorLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:tools="http://schemas.android.com/tools"
45
xmlns:app="http://schemas.android.com/apk/res-auto"
56
android:id="@+id/root"
@@ -27,7 +28,7 @@
2728
android:layout_height="wrap_content"
2829
android:layout_gravity="center_horizontal"
2930
android:text="@string/launch_title"
30-
app:drawableTopCompat="@drawable/firebase_auth_120dp"/>
31+
app:drawableTopCompat="@drawable/firebase_auth_120dp" />
3132

3233
<Button
3334
android:id="@+id/sign_in"
@@ -38,15 +39,6 @@
3839
android:layout_marginTop="16dp"
3940
android:text="@string/sign_in_start" />
4041

41-
<Button
42-
android:id="@+id/sign_in_silent"
43-
style="@style/Widget.MaterialComponents.Button"
44-
android:layout_width="wrap_content"
45-
android:layout_height="wrap_content"
46-
android:layout_gravity="center"
47-
android:layout_marginBottom="16dp"
48-
android:text="@string/sign_in_silent" />
49-
5042
<TextView
5143
style="@style/TextAppearance.MaterialComponents.Subtitle1"
5244
android:layout_width="wrap_content"
@@ -352,4 +344,4 @@
352344

353345
</ScrollView>
354346

355-
</androidx.coordinatorlayout.widget.CoordinatorLayout>
347+
</androidx.coordinatorlayout.widget.CoordinatorLayout>

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

+6-169
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
import com.firebase.ui.auth.util.data.PhoneNumberUtils;
3333
import com.firebase.ui.auth.util.data.ProviderAvailability;
3434
import com.firebase.ui.auth.util.data.ProviderUtils;
35-
import com.google.android.gms.auth.api.credentials.Credential;
36-
import com.google.android.gms.auth.api.credentials.CredentialRequest;
37-
import com.google.android.gms.auth.api.credentials.CredentialsClient;
3835
import com.google.android.gms.auth.api.signin.GoogleSignIn;
3936
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
4037
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
@@ -269,115 +266,6 @@ public static int getDefaultTheme() {
269266
return R.style.FirebaseUI_DefaultMaterialTheme;
270267
}
271268

272-
/**
273-
* Make a list of {@link Credential} from a FirebaseUser. Useful for deleting Credentials, not
274-
* for saving since we don't have access to the password.
275-
*/
276-
private static List<Credential> getCredentialsFromFirebaseUser(@NonNull FirebaseUser user) {
277-
if (TextUtils.isEmpty(user.getEmail()) && TextUtils.isEmpty(user.getPhoneNumber())) {
278-
return Collections.emptyList();
279-
}
280-
281-
List<Credential> credentials = new ArrayList<>();
282-
for (UserInfo userInfo : user.getProviderData()) {
283-
if (FirebaseAuthProvider.PROVIDER_ID.equals(userInfo.getProviderId())) {
284-
continue;
285-
}
286-
287-
String type = ProviderUtils.providerIdToAccountType(userInfo.getProviderId());
288-
if (type == null) {
289-
// Since the account type is null, we've got an email credential. Adding a fake
290-
// password is the only way to tell Smart Lock that this is an email credential.
291-
credentials.add(CredentialUtils.buildCredentialOrThrow(user, "pass", null));
292-
} else {
293-
credentials.add(CredentialUtils.buildCredentialOrThrow(user, null, type));
294-
}
295-
}
296-
297-
return credentials;
298-
}
299-
300-
/**
301-
* Signs the user in without any UI if possible. If this operation fails, you can safely start a
302-
* UI-based sign-in flow knowing it is required.
303-
*
304-
* @param context requesting the user be signed in
305-
* @param configs to use for silent sign in. Only Google and email are currently supported, the
306-
* rest will be ignored.
307-
* @return a task which indicates whether or not the user was successfully signed in.
308-
*/
309-
@NonNull
310-
public Task<AuthResult> silentSignIn(@NonNull Context context,
311-
@NonNull List<IdpConfig> configs) {
312-
if (mAuth.getCurrentUser() != null) {
313-
throw new IllegalArgumentException("User already signed in!");
314-
}
315-
316-
final Context appContext = context.getApplicationContext();
317-
final IdpConfig google =
318-
ProviderUtils.getConfigFromIdps(configs, GoogleAuthProvider.PROVIDER_ID);
319-
final IdpConfig email =
320-
ProviderUtils.getConfigFromIdps(configs, EmailAuthProvider.PROVIDER_ID);
321-
322-
if (google == null && email == null) {
323-
throw new IllegalArgumentException("No supported providers were supplied. " +
324-
"Add either Google or email support.");
325-
}
326-
327-
final GoogleSignInOptions googleOptions;
328-
if (google == null) {
329-
googleOptions = null;
330-
} else {
331-
GoogleSignInAccount last = GoogleSignIn.getLastSignedInAccount(appContext);
332-
if (last != null && last.getIdToken() != null) {
333-
return mAuth.signInWithCredential(GoogleAuthProvider.getCredential(
334-
last.getIdToken(), null));
335-
}
336-
337-
googleOptions = google.getParams()
338-
.getParcelable(ExtraConstants.GOOGLE_SIGN_IN_OPTIONS);
339-
}
340-
341-
// If Play services are not available we can't attempt to use the credentials client.
342-
if (!GoogleApiUtils.isPlayServicesAvailable(context)) {
343-
return Tasks.forException(
344-
new FirebaseUiException(ErrorCodes.PLAY_SERVICES_UPDATE_CANCELLED));
345-
}
346-
347-
return GoogleApiUtils.getCredentialsClient(context)
348-
.request(new CredentialRequest.Builder()
349-
// We can support both email and Google at the same time here because they
350-
// are mutually exclusive. If a user signs in with Google, their email
351-
// account will automatically be upgraded (a.k.a. replaced) with the Google
352-
// one, meaning Smart Lock won't have to show the picker UI.
353-
.setPasswordLoginSupported(email != null)
354-
.setAccountTypes(google == null ? null :
355-
ProviderUtils.providerIdToAccountType(GoogleAuthProvider
356-
.PROVIDER_ID))
357-
.build())
358-
.continueWithTask(task -> {
359-
Credential credential = task.getResult().getCredential();
360-
String email1 = credential.getId();
361-
String password = credential.getPassword();
362-
363-
if (TextUtils.isEmpty(password)) {
364-
return GoogleSignIn.getClient(appContext,
365-
new GoogleSignInOptions.Builder(googleOptions)
366-
.setAccountName(email1)
367-
.build())
368-
.silentSignIn()
369-
.continueWithTask(task1 -> {
370-
AuthCredential authCredential = GoogleAuthProvider
371-
.getCredential(
372-
task1.getResult().getIdToken(), null);
373-
return mAuth.signInWithCredential(authCredential);
374-
});
375-
} else {
376-
return mAuth.signInWithEmailAndPassword(email1, password);
377-
}
378-
});
379-
}
380-
381269
/**
382270
* Signs the current user out, if one is signed in.
383271
*
@@ -393,41 +281,22 @@ public Task<Void> signOut(@NonNull Context context) {
393281
Log.w(TAG, "Google Play services not available during signOut");
394282
}
395283

396-
Task<Void> maybeDisableAutoSignIn = playServicesAvailable
397-
? GoogleApiUtils.getCredentialsClient(context).disableAutoSignIn()
398-
: Tasks.forResult((Void) null);
399-
400-
maybeDisableAutoSignIn
401-
.continueWith(task -> {
402-
// We want to ignore a specific exception, since it's not a good reason
403-
// to fail (see Issue 1156).
404-
Exception e = task.getException();
405-
if (e instanceof ApiException
406-
&& ((ApiException) e).getStatusCode() == CommonStatusCodes
407-
.CANCELED) {
408-
Log.w(TAG, "Could not disable auto-sign in, maybe there are no " +
409-
"SmartLock accounts available?", e);
410-
return null;
411-
}
412-
413-
return task.getResult();
414-
});
284+
Task<Void> maybeDisableAutoSignIn = Tasks.forResult((Void) null);
415285

416286
return Tasks.whenAll(
417287
signOutIdps(context),
418288
maybeDisableAutoSignIn
419289
).continueWith(task -> {
420-
task.getResult(); // Propagate exceptions
290+
task.getResult(); // Propagate exceptions if any.
421291
mAuth.signOut();
422292
return null;
423293
});
424294
}
425295

426296
/**
427-
* Delete the use from FirebaseAuth and delete any associated credentials from the Credentials
428-
* API. Returns a {@link Task} that succeeds if the Firebase Auth user deletion succeeds and
429-
* fails if the Firebase Auth deletion fails. Credentials deletion failures are handled
430-
* silently.
297+
* Delete the user from FirebaseAuth.
298+
*
299+
* <p>Any associated saved credentials are not explicitly deleted with the new APIs.
431300
*
432301
* @param context the calling {@link Context}.
433302
*/
@@ -440,40 +309,8 @@ public Task<Void> delete(@NonNull final Context context) {
440309
"No currently signed in user."));
441310
}
442311

443-
final List<Credential> credentials = getCredentialsFromFirebaseUser(currentUser);
444-
445-
// Ensure the order in which tasks are executed properly destructures the user.
446312
return signOutIdps(context).continueWithTask(task -> {
447-
task.getResult(); // Propagate exception if there was one
448-
449-
if (!GoogleApiUtils.isPlayServicesAvailable(context)) {
450-
Log.w(TAG, "Google Play services not available during delete");
451-
return Tasks.forResult((Void) null);
452-
}
453-
454-
final CredentialsClient client = GoogleApiUtils.getCredentialsClient(context);
455-
List<Task<?>> credentialTasks = new ArrayList<>();
456-
for (Credential credential : credentials) {
457-
credentialTasks.add(client.delete(credential));
458-
}
459-
return Tasks.whenAll(credentialTasks)
460-
.continueWith(task1 -> {
461-
Exception e = task1.getException();
462-
Throwable t = e == null ? null : e.getCause();
463-
if (!(t instanceof ApiException)
464-
|| ((ApiException) t).getStatusCode() !=
465-
CommonStatusCodes.CANCELED) {
466-
// Only propagate the exception if it isn't an invalid account
467-
// one. This can occur if we failed to save the credential or it
468-
// was deleted elsewhere. However, a lack of stored credential
469-
// doesn't mean fully deleting the user failed.
470-
return task1.getResult();
471-
}
472-
473-
return null;
474-
});
475-
}).continueWithTask(task -> {
476-
task.getResult(); // Propagate exception if there was one
313+
task.getResult(); // Propagate exception if there was one.
477314
return currentUser.delete();
478315
});
479316
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.firebase.ui.auth.util;
22

3-
import android.app.Activity;
43
import android.content.Context;
54

6-
import com.google.android.gms.auth.api.credentials.Credentials;
7-
import com.google.android.gms.auth.api.credentials.CredentialsClient;
8-
import com.google.android.gms.auth.api.credentials.CredentialsOptions;
5+
import com.google.android.gms.auth.api.identity.Identity;
6+
import com.google.android.gms.auth.api.identity.SignInClient;
97
import com.google.android.gms.common.ConnectionResult;
108
import com.google.android.gms.common.GoogleApiAvailability;
119

@@ -19,20 +17,12 @@ private GoogleApiUtils() {
1917
}
2018

2119
public static boolean isPlayServicesAvailable(@NonNull Context context) {
22-
return GoogleApiAvailability
23-
.getInstance()
20+
return GoogleApiAvailability.getInstance()
2421
.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;
2522
}
2623

2724
@NonNull
28-
public static CredentialsClient getCredentialsClient(@NonNull Context context) {
29-
CredentialsOptions options = new CredentialsOptions.Builder()
30-
.forceEnableSaveDialog()
31-
.build();
32-
if (context instanceof Activity) {
33-
return Credentials.getClient((Activity) context, options);
34-
} else {
35-
return Credentials.getClient(context, options);
36-
}
25+
public static SignInClient getSignInClient(@NonNull Context context) {
26+
return Identity.getSignInClient(context);
3727
}
38-
}
28+
}

auth/src/main/java/com/firebase/ui/auth/viewmodel/AuthViewModelBase.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.firebase.ui.auth.data.model.FlowParameters;
66
import com.firebase.ui.auth.data.model.Resource;
77
import com.firebase.ui.auth.util.GoogleApiUtils;
8-
import com.google.android.gms.auth.api.credentials.CredentialsClient;
8+
import com.google.android.gms.auth.api.identity.SignInClient;
99
import com.google.firebase.FirebaseApp;
1010
import com.google.firebase.auth.FirebaseAuth;
1111
import com.google.firebase.auth.FirebaseUser;
@@ -16,7 +16,7 @@
1616

1717
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
1818
public abstract class AuthViewModelBase<T> extends OperableViewModel<FlowParameters, Resource<T>> {
19-
private CredentialsClient mCredentialsClient;
19+
private SignInClient mSignInClient;
2020
private FirebaseAuth mAuth;
2121

2222
protected AuthViewModelBase(Application application) {
@@ -27,7 +27,7 @@ protected AuthViewModelBase(Application application) {
2727
protected void onCreate() {
2828
FirebaseApp app = FirebaseApp.getInstance(getArguments().appName);
2929
mAuth = FirebaseAuth.getInstance(app);
30-
mCredentialsClient = GoogleApiUtils.getCredentialsClient(getApplication());
30+
mSignInClient = GoogleApiUtils.getSignInClient(getApplication());
3131
}
3232

3333
@Nullable
@@ -39,16 +39,16 @@ protected FirebaseAuth getAuth() {
3939
return mAuth;
4040
}
4141

42-
protected CredentialsClient getCredentialsClient() {
43-
return mCredentialsClient;
42+
protected SignInClient getSignInClient() {
43+
return mSignInClient;
4444
}
4545

4646
@VisibleForTesting
4747
public void initializeForTesting(FlowParameters parameters,
4848
FirebaseAuth auth,
49-
CredentialsClient client) {
49+
SignInClient client) {
5050
setArguments(parameters);
5151
mAuth = auth;
52-
mCredentialsClient = client;
52+
mSignInClient = client;
5353
}
54-
}
54+
}

0 commit comments

Comments
 (0)