Search in sources :

Example 41 with GoogleSignInAccount

use of com.google.android.gms.auth.api.signin.GoogleSignInAccount in project iosched by google.

the class SignInManager method onActivityResult.

/**
 * Method for processing sign in logic in the bonding activity's
 * {@link Activity#onActivityResult(int, int, Intent)}.
 *
 * @param requestCode The requestCode argument of the activity's onActivityResult.
 * @param resultCode  The resultCode argument of the activity's onActivityResult.
 * @param data        The Intent argument of the activity's onActivityResult.
 */
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    final SignInListener signInListener = getSignInListener();
    if (signInListener == null) {
        return;
    }
    if (requestCode == AccountUtils.RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            if (acct != null) {
                performPostSignInTasks(acct, result);
            }
        } else {
            LOGW(TAG, "Sign in failed");
            signInListener.onSignInFailed(result);
        }
    }
}
Also used : GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) GoogleSignInResult(com.google.android.gms.auth.api.signin.GoogleSignInResult)

Example 42 with GoogleSignInAccount

use of com.google.android.gms.auth.api.signin.GoogleSignInAccount in project androidApp by InspectorIncognito.

the class GoogleAccountLoginner method handleSignInResult.

private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        GoogleDataManager manager = new GoogleDataManager();
        manager.getCredentials(listener, acct, mGoogleApiClient);
    } else {
        listener.onError(new ServerStatusCode(LOGIN_API_ERROR));
        mGoogleApiClient.stopAutoManage(compatActivity.getActivity());
        mGoogleApiClient.disconnect();
    }
}
Also used : GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) ServerStatusCode(cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode)

Example 43 with GoogleSignInAccount

use of com.google.android.gms.auth.api.signin.GoogleSignInAccount in project FirebaseUI-Android by firebase.

the class AuthUI method silentSignIn.

/**
 * Signs the user in without any UI if possible. If this operation fails, you can safely start a
 * UI-based sign-in flow knowing it is required.
 *
 * @param context requesting the user be signed in
 * @param configs to use for silent sign in. Only Google and email are currently supported, the
 *                rest will be ignored.
 * @return a task which indicates whether or not the user was successfully signed in.
 */
@NonNull
public Task<AuthResult> silentSignIn(@NonNull Context context, @NonNull List<IdpConfig> configs) {
    if (mAuth.getCurrentUser() != null) {
        throw new IllegalArgumentException("User already signed in!");
    }
    final Context appContext = context.getApplicationContext();
    final IdpConfig google = ProviderUtils.getConfigFromIdps(configs, GoogleAuthProvider.PROVIDER_ID);
    final IdpConfig email = ProviderUtils.getConfigFromIdps(configs, EmailAuthProvider.PROVIDER_ID);
    if (google == null && email == null) {
        throw new IllegalArgumentException("No supported providers were supplied. " + "Add either Google or email support.");
    }
    final GoogleSignInOptions googleOptions;
    if (google == null) {
        googleOptions = null;
    } else {
        GoogleSignInAccount last = GoogleSignIn.getLastSignedInAccount(appContext);
        if (last != null && last.getIdToken() != null) {
            return mAuth.signInWithCredential(GoogleAuthProvider.getCredential(last.getIdToken(), null));
        }
        googleOptions = google.getParams().getParcelable(ExtraConstants.GOOGLE_SIGN_IN_OPTIONS);
    }
    // If Play services are not available we can't attempt to use the credentials client.
    if (!GoogleApiUtils.isPlayServicesAvailable(context)) {
        return Tasks.forException(new FirebaseUiException(ErrorCodes.PLAY_SERVICES_UPDATE_CANCELLED));
    }
    return GoogleApiUtils.getCredentialsClient(context).request(new CredentialRequest.Builder().setPasswordLoginSupported(email != null).setAccountTypes(google == null ? null : ProviderUtils.providerIdToAccountType(GoogleAuthProvider.PROVIDER_ID)).build()).continueWithTask(task -> {
        Credential credential = task.getResult().getCredential();
        String email1 = credential.getId();
        String password = credential.getPassword();
        if (TextUtils.isEmpty(password)) {
            return GoogleSignIn.getClient(appContext, new GoogleSignInOptions.Builder(googleOptions).setAccountName(email1).build()).silentSignIn().continueWithTask(task1 -> {
                AuthCredential authCredential = GoogleAuthProvider.getCredential(task1.getResult().getIdToken(), null);
                return mAuth.signInWithCredential(authCredential);
            });
        } else {
            return mAuth.signInWithEmailAndPassword(email1, password);
        }
    });
}
Also used : Context(android.content.Context) AuthCredential(com.google.firebase.auth.AuthCredential) CredentialRequest(com.google.android.gms.auth.api.credentials.CredentialRequest) AuthCredential(com.google.firebase.auth.AuthCredential) Credential(com.google.android.gms.auth.api.credentials.Credential) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) GoogleSignInOptions(com.google.android.gms.auth.api.signin.GoogleSignInOptions) NonNull(androidx.annotation.NonNull)

