@Override
 public void onStop() {
   super.onStop();
   if (mAuthListener != null) {
     mAuth.removeAuthStateListener(mAuthListener);
   }
 }
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAuth = FirebaseAuth.getInstance();

    mDatabase = new Firebase(Const.FIREBASE_URL + "stuffs");
    mAuthListener =
        new FirebaseAuth.AuthStateListener() {
          @Override
          public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
              Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
              // User is signed out
              Log.d(TAG, "onAuthStateChanged:signed_out");
              Intent errorIntent = new Intent(getActivity(), SignInActivity.class);
              startActivity(errorIntent);
              Toast.makeText(getActivity(), "Some error arose, please relogin", Toast.LENGTH_SHORT)
                  .show();
            }
          }
        };
  }
  // [START auth_with_google]
  private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    // [START_EXCLUDE]
    showProgressDialog();
    // [END_EXCLUDE]
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth
        .signInWithCredential(credential)
        .addOnCompleteListener(
            this,
            new OnCompleteListener<AuthResult>() {
              @Override
              public void onComplete(@NonNull Task<AuthResult> task) {
                Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                // If sign in fails, display a message to the user. If sign in succeeds
                // the auth state listener will be notified and logic to handle the
                // signed in user can be handled in the listener.
                if (!task.isSuccessful()) {
                  Log.w(TAG, "signInWithCredential", task.getException());
                  Toast.makeText(
                          GoogleSignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT)
                      .show();
                }
                // [START_EXCLUDE]
                hideProgressDialog();
                // [END_EXCLUDE]
              }
            });
  }
 @Override
 @WorkerThread
 @Nullable
 public FirebaseUser signInWithCredential(@NonNull AuthCredential credential) {
   Task<AuthResult> curTask = mFirebaseAuth.signInWithCredential(credential);
   AuthResult authResult = await(curTask);
   return authResult == null ? null : authResult.getUser();
 }
 @Override
 @WorkerThread
 public boolean isExistingAccount(@Nullable final String email) {
   if (email == null) {
     return false;
   }
   return hasProviders(await(mFirebaseAuth.fetchProvidersForEmail(email)));
 }
 @Override
 @WorkerThread
 @Nullable
 public FirebaseUser signInWithEmailPassword(
     @NonNull String emailAddress, @NonNull String password) {
   AuthResult authResult = await(mFirebaseAuth.signInWithEmailAndPassword(emailAddress, password));
   return authResult == null ? null : authResult.getUser();
 }
Example #7
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Setup the Google API object to allow Google logins */

    mAuth = FirebaseAuth.getInstance();
  }
