Search in sources :

Example 21 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project quickstart-android by firebase.

the class GoogleSignInActivity method onCreate.

@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).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]
}
Also used : GoogleSignInOptions(com.google.android.gms.auth.api.signin.GoogleSignInOptions) FirebaseUser(com.google.firebase.auth.FirebaseUser) FirebaseAuth(com.google.firebase.auth.FirebaseAuth)

Example 22 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project SocialRec by Jkuras.

the class SignIn_SignUp method sendEmailVerification.

private void sendEmailVerification() {
    // Disable button
    findViewById(R.id.verify_email_button).setEnabled(false);
    // Send verification email
    // [START send_email_verification]
    final FirebaseUser user = mAuth.getCurrentUser();
    user.sendEmailVerification().addOnCompleteListener(this, new OnCompleteListener<Void>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
            // [START_EXCLUDE]
            // Re-enable button
            findViewById(R.id.verify_email_button).setEnabled(true);
            if (task.isSuccessful()) {
                Toast.makeText(SignIn_SignUp.this, "Verification email sent to " + user.getEmail(), Toast.LENGTH_SHORT).show();
            } else {
                Log.e(TAG, "sendEmailVerification", task.getException());
                Toast.makeText(SignIn_SignUp.this, "Failed to send verification email.", Toast.LENGTH_SHORT).show();
            }
        // [END_EXCLUDE]
        }
    });
// [END send_email_verification]
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 23 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project NPSmiles by bmcglynn1.

the class LoginAuth method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_auth);
    setTitle("Login");
    mAuth = FirebaseAuth.getInstance();
    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");
            }
        // ...
        }
    };
    final EditText emailText = (EditText) findViewById(R.id.emailEditText);
    final EditText passwordText = (EditText) findViewById(R.id.passwordEditText);
    Button loginButton = (Button) findViewById(R.id.loginButton);
    loginButton.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (emailText.getText().toString().length() != 0 && passwordText.getText().toString().length() != 0) {
                signIn(emailText.getText().toString(), passwordText.getText().toString());
            }
        }
    });
    findViewById(R.id.relativeLayoutLogin).setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getRootView().getWindowToken(), 0);
            return true;
        }
    });
}
Also used : EditText(android.widget.EditText) Button(android.widget.Button) InputMethodManager(android.view.inputmethod.InputMethodManager) FirebaseUser(com.google.firebase.auth.FirebaseUser) View(android.view.View) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) MotionEvent(android.view.MotionEvent)

Example 24 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project NPSmiles by bmcglynn1.

the class MainActivity method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    FirebaseUser user = mAuth.getCurrentUser();
    optionsMenu = menu;
    if (user == null) {
        getMenuInflater().inflate(R.menu.menu_main_unauthorized, menu);
    } else {
        getMenuInflater().inflate(R.menu.menu_main, menu);
    }
    return true;
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 25 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project MadMax by deviz92.

the class ProfileEdit method updateAccount.

private boolean updateAccount() {
    Log.i(TAG, "createAccount");
    if (!validateForm()) {
        Log.i(TAG, "submitted form is not valid");
        Toast.makeText(this, "Invalid form!", Toast.LENGTH_SHORT).show();
        return false;
    }
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user == null) {
        Log.e(TAG, "Error while retriving current user from db");
        Toast.makeText(this, "Error while retriving current user from db", Toast.LENGTH_LONG).show();
        return false;
    }
    final String currentUserID = currentUser.getID();
    String newName = nameView.getText().toString();
    String newSurname = surnameView.getText().toString();
    String newUsername = usernameView.getText().toString();
    //String newEmail = emailView.getText().toString();
    String newPassword = passwordView.getText().toString();
    if (!newName.isEmpty() && (currentUser.getName() == null || !currentUser.getName().equals(newName))) {
        currentUser.setName(newName);
        databaseReference.child("users").child(currentUserID).child("name").setValue(currentUser.getName());
    }
    if (!newSurname.isEmpty() && (currentUser.getSurname() == null || !currentUser.getSurname().equals(newSurname))) {
        currentUser.setSurname(newSurname);
        databaseReference.child("users").child(currentUserID).child("surname").setValue(currentUser.getSurname());
    }
    if (!newUsername.isEmpty() && (currentUser.getUsername() == null || !currentUser.getUsername().equals(newUsername))) {
        currentUser.setUsername(newUsername);
        databaseReference.child("users").child(currentUserID).child("username").setValue(currentUser.getUsername());
    }
    if (IMAGE_CHANGED) {
        // for saving image
        StorageReference uProfileImageFilenameRef = storageReference.child("users").child(currentUserID).child(currentUserID + "_profileImage.jpg");
        // Get the data from an ImageView as bytes
        profileImageView.setDrawingCacheEnabled(true);
        profileImageView.buildDrawingCache();
        Bitmap bitmap = profileImageView.getDrawingCache();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] data = baos.toByteArray();
        UploadTask uploadTask = uProfileImageFilenameRef.putBytes(data);
        uploadTask.addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                // todo Handle unsuccessful uploads
                Log.e(TAG, "image upload failed");
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                currentUser.setProfileImage(taskSnapshot.getMetadata().getDownloadUrl().toString());
                databaseReference.child("users").child(currentUserID).child("image").setValue(currentUser.getProfileImage());
            }
        });
    }
    if (!newPassword.isEmpty() && !currentUser.getPassword().equals(User.encryptPassword(newPassword))) {
        try {
            user.updatePassword(newPassword);
        } catch (Exception e) {
            Log.e(TAG, e.getClass().toString() + ", message: " + e.getMessage());
            Toast.makeText(this, "Can't update password", Toast.LENGTH_LONG).show();
            return false;
        }
        currentUser.setPassword(newPassword);
        databaseReference.child("users").child(currentUserID).child("password").setValue(currentUser.getPassword());
    }
    return true;
}
Also used : StorageReference(com.google.firebase.storage.StorageReference) FirebaseUser(com.google.firebase.auth.FirebaseUser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bitmap(android.graphics.Bitmap) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)26 AuthResult (com.google.firebase.auth.AuthResult)7 Button (android.widget.Button)6 FakeAuthResult (com.firebase.ui.auth.testhelpers.FakeAuthResult)6 BuildConfig (com.firebase.ui.auth.BuildConfig)5 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)5 Test (org.junit.Test)5 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)4 Config (org.robolectric.annotation.Config)4 NonNull (android.support.annotation.NonNull)3 EditText (android.widget.EditText)3 ActivityHelperShadow (com.firebase.ui.auth.testhelpers.ActivityHelperShadow)3 TaskFailureLogger (com.firebase.ui.auth.ui.TaskFailureLogger)3 Task (com.google.android.gms.tasks.Task)3 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Bitmap (android.graphics.Bitmap)2 IdpResponse (com.firebase.ui.auth.IdpResponse)2 FirebaseAuthWeakPasswordException (com.google.firebase.auth.FirebaseAuthWeakPasswordException)2