1

The code seems to work. Facebook and email log in work. When I click the twitter button it takes me to the twitter sign in and authorization page, I log in, then nothing happens. The log does not show success and console does not show a new user. No errors either. Here's the log in fragment. Sorry if it's messy but I'm not very experienced.

public class Login_Fragment extends Fragment implements OnClickListener {
    private View view;


    private EditText editTextEmail, editTextPassword;
    private Button loginButton;
    private TextView forgotPassword, signUp;
    private CheckBox show_hide_password, remember_me;
    private LinearLayout loginLayout;
    private static Animation shakeAnimation;


    private FirebaseAuth.AuthStateListener mAuthListener;

    FragmentManager manager;

    private FirebaseAuth mAuth;

    private TwitterLoginButton mLoginButton;

    private static final String TAG = "TwitterLogin";




    public Login_Fragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        TwitterAuthConfig authConfig = new TwitterAuthConfig(
                getString(R.string.twitter_consumer_key),
                getString(R.string.twitter_consumer_secret));
        Fabric.with(getActivity(), new com.twitter.sdk.android.Twitter(authConfig), new Twitter(authConfig));

        view = inflater.inflate(R.layout.login_layout, container, false);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        manager = getFragmentManager();

        mAuth = FirebaseAuth.getInstance();


        initViews();
        setListeners();



        mLoginButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {

                Log.d(TAG, "twitterLogin:success" + result);

                handleTwitterSession(result.data);


            }

