[英]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.