2016-10-05 18 views
9

Tôi muốn triển khai chức năng thay đổi mật khẩu cho ứng dụng của mình.Thay đổi mật khẩu bằng Firebase cho Android

Tôi đã bao gồm com.google.firebase:firebase-auth:9.0.2 trong tệp build.gradle của mình và cho đến nay mọi thứ đã hoạt động tốt cho đến khi tôi cố triển khai chức năng thay đổi mật khẩu.

Tôi thấy rằng đối tượng FirebaseUser có phương thức updatePassword có mật khẩu mới làm thông số. Tôi có thể sử dụng phương pháp này và tự thực hiện xác thực. Tuy nhiên, tôi cần mật khẩu hiện tại của người dùng để so sánh với mật khẩu đã nhập và tôi không thể tìm cách lấy mật khẩu đó.

Tôi cũng tìm thấy một method khác trên đối tượng Firebase có mật khẩu cũ, mật khẩu mới và trình xử lý. Vấn đề là tôi cũng cần phải bao gồm com.firebase:firebase-client-android:2.5.2+ để truy cập lớp này và khi tôi đang cố gắng phương pháp này tôi nhận được lỗi sau:

Projects created at console.firebase.google.com must use the new Firebase Authentication SDKs available from firebase.google.com/docs/auth/

Cảm thấy như tôi là thiếu một cái gì đó ở đây. Cách tiếp cận được khuyến nghị để thực hiện điều này là gì? Và khi nào thì dùng phụ thuộc nào?

+0

thử này . –

Trả lời

17

Tôi tìm thấy một ví dụ tiện dụng của việc này trong Firebase docs:

Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails and throws FirebaseAuthRecentLoginRequiredException. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticate. For example:

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("[email protected]", "password1234"); 

// 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(newPass).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") 
          } 
         } 
        }); 
       } else { 
        Log.d(TAG, "Error auth failed") 
       } 
      } 
     }); 
7

Không có cách nào để truy xuất mật khẩu hiện tại của người dùng khỏi Xác thực Firebase.

Một cách để cho phép người dùng thay đổi mật khẩu của họ là hiển thị hộp thoại nơi họ nhập mật khẩu hiện tại của họ và mật khẩu mới mà họ muốn. Sau đó, bạn sign in (hoặc re-authenticate) người dùng có mật khẩu hiện tại và gọi updatePassword() để cập nhật.

8

Thay đổi mật khẩu trong căn cứ hỏa lực là chút khéo léo. nó không giống như những gì chúng ta thường làm để thay đổi mật khẩu trong kịch bản phía máy chủ và cơ sở dữ liệu. để triển khai chức năng thay đổi mật khẩu trong ứng dụng của bạn, trước tiên bạn cần nhận email của người dùng từ FirebaseAuth hoặc người dùng nhắc nhập email và sau đó nhắc người dùng nhập mật khẩu cũ vì bạn không thể truy xuất mật khẩu của người dùng như Frank van Puffelen nói. Sau đó bạn cần xác nhận lại điều đó. Sau khi thực hiện xác thực lại, nếu thành công, bạn có thể sử dụng updatePassword(). Tôi đã thêm một mẫu bên dưới mà tôi đã sử dụng cho ứng dụng của riêng mình. Hy vọng, nó sẽ giúp bạn.

private FirebaseUser user; 
user = FirebaseAuth.getInstance().getCurrentUser(); 
       final String email = user.getEmail(); 
       AuthCredential credential = EmailAuthProvider.getCredential(email,oldpass); 

       user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() { 
        @Override 
        public void onComplete(@NonNull Task<Void> task) { 
         if(task.isSuccessful()){ 
           user.updatePassword(newPass).addOnCompleteListener(new OnCompleteListener<Void>() { 
            @Override 
            public void onComplete(@NonNull Task<Void> task) { 
             if(!task.isSuccessful()){ 
              Snackbar snackbar_fail = Snackbar 
                .make(coordinatorLayout, "Something went wrong. Please try again later", Snackbar.LENGTH_LONG); 
              snackbar_fail.show(); 
             }else { 
              Snackbar snackbar_su = Snackbar 
                .make(coordinatorLayout, "Password Successfully Modified", Snackbar.LENGTH_LONG); 
              snackbar_su.show(); 
             } 
            } 
           }); 
         }else { 
          Snackbar snackbar_su = Snackbar 
            .make(coordinatorLayout, "Authentication Failed", Snackbar.LENGTH_LONG); 
          snackbar_su.show(); 
         } 
        } 
       }); 
      } 
     } 
-2

Truy vấn xoay quanh người dùng quên mật khẩu của họ hoặc muốn đặt lại mật khẩu của họ qua thư email. Mà có thể đạt được bằng cách Auth.sendPasswordResetEmail("[email protected]");

bắt đầu bằng cách khởi

private FirebaseAuth mAuth; 
    private FirebaseAuth.AuthStateListener mAuthListener; 
    private String DummyEmail = "[email protected]" 

mAuth = FirebaseAuth.getInstance(); 
    mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      if (firebaseAuth.getCurrentUser() == null) { 
      } 
     } 
    }; 

Một nơi nào đó khác khi người dùng yêu cầu cập nhật hoặc thiết lập lại mật khẩu của họ chỉ đơn giản là truy cập vào Mauth,

private void PassResetViaEmail(){ 
    if(mAuth != null) { 
     Log.w(" if Email authenticated", "Recovery Email has been sent to " + DummyEmail); 
     mAuth.sendPasswordResetEmail(DummyEmail); 
    } else { 
     Log.w(" error ", " bad entry "); 
    } 
    } 

Bây giờ, không cần phải gánh nặng bản thân truy vấn xung quanh cơ sở dữ liệu của bạn để tìm xem liệu thoát email hoặc không, Firebase mAuth sẽ xử lý điều đó cho bạn.

Email có được xác thực không?Nó có hoạt động trong danh sách Xác thực của bạn không? Sau đó gửi Email đặt lại mật khẩu.

enter image description here

Nội dung sẽ trông giống như liên kết này

enter image description here

reset sẽ nhắc hộp thoại sau đây trên một trang web mới io

tắm

nếu bạn hơi lo lắng bởi mẫu thiết lập lại "được tạo ra" bởi Firebase. Bạn có thể dễ dàng truy cập và tùy chỉnh thư của riêng mình từ Bảng điều khiển Firebase. Xác thực> Email mẫu> Password reset

enter image description here

Các vấn đề liên quan