            @Override
            public void failure(TwitterException exception) {

                Log.w(TAG, "twitterLogin:failure", exception);

            }
        });




        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        FirebaseUser currentUser = mAuth.getCurrentUser();
    }

    @Override
    public void onResume(){
        super.onResume();

    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        Fragment fragment = getFragmentManager().findFragmentByTag(Utils.Login_Fragment);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void handleTwitterSession(TwitterSession session) {

        Log.d(TAG, "handleTwitterSession:" + session);


        AuthCredential credential = TwitterAuthProvider.getCredential(
                session.getAuthToken().token,
                session.getAuthToken().secret);


        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            Log.d(TAG, "signInWithCredential:success");

                            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

                            BuySell_Fragment frag = new BuySell_Fragment();

                            ft.replace(R.id.frameContainer, frag);
                            ft.addToBackStack(null);
                            ft.commit();

                        } else {

                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(getContext(), "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();

                        }


                    }
                });
    }



    private void initViews() {

        editTextEmail = (EditText) view.findViewById(R.id.login_emailid);
        editTextPassword = (EditText) view.findViewById(R.id.login_password);
        loginButton = (Button) view.findViewById(R.id.loginBtn);
        forgotPassword = (TextView) view.findViewById(R.id.forgot_password);
        signUp = (TextView) view.findViewById(R.id.createAccount);
        show_hide_password = (CheckBox) view
                .findViewById(R.id.show_hide_password);
        loginLayout = (LinearLayout) view.findViewById(R.id.login_layout);
        remember_me = (CheckBox) view.findViewById(R.id.remember_me);

        mLoginButton = (TwitterLoginButton) view.findViewById(R.id.button_twitter_login);



        shakeAnimation = AnimationUtils.loadAnimation(getActivity(),
                R.anim.shake);


    }

    // Set Listeners
    private void setListeners() {
        loginButton.setOnClickListener(this);
        forgotPassword.setOnClickListener(this);
        signUp.setOnClickListener(this);

        show_hide_password.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton button,
                                         boolean isChecked) {


                if (isChecked) {

                    show_hide_password.setText(R.string.hide_pwd);// change

                    editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT);
                    editTextPassword.setTransformationMethod(HideReturnsTransformationMethod
                            .getInstance());
                } else {
                    show_hide_password.setText(R.string.show_pwd);

                    editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    editTextPassword.setTransformationMethod(PasswordTransformationMethod
                            .getInstance());

                }

            }
        });


    }

    @Override
    public void onClick(View v) {

        String email = editTextEmail.getText().toString();
        String password = editTextPassword.getText().toString();


        switch (v.getId()) {
            case R.id.loginBtn:

                if (email.isEmpty() || password.isEmpty()) {
                    new CustomToast().Show_Toast(getActivity(), view,
                            "المرجو ادخال ايميل و كلمة سر");
                } else {

                    checkValidation();

                    signIn(email, password);

                }


                break;

            case R.id.forgot_password:

                android.support.v4.app.FragmentTransaction ft =
                        getActivity().getSupportFragmentManager().beginTransaction();
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

                ForgotPassword_Fragment frag = new ForgotPassword_Fragment();

                ft.replace(R.id.frameContainer, frag);
                ft.addToBackStack(null);
                ft.commit();

                break;

            case R.id.createAccount:

                android.support.v4.app.FragmentTransaction fpft =
                        getActivity().getSupportFragmentManager().beginTransaction();
                fpft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

                SignUp_Fragment fpfrag = new SignUp_Fragment();

                fpft.replace(R.id.frameContainer, fpfrag);
                fpft.addToBackStack(null);
                fpft.commit();


        }

    }


    private void checkValidation() {

        String getEmailId = editTextEmail.getText().toString();
        String getPassword = editTextPassword.getText().toString();


        Pattern p = Pattern.compile(Utils.regEx);

        Matcher m = p.matcher(getEmailId);

        // Check for both field is empty or not
        if (getEmailId.equals("") || getEmailId.length() == 0
                || getPassword.equals("") || getPassword.length() == 0) {
            loginLayout.startAnimation(shakeAnimation);
            new CustomToast().Show_Toast(getActivity(), view,
                    "جميع الخانات مطلوبة");

        }

        else if (!m.find())
            new CustomToast().Show_Toast(getActivity(), view,
                    "الايميل غير صحيح");


    }


    private void signIn(String email, String password) {

        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (!task.isSuccessful()) {
                    new CustomToast().Show_Toast(getActivity(), view,
                            "محاولة تسجبل الدخول فشلت");
                } else {
                    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

                    BuySell_Fragment frag = new BuySell_Fragment();

                    ft.replace(R.id.frameContainer, frag);
                    ft.addToBackStack(null);
                    ft.commit();
                }

            }
        });

    }


    @Override
    public void onDetach() {
        super.onDetach();

        try {
            Field fragmentManager = Fragment.class.getDeclaredField("mFragmentManager");
            fragmentManager.setAccessible(true);
            fragmentManager.set(this, null);


        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Yousef
  • 307
  • 2
  • 14
  • I am voting to close this question as it seems to have been caused by a temporary service disruption on Firebase's end (as evidenced by the OP's answer). – Dev-iL May 25 '17 at 09:11
  • I contacted firebase support and they said they are aware of the issue and are working on resolving it. – Yousef May 25 '17 at 00:31

2 Answers2

0

Did you do all the backend stuff? I assume that you've added the API secrete and API key to the firebase console when you enabled twitter login in the auth panel, but did you also go over to apps.twitter.com and add the OAuth redirect URI?

https://firebase.google.com/docs/auth/android/twitter-login

James Poag
  • 2,320
  • 1
  • 13
  • 20
0

Pass the ActivityResult inside the Fragment to the TwitterLoginButton

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mLoginButton.onActivityResult(requestCode, resultCode, data);
}

In the Activity which inflates these Fragments use the below code to pass the result from the twitter intent to the child Fragment

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, intent);
    }
}
Gowrav
  • 189
  • 2
  • 17
  • Are you getting failed or success message now?... You said you were not getting the messages..., twitterLogin:failure twitterLogin:success This activity result being passed to the login button just does that... Are you getting the control inside your mLoginButton.setCallback ? – Gowrav May 25 '17 at 04:17
  • If it's a fragment also ensure the onActivityResult is overridden inside the Activity as well with result being passed to the parent using super .. – Gowrav May 25 '17 at 04:36
  • I'm getting no success or failure messages. It is a fragment and when I try to override the parent I get a null point exception – Yousef May 25 '17 at 07:58
  • refer here [onActivityResult is not being called in the fragment](https://stackoverflow.com/a/16449850/1143807) – Gowrav May 25 '17 at 08:13
  • This means you have a client side issue.., this has to be fixed by you.., Firebase team will come back with the same resolve. I had a similar issue for a Placepicker in a fragmented activity, I have updated the answer for this scenario – Gowrav May 25 '17 at 08:18
  • I did exactly that and it got me the null point exception. I gave the firebase all my code and they said it was an issue they were aware of. – Yousef May 25 '17 at 13:45