为未经授权的用户隐藏应用程序中的内容 - Flutter Firebase

发布于 2025-01-11 13:33:48 字数 840 浏览 0 评论 0原文

所以我想显示一个特殊的按钮。仅当用户使用正确的权限登录时,该按钮才应起作用(或可见)。

我发现我可以用 flutter firestore 以某种方式做到这一点。我如何将其实现到我的代码中?如果您需要我的 firestore 数据库中的更多代码或信息(屏幕),请在评论中询问。 :)

//serverstatuspage

    final serverstatuspage = Material(
        elevation: 5,
        borderRadius: BorderRadius.circular(30),
        color: Colors.green,
        child: MaterialButton(
            padding: EdgeInsets.fromLTRB(20, 15, 20, 15),
            minWidth: MediaQuery.of(context).size.width,
            onPressed: () {
              serverstatusbutton(context);
            },
            child: Text(
              "Admin Dashboard",
              textAlign: TextAlign.center,
              style: TextStyle(
                  fontSize: 20,
                  color: Colors.white,
                  fontWeight: FontWeight.bold),
            )));

So i want to show a special button. The button should only work (or be seen) when the user is logged in with the right permissions.

I saw that i can somehow do this with flutter firestore. How can i implement this to my code? If you need more code or infos (screens) from my firestore database just ask in the comments. :)

//serverstatuspage

    final serverstatuspage = Material(
        elevation: 5,
        borderRadius: BorderRadius.circular(30),
        color: Colors.green,
        child: MaterialButton(
            padding: EdgeInsets.fromLTRB(20, 15, 20, 15),
            minWidth: MediaQuery.of(context).size.width,
            onPressed: () {
              serverstatusbutton(context);
            },
            child: Text(
              "Admin Dashboard",
              textAlign: TextAlign.center,
              style: TextStyle(
                  fontSize: 20,
                  color: Colors.white,
                  fontWeight: FontWeight.bold),
            )));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2025-01-18 13:33:49

您可以根据您的权限在用户文档中添加新字段,然后在显示小部件之前或在按钮的 onPressed 时检查它们。
例如,在用户文档中添加一个新的布尔字段“isAdmin”,并为其指定 true 或 false。
现在,您获取此值并仅在 isAdmin 为 true 时使按钮起作用,如下所示:

首先获取字段值

bool isAdmin = false;
Future<void> getRole() {
  return FirebaseFirestore.instance
      .collection('users')
      .doc('yourCurrentUserDocumentID')
      .get()
      .then((snapshot) {
    if (snapshot.exists) {
      isAdmin= snapshot.data()!['isAdmin'];
    } else {
      print('Document does not exist on the database');
    }
  });
}

现在在 onPressed() 中使用此方法

final serverstatuspage = Material(
elevation: 5,
borderRadius: BorderRadius.circular(30),
color: Colors.green,
child: MaterialButton(
padding: EdgeInsets.fromLTRB(20, 15, 20, 15),
minWidth: MediaQuery.of(context).size.width,
onPressed: () async {
  await getRole();
  isAdmin ? serverstatusbutton(context) : null; //onPressed will only work if isAdmin is true
},
child: Text(
  "Admin Dashboard",
  textAlign: TextAlign.center,
  style: TextStyle(
      fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold),
),
),
);

您可以检查此之前在此处讨论过的主题。如果您想了解有关此主题的更多说明,请参阅使用云的指南函数对于了解整个想法很有用,如果您想深入了解它,基于角色的登录架构可以帮助您实现这一点。

You can add new fields in your user documents according to your permissions and then check them before displaying the widget, or at onPressed of your button.
For example, add a new boolean field 'isAdmin' in the user documents and assign either true or false to it.
Now you get this value and make the button functional only if isAdmin is true, like this:

First get the field value

bool isAdmin = false;
Future<void> getRole() {
  return FirebaseFirestore.instance
      .collection('users')
      .doc('yourCurrentUserDocumentID')
      .get()
      .then((snapshot) {
    if (snapshot.exists) {
      isAdmin= snapshot.data()!['isAdmin'];
    } else {
      print('Document does not exist on the database');
    }
  });
}

Now use this method at onPressed()

final serverstatuspage = Material(
elevation: 5,
borderRadius: BorderRadius.circular(30),
color: Colors.green,
child: MaterialButton(
padding: EdgeInsets.fromLTRB(20, 15, 20, 15),
minWidth: MediaQuery.of(context).size.width,
onPressed: () async {
  await getRole();
  isAdmin ? serverstatusbutton(context) : null; //onPressed will only work if isAdmin is true
},
child: Text(
  "Admin Dashboard",
  textAlign: TextAlign.center,
  style: TextStyle(
      fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold),
),
),
);

You can check this previously discussed topic here. If you want some more explanation on this topic, a guide using Cloud Functions can be useful to get the whole idea, and in case you want to go deep into it, the roled-based login architecture can help you with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文