Example 44 with GoogleSignInAccount

use of com.google.android.gms.auth.api.signin.GoogleSignInAccount in project FirebaseUI-Android by firebase.

the class GoogleSignInHandler method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode != RequestCodes.GOOGLE_PROVIDER) {
        return;
    }
    try {
        GoogleSignInAccount account = GoogleSignIn.getSignedInAccountFromIntent(data).getResult(ApiException.class);
        setResult(Resource.forSuccess(createIdpResponse(account)));
    } catch (ApiException e) {
        if (e.getStatusCode() == CommonStatusCodes.INVALID_ACCOUNT) {
            // If we get INVALID_ACCOUNT, it means the pre-set account was not available on the
            // device so set the email to null and launch the sign-in picker.
            mEmail = null;
            start();
        } else if (e.getStatusCode() == GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS) {
            // Hack for https://github.com/googlesamples/google-services/issues/345
            // Google remembers the account so the picker doesn't appear twice for the user.
            start();
        } else if (e.getStatusCode() == GoogleSignInStatusCodes.SIGN_IN_CANCELLED) {
            setResult(Resource.forFailure(new UserCancellationException()));
        } else {
            if (e.getStatusCode() == CommonStatusCodes.DEVELOPER_ERROR) {
                Log.w(TAG, "Developer error: this application is misconfigured. " + "Check your SHA1 and package name in the Firebase console.");
            }
            setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.PROVIDER_ERROR, "Code: " + e.getStatusCode() + ", message: " + e.getMessage())));
        }
    }
}
Also used : GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 45 with GoogleSignInAccount

use of com.google.android.gms.auth.api.signin.GoogleSignInAccount in project WordPress-Login-Flow-Android by wordpress-mobile.

the class SignupGoogleFragment method onActivityResult.

