代码之家  ›  专栏  ›  技术社区  ›  vivanred

Firebase数据库身份验证电话权限被拒绝

  •  0
  • vivanred  · 技术社区  · 6 年前

    我正在尝试编写应用程序,我有一些无法解决的安全问题,我需要帮助。 我在firebase上使用用户电话身份验证,它工作正常。现在我想知道,当用户auth被授予用户id时,它会保存在数据库中,并且工作正常。

    但问题是,我使用此代码为新用户在数据库上写代码的规则是:

    {
      "rules": {
    
          ".write": "auth == null ",
          ".read": "auth == null "              
      }
    }
    

    此代码用于新用户注册时:

      FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
       Firebase Ref = new Firebase( "database address" );
         Firebase usersRef = Ref.child( "Users" ).child( "user_id" );
           usersRef.setValue( user.getUid() );
    

    当我使用此规则时,身份验证被拒绝:

    {
      "rules": {
    
          ".write": "auth != null ",
          ".read": "auth != null "              
      }
    }
    

    但是我的用户已经注册了,我获得了他的id和电话表单身份验证用户如何解决这个安全问题?

    D/FirebaseAuth: Notifying id token listeners about user ( b3bizXzmH7XQfXWNvHCMVXzDHSx2 ).
    D/FirebaseApp: Notifying auth state listeners.
    D/FirebaseApp: Notified 0 auth state listeners.
    D/PhoneAuthActivity: signInWithCredential:success
    D/FirebaseAuth: Notifying id token listeners about user ( b3bizXzmH7XQfXWNvHCMVXzDHSx2 ).
    D/FirebaseApp: Notifying auth state listeners.
                   Notified 0 auth state listeners.
    D/PhoneAuthActivity: signInWithCredential:success
    W/RepoOperation: setValue at /Users/user_id failed: FirebaseError: Permission denied
    W/RepoOperation: setValue at /Users/user_id failed: FirebaseError: Permission denied
    D/FirebaseAuth: Notifying id token listeners about user ( b3bizXzmH7XQfXWNvHCMVXzDHSx2 ).
    D/FirebaseApp: Notifying auth state listeners.
    D/FirebaseApp: Notified 0 auth state listeners.
    D/PhoneAuthActivity: signInWithCredential:success
    W/RepoOperation: setValue at /Users/user_id failed: FirebaseError: Permission denied
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Kopi Bryant    6 年前

    尝试

    {
    "rules": {
    
      ".write": "auth == true",
      ".read": "auth == true"              
     }
    }
    
        2
  •  0
  •   vivanred    6 年前

    我解决了我的问题,答案是:

       FirebaseUser user = task.getResult().getUser();
              mDatabase =FirebaseDatabase.getInstance().getReference();
                     String current_uid =user.getUid();
                    mDatabase.child( "Users" ).child( "uid" ).setValue( current_uid );