Example #8
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    mAuth = FirebaseAuth.getInstance();

    initGoogleSignIn();
  }
 @Override
 @WorkerThread
 @Nullable
 public FirebaseUser createUserWithEmailAndPassword(
     @NonNull String emailAddress, @NonNull String password)
     throws ExecutionException, InterruptedException {
   Task<AuthResult> curTask;
   curTask = mFirebaseAuth.createUserWithEmailAndPassword(emailAddress, password);
   Tasks.await(curTask);
   return curTask.getResult().getUser();
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google);

    // Views
    mStatusTextView = (TextView) findViewById(R.id.status);
    mDetailTextView = (TextView) findViewById(R.id.detail);

    // Button listeners
    findViewById(R.id.sign_in_button).setOnClickListener(this);
    findViewById(R.id.sign_out_button).setOnClickListener(this);
    findViewById(R.id.disconnect_button).setOnClickListener(this);

    // [START config_signin]
    // Configure Google Sign In
    GoogleSignInOptions gso =
        new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    // [END config_signin]

    mGoogleApiClient =
        new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    // [START initialize_auth]
    mAuth = FirebaseAuth.getInstance();
    // [END initialize_auth]

    // [START auth_state_listener]
    mAuthListener =
        new FirebaseAuth.AuthStateListener() {
          @Override
          public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
              // User is signed in
              Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
              // User is signed out
              Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // [START_EXCLUDE]
            updateUI(user);
            // [END_EXCLUDE]
          }
        };
    // [END auth_state_listener]
  }
 @Override
 @WorkerThread
 public boolean resetPasswordForEmail(@NonNull String emailAddress) {
   Task<Void> curTask = mFirebaseAuth.sendPasswordResetEmail(emailAddress);
   try {
     Tasks.await(curTask);
   } catch (ExecutionException | InterruptedException e) {
     Log.w(TAG, "attempt to reset password failed", e);
     return false;
   }
   return true;
 }
  private void revokeAccess() {
    // Firebase sign out
    mAuth.signOut();

    // Google revoke access
    Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient)
        .setResultCallback(
            new ResultCallback<Status>() {
              @Override
              public void onResult(@NonNull Status status) {
                updateUI(null);
              }
            });
  }
  private void initUI() {

    firebaseAuth = FirebaseAuth.getInstance();

    if (firebaseAuth.getCurrentUser() != null) {
      startActivity(new Intent(this, MainActivity.class));
      finish();
    }

    tilEmail = (TextInputLayout) findViewById(R.id.tilEmail);
    clGeneral = (CoordinatorLayout) findViewById(R.id.clGeneral);
    tilPassword = (TextInputLayout) findViewById(R.id.tilPassword);
    tieEmail = (TextInputEditText) findViewById(R.id.tieEmail);
    tiePassword = (TextInputEditText) findViewById(R.id.tiePassword);
    btnLogin = (AppCompatButton) findViewById(R.id.btnLogin);
    lblRecoveryPassword = (AppCompatTextView) findViewById(R.id.lblRecoveryPassword);
    lblCreateAccount = (AppCompatTextView) findViewById(R.id.lblCreateAccount);

    btnLogin.setOnClickListener(this);
    lblRecoveryPassword.setOnClickListener(this);
    lblCreateAccount.setOnClickListener(this);

    tieEmail.addTextChangedListener(new CustomTextWatcher(tieEmail));
    tiePassword.addTextChangedListener(new CustomTextWatcher(tiePassword));

    tiePassword.setOnEditorActionListener(
        new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
              btnLogin.performClick();
              return true;
            }
            return false;
          }
        });
  }
  @Override
  @WorkerThread
  @NonNull
  public List<String> getProvidersForEmail(@Nullable String emailAddress) {
    if (emailAddress == null) {
      return Collections.emptyList();
    }

    ProviderQueryResult result = await(mFirebaseAuth.fetchProvidersForEmail(emailAddress));
    if (hasProviders(result)) {
      return result.getProviders();
    }

    return Collections.emptyList();
  }
Example #15
0
 private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
   AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
   mAuth
       .signInWithCredential(credential)
       .addOnCompleteListener(
           this,
           new OnCompleteListener<AuthResult>() {
             @Override
             public void onComplete(@NonNull Task<AuthResult> task) {
               if (!task.isSuccessful()) {
                 Toast.makeText(
                         SignInActivity.this, R.string.erro_google_sign_in, Toast.LENGTH_SHORT)
                     .show();
               }
             }
           });
 }
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.btnLogin:
        if (!validEmail()) {
          return;
        }

        if (!validPassword()) {
          return;
        }

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

                    if (task.isSuccessful()) {
                      startActivity(new Intent(LoginActivity.this, MainActivity.class));
                      finish();
                    } else {
                      Util.showSnackBar(clGeneral, getString(R.string.msg_invalid_login));
                    }
                  }
                });

        break;
      case R.id.lblCreateAccount:
        startActivity(new Intent(this, CreateAccountActivity.class));
        finish();
        break;
      case R.id.lblRecoveryPassword:
        FragmentManager fm = this.getSupportFragmentManager();
        RecoveryDialogFragment recoveryDialogFragment = new RecoveryDialogFragment();
        recoveryDialogFragment.show(fm, "layout_filter_checkbox_dialog");
        break;
      default:
        break;
    }
  }
 @Override
 @WorkerThread
 @Nullable
 public FirebaseUser getCurrentUser() {
   return mFirebaseAuth.getCurrentUser();
 }
 @Override
 public void onStart() {
   super.onStart();
   mAuth.addAuthStateListener(mAuthListener);
 }