我喜欢火力点,它提供了应用程序开发什么访问某个页面,还在学习吧..如何只允许经过身份验证的用户通过后台
所以我想知道我怎么可能让某些网页被唯一通过云功能访问认证用户?
我知道我可以在客户端是通过把这段代码很容易就在你的主脚本
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
//do something
} else {
// redirect to login page
}
});
它工作正常,但对于在后端的顶部做呢?
我已经试过这种方式 - index.js的云功能。
const functions = require('firebase-functions');
const firebase = require('firebase');
const config = {
// needed configs here
};
firebase.initializeApp(config);
exports.api = functions.https.onRequest((req, res) => {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
res.status(200).send("logged in")
} else {
res.status(200).send("not logged in")
}
});
});
所以我可以这样来配置
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"rewrites": [ {
"source": "/getstarted", "function": "api"
} ]
}
}
重写规则当我访问/ getstarted页面,它会检查是否验证,如果不只是给未经授权的文/重定向到他们/登录页面
因此,在我登录前端后,我尝试访问/ getstarted页面,它仍然显示没有登录
我必须做错了什么。 。
[此](https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint)和[这](https://github.com/firebase/functions-samples/tree/master/authenticated-json-api)示例显示了一种方式,但这些代码看起来很恐怖,我不知道如何实现什么我想 – user7716943
这两个例子都是通过POST验证用户请求http功能的。您正试图使用GET保护http文件。如下所述,这需要成为前端问题,而不是使用Firebase云代码时的后端问题。 Express +节点通过模板生成您的页面,Firebase云代码不应该这样做。这是您的前端使用的服务。 – David