简体   繁体   English

如何使用 Firebase 身份验证制作受保护的网页/面板?

[英]How do I make a protected webpage/panel with Firebase authentication?

I want to know if I can make a protected website which only people which are logged in via firebase authentication can access and use.我想知道我是否可以制作一个受保护的网站,只有通过 firebase 身份验证登录的人才能访问和使用。

Yes you can.是的你可以。 You just need to check if a user is logged in on page load.您只需要检查用户是否在页面加载时登录。

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // redirect to login
  }
});

If the user is not logged in, just redirect then.如果用户未登录,则重定向。 That was for user facing part.那是面向用户的部分。 You should setup your security rules in such a way that only authenticated users can access your Firebase resources.您应该以只有经过身份验证的用户才能访问您的 Firebase 资源的方式设置安全规则。 I'm not totally sure which services you are using but let's take Firestore as an example:我不完全确定您使用的是哪些服务,但让我们以 Firestore 为例:

match /collection/{docs=**} {
  allow read: if request.auth!= null;
}

This rules will allow only logged in users to read data from that collection.此规则将只允许登录用户从该集合中读取数据。 These were basic rules but you can write granular rules such a user specific content and much more.这些是基本规则,但您可以编写细化规则,例如用户特定内容等等。

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

相关问题 我如何限制 ionic 和 firebase-authentication 中的页面 - how do i restrict pages in ionic and firebase-authentication 如何使用 Firebase 在 Flutter 中进行电话身份验证? - How to do Phone Authentication in Flutter using Firebase? 如何获取我刚刚在 Node.js 上使用 firebase 身份验证创建的用户的令牌 ID? - How do I get the token ID of a user that I just created using firebase authentication on Node js? 如何使用 flutter 检查用户是否已存在于 firebase 身份验证中? - How do I check if a user is already exist in firebase Authentication using flutter? 如何访问我的 Python 脚本生成的网页? - How do I access the webpage that my Python script makes? 如何保护 firebase 身份验证 - How to secure firebase authentication 如何创建 Firebase 身份验证声明? - How to create Firebase Authentication claims? 如何使用 firebase 9 push 来实现这一点? - How do I achieve this using firebase 9 push? 我如何检查 email 是否已存在于身份验证 firebase 中 - how I can check if the email is already exist in authentication firebase 我们如何在不使用 Firestore 身份验证的情况下保护 Firebase Firestore? 还是必须的? - How do we secure a Firebase Firestore without using Firestore Authentication? Or is it a must?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM