Firebase托管的身份验证 [英] Authentication for firebase hosting
问题描述
我在Firebase托管上托管了一个静态应用程序,其后端也在Firebase上(使用firebase JS api进行通信).我想向该网站的所有页面添加一个简单的身份验证页面,以便只有我想要的用户才能访问该网站.这可能吗?
I have a static app hosted on Firebase hosting whose backend is also on Firebase(communicating using firebase JS api). I want to add a simple auth page to all pages of this website so that only users I want can access this site. Is this possible?
看了看文档,却没有找到任何对我有帮助的东西.
Looked at the docs but didn't find anything that helps me in this regard.
推荐答案
您可以使用Firebase函数和Express调用来做到这一点.将所有静态文件放入名为functions/admin
的文件夹,然后将此函数放入functions/index.js
:
You can do this using Firebase Functions, and an Express call. Put all of your static files into a folder named functions/admin
and put this function into functions/index.js
:
exports.admin = functions.https.onRequest((req, res) => {
const url = req.originalUrl ? req.originalUrl : '/index.html' // default to index.html
res.sendfile('admin' + url)
})
然后,向功能服务器请求/admin/*
的请求将提供相同名称的文件.
Then, a request to your functions server for /admin/*
will serve up the file of the same name.
如果要添加授权,请尝试以下操作:
If you want to add authorization, try this:
exports.admin = functions.https.onRequest(async (req, res) => {
const url = req.originalUrl ? req.originalUrl : '/index.html'
const user = await get_user(req) // get the current user
if (user && user.is_admin) // is current user an admin?
res.sendfile('admin' + url)
else {
res.status(403).send(null)
}
})
您将必须定义get_user()
,以便它返回带有is_admin
字段的用户对象.
You will have to define get_user()
so it returns a user object with an is_admin
field.
这篇关于Firebase托管的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!