Firebase不会更改用户密码

时间:2019-02-25 22:22:30

标签: java android firebase firebase-authentication

我在我的应用上创建了一个按钮,该按钮仅用于更改当前用户的密码即可转到另一个“活动”。

我在Google上搜索了用于在Firebase中更改密码的代码,并且已经完成了所有设置,但是我的问题是,当我单击“确认”按钮时,它什么都不做,甚至没有错误。我该如何解决?

我的 ChangePW.java

package com.pap.diogo.pilltrack;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidUserException;
import com.google.firebase.auth.FirebaseUser;

public class ChangePW extends AppCompatActivity implements View.OnClickListener{
    private EditText txtPassword;
    private FirebaseUser user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.changepw);
        Button btnChangePassword = findViewById(R.id.btnChangePassword);
        btnChangePassword.setOnClickListener(this);
        txtPassword = findViewById(R.id.txtPassword);
        user = FirebaseAuth.getInstance().getCurrentUser();
    }

    @Override
    public void onClick(View v) {
        String newPassword = txtPassword.getText().toString().trim();

        user.updatePassword(newPassword)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(ChangePW.this, "Palavra-Passe atualizada com sucesso!", Toast.LENGTH_SHORT).show();
                        Intent Home = new Intent(ChangePW.this, MainActivity.class);
                        Home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(Home);
                        finish(); // call this to finish the current activity
                    }else if (task.getException() instanceof FirebaseAuthInvalidUserException){
                        Toast.makeText(ChangePW.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
    }
}

我的 changepw.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/changepw_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="true"
            app:hintTextAppearance="@style/TextLabel">

            <EditText
                android:id="@+id/txtPassword"
                android:layout_width="275dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="15dp"
                android:layout_marginRight="15dp"
                android:background="@color/colorDarkRed"
                android:hint="Nova Password"
                android:inputType="textPassword"
                android:padding="10dp"
                android:textColor="@color/colorWhite"
                android:textSize="22sp" />

        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/btnChangePassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layout2"
            android:layout_centerInParent="true"
            android:layout_marginTop="18dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/change_password"
            android:padding="15dp"
            android:text="Confirmar"
            android:textColor="@color/colorWhite"
            android:textSize="22sp" />
    </RelativeLayout>


</RelativeLayout>

0 个答案:

没有答案