验证后,Firebase权限被拒绝 [英] Firebase Permission denied after auth
问题描述
我的主要活动代码如下 -
private FirebaseAuth auth;
private EditText inputEmail,inputPassword;
私人按钮btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
//获取Firebase身份验证实例
auth = FirebaseAuth.getInstance(); $(b)b
if(auth.getCurrentUser()!= null){
startActivity(new Intent(MainActivity.this,MainActivity.class));
finish();
}
setContentView(R.layout.activity_main);
inputEmail =(EditText)findViewById(R.id.txtMobile);
inputPassword =(EditText)findViewById(R.id.txtPassword);
btnLogin =(Button)findViewById(R.id.btnLogin);
auth = FirebaseAuth.getInstance();
btnLogin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String email = inputEmail。 getText()。toString();
final String password = inputPassword.getText()。toString();
$ b //认证用户
auth.signInWithEmailAndPassword电子邮件,密码)
.addOnCompleteListener(MainActivity.this,new OnCompleteListener< AuthResult>(){
@Override
public void onComplete(@NonNull Task< AuthResult> task){
如果登录失败,向用户显示一条消息,如果登录成功
//将通知auth状态监听器,并且处理
//登录用户的逻辑可以在监听器。
if(!task.isSuccessful()){
//出现错误
Log.e(Invalid,Invalid);
} else {
/ *意图意图=新意图(MainActivity.this,Second.class);
startActivity(intent);
finish(); * /
Firebase.getDefaultConfig();
auth = FirebaseAuth.getInstance();如果(auth.getCurrentUser()!= null){
Log.e(awesome,auth.getCurrentUser()。getEmail());
}
Firebase firebase =新的Firebase(https://test-fd3f2.firebaseio.com);
firebase.addChildEventListener(new ChildEventListener(){
//在添加到Firebase时检索新帖子
@SuppressWarnings(unchecked)
@Override
public void onChildAdded(DataSnapshot snapshot,String previousChildKey){$ b $ if(snapshot.getValue()!= null){
String mobileNum = snapshot.getValue()。toString();
Log.e (here,mobileNum);
}
//Log.e(\"Title:,newPost.get(password).toString());
}
@Override
public void onChildChanged(DataS napshot dataSnapshot,String s){
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot){
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){
$ @覆盖
public void onCancelled(FirebaseError firebaseError){
Log.e(Cancel,firebaseError.getCode()+);
Log.e(Cancel,firebaseError.getDetails());
Log.e(Cancel,firebaseError.getMessage());
}
// ... ChildEventListener还定义了onChildChanged,onChildRemoved,
// onChildMoved和onCanceled,在后面的章节中有介绍。
});
}
}
});
}
});
$ b $ / code>
更新 -
我的firebase规则如下 -
{
rules:{
.read:auth != null,
.write:auth!= null
}
}
您正在混合使用不同的Firebase SDK: $ b code> auth.signInWithEmailAndPassword()来自新的3.x / 9.x Firebase SDK
新的Firebase )
来自Firebase 2.x SDK
登录到新的SDK不会验证连接到使用旧SDK版本创建的数据库。所以您的数据库连接仍然是未经验证的。
$ b 解决方案是在您的build.gradle文件中为所有Firebase库使用单一版本(9.x)
I am making an android application. I first login the user with email and password and then in OnComplete method,if the task is successful, I am trying to access data on the database. But every time I get the message Listen at / failed.
My Main Activity code is as follows -
private FirebaseAuth auth;
private EditText inputEmail, inputPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
}
setContentView(R.layout.activity_main);
inputEmail = (EditText) findViewById(R.id.txtMobile);
inputPassword = (EditText) findViewById(R.id.txtPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
auth = FirebaseAuth.getInstance();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
// there was an error
Log.e("Invalid","Invalid");
} else {
/*Intent intent = new Intent(MainActivity.this, Second.class);
startActivity(intent);
finish();*/
Firebase.getDefaultConfig();
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
Log.e("awesome",auth.getCurrentUser().getEmail());
}
Firebase firebase = new Firebase("https://test-fd3f2.firebaseio.com");
firebase.addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to Firebase
@SuppressWarnings("unchecked")
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
if(snapshot.getValue()!= null) {
String mobileNum = snapshot.getValue().toString();
Log.e("here", mobileNum);
}
//Log.e("Title: ",newPost.get("password").toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("Cancel",firebaseError.getCode()+"");
Log.e("Cancel",firebaseError.getDetails());
Log.e("Cancel",firebaseError.getMessage());
}
//... ChildEventListener also defines onChildChanged, onChildRemoved,
// onChildMoved and onCanceled, covered in later sections.
});
}
}
});
}
});
}
UPDATE - My firebase rules is as follows-
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
You're mixing different Firebase SDKs:
auth.signInWithEmailAndPassword()
is from the new 3.x/9.x Firebase SDKnew Firebase()
is from the Firebase 2.x SDK
Signing in to the new SDK, will not authenticate the connection to the database made with the older SDK version. So your database connection is still unauthenticated.
The solution is to use a single version (9.x) for all Firebase libraries in your build.gradle file
这篇关于验证后,Firebase权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!