Skip to content

Commit

Permalink
Merge pull request #43 from andy-cameron/update_user_password
Browse files Browse the repository at this point in the history
Password upates, but does not confirm old password
  • Loading branch information
andy-cameron committed Mar 24, 2019
2 parents 2b5a5c0 + e248d73 commit a6fae28
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
Expand All @@ -31,7 +33,9 @@ public class InformationScreenActivity extends AppCompatActivity {

private TextView profileName;
private EditText profileEmail;
private EditText profileOrganisation;
private EditText currentPassword;
private EditText newPassword;
private EditText newPasswordConfirm;
private TextView profileLeaves;

@Override
Expand All @@ -42,8 +46,12 @@ protected void onCreate(Bundle savedInstanceState) {

Button returnToProfileScreen = (Button) findViewById(R.id.return_to_profile);
Button editEmail = (Button) findViewById(R.id.edit_email_button);
Button editPassword = (Button) findViewById(R.id.edit_password);
profileName = findViewById(R.id.profile_name);
profileEmail = findViewById(R.id.profile_email);
currentPassword = findViewById(R.id.current_password);
newPassword = findViewById(R.id.new_password);
newPasswordConfirm = findViewById(R.id.confirm_password);

mUser = FirebaseAuth.getInstance().getCurrentUser();
if (mUser != null) {
Expand Down Expand Up @@ -93,5 +101,44 @@ public void onComplete(@NonNull Task<Void> task) {
});
}
});

editPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (newPassword.getText().toString().equals(newPasswordConfirm.getText().toString())) {
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String userEmail = (String) dataSnapshot.child("email").getValue();
AuthCredential credential = EmailAuthProvider.getCredential(userEmail, currentPassword.getText().toString());
mUser.reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mUser.updatePassword(newPassword.getText().toString()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Password updated");
} else {
Log.d(TAG, "Error password not updated");
}
}
});
}
});
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
} else {
System.out.println("Unsuccessful!!!");
}
}
});
}
}
5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_information_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
android:textSize="14sp"
android:layout_weight="1"/>
<EditText
android:id="@+id/current_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="30dp"
Expand All @@ -168,6 +169,7 @@
android:textSize="14sp"
android:layout_weight="1"/>
<EditText
android:id="@+id/new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="30dp"
Expand All @@ -190,6 +192,7 @@
android:textSize="14sp"
android:layout_weight="1"/>
<EditText
android:id="@+id/confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="30dp"
Expand All @@ -199,7 +202,7 @@
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/go_to_leaderboard"
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/content_panel_button"
Expand Down

0 comments on commit a6fae28

Please sign in to comment.