mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 20:00:50 +00:00
Add method change password
This commit is contained in:
@@ -1,21 +1,42 @@
|
||||
package com.capstone.foodify.Activity;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.capstone.foodify.Common;
|
||||
import com.capstone.foodify.R;
|
||||
import com.google.android.gms.tasks.OnCompleteListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
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.thecode.aestheticdialogs.AestheticDialog;
|
||||
import com.thecode.aestheticdialogs.DialogStyle;
|
||||
import com.thecode.aestheticdialogs.DialogType;
|
||||
import com.thecode.aestheticdialogs.OnDialogClickListener;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ChangePasswordActivity extends AppCompatActivity {
|
||||
|
||||
TextInputLayout textInput_old_password, textInput_password, textInput_password_confirm;
|
||||
|
||||
EditText edtOldPassword, edtPassword, edtPasswordConfirm;
|
||||
ImageView back_image;
|
||||
ConstraintLayout progressLayout;
|
||||
|
||||
Button changePasswordButton;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -32,6 +53,88 @@ public class ChangePasswordActivity extends AppCompatActivity {
|
||||
onBackPressed();
|
||||
}
|
||||
});
|
||||
|
||||
changePasswordButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
//Show progress bar
|
||||
progressLayout.setVisibility(View.VISIBLE);
|
||||
|
||||
if(validateData()){
|
||||
changePassword(edtOldPassword.getText().toString(), edtPasswordConfirm.getText().toString());
|
||||
} else {
|
||||
progressLayout.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean validateData(){
|
||||
boolean dataHasValidate = true;
|
||||
|
||||
//Check password
|
||||
Pattern patternPassword = Pattern.compile(Common.PASSWORD_PATTERN);
|
||||
Matcher matcherPassword = patternPassword.matcher(edtPassword.getText().toString());
|
||||
|
||||
if(!matcherPassword.matches() || edtPassword.getText().toString().length() < 8) {
|
||||
textInput_password.setError("Mật khẩu của bạn cần tối thiểu có 8 ký tự, 1 ký tự viết hoa, 1 số và 1 ký tự đặc biệt!");
|
||||
dataHasValidate = false;
|
||||
} else {
|
||||
textInput_password.setErrorEnabled(false);
|
||||
}
|
||||
|
||||
if(!edtPassword.getText().toString().equals(edtPasswordConfirm.getText().toString())){
|
||||
textInput_password_confirm.setError("Mật khẩu không giống nhau. Vui lòng kiểm tra lại!");
|
||||
dataHasValidate = false;
|
||||
} else {
|
||||
textInput_password_confirm.setErrorEnabled(false);
|
||||
}
|
||||
|
||||
return dataHasValidate;
|
||||
}
|
||||
|
||||
private void changePassword(String oldPassword, String newPassword){
|
||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
|
||||
// Get auth credentials from the user for re-authentication. The example below shows
|
||||
// email and password credentials but there are multiple possible providers,
|
||||
// such as GoogleAuthProvider or FacebookAuthProvider.
|
||||
AuthCredential credential = EmailAuthProvider
|
||||
.getCredential(Common.CURRENT_USER.getEmail(), oldPassword);
|
||||
|
||||
// Prompt the user to re-provide their sign-in credentials
|
||||
user.reauthenticate(credential)
|
||||
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Void> task) {
|
||||
if (task.isSuccessful()) {
|
||||
user.updatePassword(newPassword).addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Void> task) {
|
||||
if (task.isSuccessful()) {
|
||||
new AestheticDialog.Builder(ChangePasswordActivity.this, DialogStyle.RAINBOW, DialogType.SUCCESS)
|
||||
.setTitle("Thàng công!")
|
||||
.setMessage("Đã thay đổi mật khẩu thành công!")
|
||||
.setCancelable(true)
|
||||
.show();
|
||||
|
||||
progressLayout.setVisibility(View.GONE);
|
||||
} else {
|
||||
Toast.makeText(ChangePasswordActivity.this, "Đã có lỗi từ hệ thống, vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
||||
progressLayout.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
new AestheticDialog.Builder(ChangePasswordActivity.this, DialogStyle.RAINBOW, DialogType.ERROR)
|
||||
.setTitle("Lỗi!")
|
||||
.setMessage("Mật khẩu không đúng! Xin vui lòng thử lại!")
|
||||
.setCancelable(true)
|
||||
.show();
|
||||
progressLayout.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initComponent() {
|
||||
@@ -39,7 +142,15 @@ public class ChangePasswordActivity extends AppCompatActivity {
|
||||
textInput_password = findViewById(R.id.textInput_password);
|
||||
textInput_password_confirm = findViewById(R.id.textInput_password_confirm);
|
||||
|
||||
edtOldPassword = findViewById(R.id.edt_old_password);
|
||||
edtPassword = findViewById(R.id.edt_password);
|
||||
edtPasswordConfirm = findViewById(R.id.edt_confirmPassword);
|
||||
|
||||
back_image = findViewById(R.id.back_image);
|
||||
|
||||
changePasswordButton = findViewById(R.id.change_password_button);
|
||||
|
||||
progressLayout = findViewById(R.id.progress_layout);
|
||||
}
|
||||
|
||||
private void setFontUI() {
|
||||
|
||||
@@ -6,118 +6,148 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".Activity.ChangePasswordActivity">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/back_image"
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp"
|
||||
android:src="@drawable/back_button"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginTop="25dp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginTop="40dp"
|
||||
android:fontFamily="@font/bebas"
|
||||
android:text="@string/change_password"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="40sp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/back_image" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/textInput_old_password"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="60dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:hint="@string/old_password"
|
||||
app:boxBackgroundColor="@color/white"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textInput_password"
|
||||
app:passwordToggleEnabled="true"
|
||||
app:passwordToggleTint="@color/primaryColor"
|
||||
app:startIconDrawable="@drawable/baseline_vpn_key_24"
|
||||
app:startIconTint="@color/primaryColor">
|
||||
android:layout_height="match_parent"
|
||||
android:clickable="false"
|
||||
>
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/edt_old_password"
|
||||
<ImageView
|
||||
android:id="@+id/back_image"
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp"
|
||||
android:src="@drawable/back_button"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginTop="25dp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginTop="40dp"
|
||||
android:fontFamily="@font/bebas"
|
||||
android:text="@string/change_password"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="40sp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/back_image" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/textInput_old_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:inputType="textPassword" />
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:hint="@string/old_password"
|
||||
app:boxBackgroundColor="@color/white"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textInput_password"
|
||||
app:passwordToggleEnabled="true"
|
||||
app:passwordToggleTint="@color/primaryColor"
|
||||
app:startIconDrawable="@drawable/baseline_vpn_key_24"
|
||||
app:startIconTint="@color/primaryColor">
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/edt_old_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:inputType="textPassword" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/textInput_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="60dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:hint="@string/password_text"
|
||||
app:boxBackgroundColor="@color/white"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textInput_password_confirm"
|
||||
app:passwordToggleEnabled="true"
|
||||
app:passwordToggleTint="@color/primaryColor"
|
||||
app:startIconDrawable="@drawable/baseline_vpn_key_24"
|
||||
app:startIconTint="@color/primaryColor">
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/edt_password"
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/textInput_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:inputType="textPassword" />
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:hint="@string/password_text"
|
||||
app:boxBackgroundColor="@color/white"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textInput_password_confirm"
|
||||
app:passwordToggleEnabled="true"
|
||||
app:passwordToggleTint="@color/primaryColor"
|
||||
app:startIconDrawable="@drawable/baseline_vpn_key_24"
|
||||
app:startIconTint="@color/primaryColor">
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/edt_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:inputType="textPassword" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/textInput_password_confirm"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="60dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_marginBottom="100dp"
|
||||
android:hint="@string/repeat_password_text"
|
||||
app:boxBackgroundColor="@color/white"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@id/change_password_button"
|
||||
app:passwordToggleEnabled="true"
|
||||
app:passwordToggleTint="@color/primaryColor"
|
||||
app:startIconDrawable="@drawable/baseline_vpn_key_24"
|
||||
app:startIconTint="@color/primaryColor">
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/edt_confirmPassword"
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/textInput_password_confirm"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:inputType="textPassword" />
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_marginBottom="100dp"
|
||||
android:hint="@string/repeat_password_text"
|
||||
app:boxBackgroundColor="@color/white"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@id/change_password_button"
|
||||
app:passwordToggleEnabled="true"
|
||||
app:passwordToggleTint="@color/primaryColor"
|
||||
app:startIconDrawable="@drawable/baseline_vpn_key_24"
|
||||
app:startIconTint="@color/primaryColor">
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/edt_confirmPassword"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:inputType="textPassword" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/change_password_button"
|
||||
style="@style/DefaultSolid"
|
||||
android:fontFamily="@font/bebas"
|
||||
android:text="@string/change_password"
|
||||
android:textSize="20sp"
|
||||
android:padding="10dp"
|
||||
android:layout_marginStart="40dp"
|
||||
android:layout_marginEnd="40dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/change_password_button"
|
||||
style="@style/DefaultSolid"
|
||||
android:fontFamily="@font/bebas"
|
||||
android:text="@string/change_password"
|
||||
android:textSize="20sp"
|
||||
android:padding="10dp"
|
||||
android:layout_marginStart="40dp"
|
||||
android:layout_marginEnd="40dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
android:layout_height="match_parent"
|
||||
android:background="#80000000"
|
||||
android:id="@+id/progress_layout"
|
||||
android:visibility="gone"
|
||||
android:clickable="true"
|
||||
>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progressBarLoadData"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminateTint="@color/primaryColor"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user