@Override
public void onActivityResult(int request, int result, Intent data) {
    super.onActivityResult(request, result, data);
    switch(request) {
        case REQUEST_SIGNUP:
            disconnectGoogleClient();
            mSignupRequested = false;
            if (result == RESULT_OK) {
                GoogleSignInResult signInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                if (signInResult.isSuccess()) {
                    AppLog.d(T.MAIN, "GOOGLE SIGNUP: sign up result returned - succcess");
                    try {
                        GoogleSignInAccount account = signInResult.getSignInAccount();
                        if (account != null) {
                            mDisplayName = account.getDisplayName() != null ? account.getDisplayName() : "";
                            mGoogleEmail = account.getEmail() != null ? account.getEmail() : "";
                            mIdToken = account.getIdToken() != null ? account.getIdToken() : "";
                            mPhotoUrl = removeScaleFromGooglePhotoUrl(account.getPhotoUrl() != null ? account.getPhotoUrl().toString() : "");
                        }
                        AppLog.d(T.MAIN, "GOOGLE SIGNUP: sign up result returned - dispatching SocialSignupAction");
                        dispatchSocialSignup(mIdToken);
                    } catch (NullPointerException exception) {
                        AppLog.d(T.MAIN, "GOOGLE SIGNUP: sign up result returned - NPE");
                        AppLog.e(T.NUX, "Cannot get ID token from Google signup account.", exception);
                        showError(getString(R.string.login_error_generic));
                    }
                } else {
                    AppLog.d(T.MAIN, "GOOGLE SIGNUP: sign up result returned - error");
                    mAnalyticsListener.trackSignupSocialButtonFailure();
                    switch(signInResult.getStatus().getStatusCode()) {
                        // Internal error.
                        case GoogleSignInStatusCodes.INTERNAL_ERROR:
                            AppLog.e(T.NUX, "Google Signup Failed: internal error.");
                            showError(getString(R.string.login_error_generic));
                            break;
                        // Attempted to connect with an invalid account name specified.
                        case GoogleSignInStatusCodes.INVALID_ACCOUNT:
                            AppLog.e(T.NUX, "Google Signup Failed: invalid account name.");
                            showError(getString(R.string.login_error_generic) + getString(R.string.login_error_suffix));
                            break;
                        // Network error.
                        case GoogleSignInStatusCodes.NETWORK_ERROR:
                            AppLog.e(T.NUX, "Google Signup Failed: network error.");
                            showError(getString(R.string.error_generic_network));
                            break;
                        // Cancelled by the user.
                        case GoogleSignInStatusCodes.SIGN_IN_CANCELLED:
                            AppLog.e(T.NUX, "Google Signup Failed: cancelled by user.");
                            break;
                        // Attempt didn't succeed with the current account.
                        case GoogleSignInStatusCodes.SIGN_IN_FAILED:
                            AppLog.e(T.NUX, "Google Signup Failed: current account failed.");
                            showError(getString(R.string.login_error_generic));
                            break;
                        // Attempted to connect, but the user is not signed in.
                        case GoogleSignInStatusCodes.SIGN_IN_REQUIRED:
                            AppLog.e(T.NUX, "Google Signup Failed: user is not signed in.");
                            showError(getString(R.string.login_error_generic));
                            break;
                        // Timeout error.
                        case GoogleSignInStatusCodes.TIMEOUT:
                            AppLog.e(T.NUX, "Google Signup Failed: timeout error.");
                            showError(getString(R.string.google_error_timeout));
                            break;
                        // Unknown error.
                        default:
                            AppLog.e(T.NUX, "Google Signup Failed: unknown error.");
                            showError(getString(R.string.login_error_generic));
                            break;
                    }
                }
            } else if (result == RESULT_CANCELED) {
                AppLog.d(T.MAIN, "GOOGLE SIGNUP: sign up result returned - canceled");
                mAnalyticsListener.trackSignupSocialButtonFailure();
                AppLog.e(T.NUX, "Google Signup Failed: result was CANCELED.");
                finishFlow();
            } else {
                AppLog.d(T.MAIN, "GOOGLE SIGNUP: sign up result returned - unknown");
                mAnalyticsListener.trackSignupSocialButtonFailure();
                AppLog.e(T.NUX, "Google Signup Failed: result was not OK or CANCELED.");
                showError(getString(R.string.login_error_generic));
            }
            break;
    }
}
Also used : GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) GoogleSignInResult(com.google.android.gms.auth.api.signin.GoogleSignInResult)

Aggregations

GoogleSignInAccount (com.google.android.gms.auth.api.signin.GoogleSignInAccount)48 ApiException (com.google.android.gms.common.api.ApiException)15 GoogleSignInResult (com.google.android.gms.auth.api.signin.GoogleSignInResult)12 Intent (android.content.Intent)7 IOException (java.io.IOException)5 User (com.androidstudy.andelamedmanager.data.model.User)2 GoogleSignInOptions (com.google.android.gms.auth.api.signin.GoogleSignInOptions)2 Scope (com.google.android.gms.common.api.Scope)2 Request (okhttp3.Request)2 JSONException (org.json.JSONException)2 Context (android.content.Context)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 NonNull (androidx.annotation.NonNull)1 ServerStatusCode (cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 IntentResultListener (com.codename1.impl.android.IntentResultListener)1 AccessToken (com.codename1.io.AccessToken)1 ConnectionRequest (com.codename1.io.ConnectionRequest)1