简体   繁体   English

Flutter:firebase_auth 好像不行,怎么授权?

[英]Flutter: firebase_auth seems not to work, how do I authorize?

I have a working flutter app which uses the cloud_firestore package, it's going well with Android and iOS.我有一个工作 flutter 应用程序,它使用 cloud_firestore package,它与 Android 和 Z1BDF605905204DB1ZCBDF8 配合得很好But my database is "allow read,write: if true", and so Google keeps reminding me to fix this unsecurity.但是我的数据库是“允许读,写:如果为真”,所以谷歌一直提醒我解决这个不安全问题。 This I did:这是我做的:

  • including firebase_auth包括 firebase_auth
  • changing my rules on firebase改变我对 firebase 的规则
  • creating a mail/password account创建邮件/密码帐户
  • implementing the login procedure实现登录过程
  • changing the rules to "allow write: if request.auth;= null;"将规则更改为“允许写入:如果 request.auth;= null;”

I think I made everything ok.我想我做的一切都很好。 In the app, it seems so:在应用程序中,似乎是这样:

print("User ${(await FirebaseAuth.instance.currentUser()).email})");

This gives me the mail of my account.这给了我我帐户的邮件。 Also I have a onAuthStageChanged listener and I get what I would expect.我也有一个 onAuthStageChanged 监听器,我得到了我所期望的。 So I guess the login did work.所以我猜登录确实有效。

But if I try to make a database access eg with.setData(), I get an error但是,如果我尝试使用.setData() 进行数据库访问,则会出现错误

W/Firestore( 4411): (19.0.0) [Firestore]: Write failed at...: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null} W/Firestore(4411):(19.0.0)[Firestore]:写入失败...:状态{code=PERMISSION_DENIED,描述=缺少或权限不足。,原因=null}

It looked for me as if the auth package is not informing the firestore package correctly.在我看来,好像 auth package 没有正确通知 Firestore package。 In the logs, I see在日志中,我看到

D/FirebaseAuth( 5698): Notifying id token listeners about user (...). D/FirebaseAuth(5698):通知 id 令牌侦听器有关用户 (...)。

I would expect something like "Firestore: received token...", but this does not come.我希望像“Firestore:收到令牌......”这样的东西,但这不会出现。

Do I have to be careful with the initialization order?我必须小心初始化顺序吗? Mine is我的是

  1. _app = await Firebase.App.configure... _app = 等待 Firebase.App.configure...
  2. _firestore = Firestore(app: _app); _firestore = Firestore(应用程序:_app);
  3. await FirebaseAuth.instance.signInWithEmailAndPassword...等待 FirebaseAuth.instance.signInWithEmailAndPassword...

I tried it on some Samsung and OnePlus devices with Android 8 and 9.我在一些带有 Android 8 和 9 的三星和 OnePlus 设备上进行了尝试。

Any hints someone?有什么提示吗?

I got it running!!我让它运行了!! Used a hint from Flutter & Firebase - using firebase_storage along with firebase_auth使用来自Flutter 和 Firebase 的提示 - 使用 firebase_storage 和 firebase_auth

When accessing the database, I used a pointer to the firestore which I created like访问数据库时,我使用了指向我创建的 firestore 的指针

static Firestore _firestore;
...
_firestore = Firestore(app: ...);

And later I used后来我用

await _firestore.collection(...)...setData(...);

But the Firestore seems to change after a login.但是登录后 Firestore 似乎发生了变化。 It works when I write:它在我写的时候起作用:

await Firestore.instance.collection...setData

Interesting!!有趣的!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM