|
40 | 40 | import com.firebase.ui.auth.util.data.PhoneNumberUtils;
|
41 | 41 | import com.firebase.ui.auth.util.data.ProviderUtils;
|
42 | 42 | import com.google.android.gms.auth.api.credentials.Credential;
|
| 43 | +import com.google.android.gms.auth.api.credentials.CredentialRequest; |
| 44 | +import com.google.android.gms.auth.api.credentials.CredentialRequestResponse; |
43 | 45 | import com.google.android.gms.auth.api.credentials.CredentialsClient;
|
44 | 46 | import com.google.android.gms.auth.api.signin.GoogleSignIn;
|
| 47 | +import com.google.android.gms.auth.api.signin.GoogleSignInAccount; |
45 | 48 | import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
|
46 | 49 | import com.google.android.gms.common.api.ApiException;
|
47 | 50 | import com.google.android.gms.common.api.CommonStatusCodes;
|
|
50 | 53 | import com.google.android.gms.tasks.Task;
|
51 | 54 | import com.google.android.gms.tasks.Tasks;
|
52 | 55 | import com.google.firebase.FirebaseApp;
|
| 56 | +import com.google.firebase.auth.AuthCredential; |
| 57 | +import com.google.firebase.auth.AuthResult; |
53 | 58 | import com.google.firebase.auth.EmailAuthProvider;
|
54 | 59 | import com.google.firebase.auth.FacebookAuthProvider;
|
55 | 60 | import com.google.firebase.auth.FirebaseAuth;
|
@@ -262,6 +267,75 @@ public static int getDefaultTheme() {
|
262 | 267 | return R.style.FirebaseUI;
|
263 | 268 | }
|
264 | 269 |
|
| 270 | + public Task<AuthResult> silentSignIn(@NonNull final Context context, List<IdpConfig> configs) { |
| 271 | + if (configs.isEmpty()) { |
| 272 | + throw new IllegalArgumentException("Configs must not be empty."); |
| 273 | + } |
| 274 | + for (IdpConfig config : configs) { |
| 275 | + String provider = config.getProviderId(); |
| 276 | + if (!provider.equals(EmailAuthProvider.PROVIDER_ID) |
| 277 | + && !provider.equals(GoogleAuthProvider.PROVIDER_ID)) { |
| 278 | + throw new IllegalArgumentException("Only email and google providers are supported " + |
| 279 | + "for silent sign-in."); |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + if (mAuth.getCurrentUser() != null) { |
| 284 | + return Tasks.forException(new IllegalStateException("User already signed in!")); |
| 285 | + } |
| 286 | + |
| 287 | + final IdpConfig google = |
| 288 | + ProviderUtils.getConfigFromIdps(configs, GoogleAuthProvider.PROVIDER_ID); |
| 289 | + final IdpConfig email = |
| 290 | + ProviderUtils.getConfigFromIdps(configs, EmailAuthProvider.PROVIDER_ID); |
| 291 | + |
| 292 | + GoogleSignInOptions googleOptions = null; |
| 293 | + if (google != null) { |
| 294 | + GoogleSignInAccount last = GoogleSignIn.getLastSignedInAccount(context); |
| 295 | + if (last != null && last.getIdToken() != null) { |
| 296 | + return mAuth.signInWithCredential(GoogleAuthProvider.getCredential( |
| 297 | + last.getIdToken(), null)); |
| 298 | + } |
| 299 | + |
| 300 | + googleOptions = google.getParams() |
| 301 | + .getParcelable(ExtraConstants.EXTRA_GOOGLE_SIGN_IN_OPTIONS); |
| 302 | + } |
| 303 | + |
| 304 | + final GoogleSignInOptions finalGoogleOptions = googleOptions; |
| 305 | + return GoogleApiUtils.getCredentialsClient(context) |
| 306 | + .request(new CredentialRequest.Builder() |
| 307 | + .setPasswordLoginSupported(email != null) |
| 308 | + .setAccountTypes(google == null ? null : |
| 309 | + ProviderUtils.providerIdToAccountType(GoogleAuthProvider.PROVIDER_ID)) |
| 310 | + .build()) |
| 311 | + .continueWithTask(new Continuation<CredentialRequestResponse, Task<AuthResult>>() { |
| 312 | + @Override |
| 313 | + public Task<AuthResult> then(@NonNull Task<CredentialRequestResponse> task) { |
| 314 | + Credential credential = task.getResult().getCredential(); |
| 315 | + String email = credential.getId(); |
| 316 | + String password = credential.getPassword(); |
| 317 | + |
| 318 | + if (TextUtils.isEmpty(password)) { |
| 319 | + return GoogleSignIn.getClient(context, |
| 320 | + new GoogleSignInOptions.Builder(finalGoogleOptions) |
| 321 | + .setAccountName(email) |
| 322 | + .build()) |
| 323 | + .silentSignIn() |
| 324 | + .continueWithTask(new Continuation<GoogleSignInAccount, Task<AuthResult>>() { |
| 325 | + @Override |
| 326 | + public Task<AuthResult> then(@NonNull Task<GoogleSignInAccount> task) { |
| 327 | + AuthCredential authCredential = GoogleAuthProvider.getCredential( |
| 328 | + task.getResult().getIdToken(), null); |
| 329 | + return mAuth.signInWithCredential(authCredential); |
| 330 | + } |
| 331 | + }); |
| 332 | + } else { |
| 333 | + return mAuth.signInWithEmailAndPassword(email, password); |
| 334 | + } |
| 335 | + } |
| 336 | + }); |
| 337 | + } |
| 338 | + |
265 | 339 | /**
|
266 | 340 | * Signs the current user out, if one is signed in.
|
267 | 341 | *
|
|
0 commit comments