Created
August 26, 2016 15:17
-
-
Save emrekose26/844ea5b5073933d203b455f797a393d3 to your computer and use it in GitHub Desktop.
FirebaseAuth methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check user session | |
auth = FirebaseAuth.getInstance(); | |
if (auth.getCurrentUser() != null) { | |
// User is logged in | |
} | |
// Change Password | |
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); | |
user.updatePassword(newPassword.getText().toString().trim()) | |
.addOnCompleteListener(new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
if (task.isSuccessful()) { | |
Toast.makeText(MainActivity.this, "Password is updated!", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(MainActivity.this, "Failed to update password!", Toast.LENGTH_SHORT).show(); | |
progressBar.setVisibility(View.GONE); | |
} | |
} | |
}); | |
// Change Email | |
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); | |
user.updateEmail(newEmail.getText().toString().trim()) | |
.addOnCompleteListener(new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
if (task.isSuccessful()) { | |
Toast.makeText(MainActivity.this, "Email address is updated.", Toast.LENGTH_LONG).show(); | |
} else { | |
Toast.makeText(MainActivity.this, "Failed to update email!", Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
// Deleting Account / User | |
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); | |
if (user != null) { | |
user.delete() | |
.addOnCompleteListener(new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
if (task.isSuccessful()) { | |
Toast.makeText(MainActivity.this, "Your profile is deleted:( Create a account now!", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(MainActivity.this, "Failed to delete your account!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
// Sign Out | |
auth.signOut(); | |
// this listener will be called when there is change in firebase user session | |
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() { | |
@Override | |
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
FirebaseUser user = firebaseAuth.getCurrentUser(); | |
if (user == null) { | |
// user auth state is changed - user is null | |
// launch login activity | |
startActivity(new Intent(MainActivity.this, LoginActivity.class)); | |
finish(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment